Inno setup常用代码补充【收藏】

1、调用DOS命令或批处理等其它命令行工具等
Exec(ExpandConstant(‘{cmd}’), ‘/c dir c:\ >a.txt’,ExpandConstant(‘{app}’), SW_SHOWNORMAL, ewNoWait, ResultCode);

 

2、不显示一些特定的安装界面
[code]
function ShouldSkipPage(PageID: Integer): Boolean;
begin
if PageID=wpReady then
result := true;
end;
wpReady 是准备安装界面
PageID查询 INNO帮助中的 Pascal 脚本: 事件函数常量
预定义向导页 CurPageID 值
wpWelcome, wpLicense, wpPassword, wpInfoBefore, wpUserInfo, wpSelectDir, wpSelectComponents, wpSelectProgramGroup, wpSelectTasks, wpReady, wpPreparing, wpInstalling, wpInfoAfter, wpFinished
如果是自定义的窗体,则PageID可能是100,你可以在curPageChanged(CurPageID: Integer)方法中打印出到curpageid到底是多少。

 

3、获取SQLserver安装路径
var
dbpath:string;
rtn:boolean;
rtn := RegQueryStringValue(HKEY_LOCAL_MACHINE, 'SOFTWARE\Microsoft\MSSQLServer\Setup','SQLPath', dbpath);
if (!rtn) then dbpath := ExpandConstant('{app}');

 

4、获取本机的IP地址
ip:string;
rtn:boolean;
//{083565F8-18F0-4F92-8797-9AD701FCF1BF}视网卡而定见LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards处
rtn :=RegQueryStringValue(HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Services\{083565F8-18F0-4F92-8797-9AD701FCF1BF}\Parameters\TcpIp','IpAddress', ip);
if (not rtn) or (ip='0.0.0.0') or (ip='') then ip := '127.0.0.1';

 

5、检查数据库是否安装
//检查是否已安装SQL
try
CreateOleObject('SQLDMO.SQLServer');
except
RaiseException('您还没有安装SQL数据库.'#13#13'(Error ''' + GetExceptionMessage + ''' occurred)');
end;

 

6、根据环境变量选择组件,获取系统环境变量值见方法6
procedure CurPageChanged(CurPageID: Integer);
var
path:string;
rtn:boolean;
begin
//MsgBox(inttostr(curpageid),mbInformation,mb_ok);
if (curpageId =7) then
begin
rtn := checkTomcat6(path);
if rtn then//如果系统以前没安装tomcat则选中组件,否则不选中
begin
WizardForm.ComponentsList.CheckItem(2,coUnCheck);
WizardForm.ComponentsList.ItemEnabled[2] := false;
end;
end;
end;

 

7、系统环境变量操作
读取:
function GetEnv(const EnvVar: String): String;
举例:GetEnv('java_home')
设置:
[Setup]
ChangesEnvironment=true
[code]
//环境变量名、值、是否安装(删除)、是否所有用户有效
procedure SetEnv(aEnvName, aEnvValue: string; aIsInstall: Boolean);//设置环境变量函数
var
sOrgValue: string;
x,len: integer;
begin
//得到以前的值
RegQueryStringValue(HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', aEnvName, sOrgValue)
sOrgValue := Trim(sOrgValue);
begin
x := pos( Uppercase(aEnvValue),Uppercase(sOrgValue));
len := length(aEnvValue);
if aIsInstall then//是安装还是反安装
begin
if length(sOrgValue)>0 then aEnvValue := ';'+ aEnvValue;
if x = 0 then Insert(aEnvValue,sOrgValue,length(sOrgValue) +1);
end
else
begin
if x>0 then Delete(sOrgValue,x,len);
if length(sOrgValue)=0 then
begin
RegDeleteValue(HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment',aEnvName);
exit;
end;
end;
StringChange(sOrgValue,';;',';');
RegWriteStringValue(HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', aEnvName, sOrgValue)
end;
end;

 

8、获取NT服务安装路径
Windows服务在系统安装后会在注册表的 "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\"下
以服务的ServiceName建1个目录,
目录中会有"ImagePath"
举例获取tomcat6服务安装路径:
RegQueryStringValue(HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Services\tomcat6','ImagePath', sPath);
posted @ 2011-01-22 00:28  Day_Dreamer  阅读(1726)  评论(0编辑  收藏  举报