Install Shield Script小结(调用api,com,bat)
老大让我在先前打包程序基础上,把现在项目打包了(web项目)。基本要求是这样子的,用项目包括所需的控件,建虚拟目录。在用户可对SQLSERVER 对行配置,也可安装包内SQLEXPRESS(如果用户配置后无法连接且判断机器内未安装SQLSERVER即安装包内SQLEXPRESS),捣鼓了一星期,把学到的共享下。语法主要看下那个教程就可以,函数直接F1
基本资料:
InstallshieldX安装制作杂记.rar
installshieldjc.rar
再就是InstallShield和帮助,推荐它的官方论坛http://community.installshield.com/
1.用InstallShield Script调用WINAPI方法
如取得机器的登陆用户名。MSDN
GetUserName
The GetUserName function retrieves the name of the user associated with the current thread.
Use the GetUserNameEx function to retrieve the user name in a specified format. Additional information is provided by the
BOOL GetUserName(
LPTSTR lpBuffer,
LPDWORD nSize
);
.......
DLL | Requires Advapi32.dll. |
---|---|
Unicode |
Implemented as GetUserNameW (Unicode) and GetUserNameA (ANSI). Note that Unicode support on Windows Me/98/95 requires Microsoft Layer for Unicode. |
.......
参照installshieldjc.rar里的对应于Winapi里的函数类型声明,于api函数签名对应,如下声明使用:
function OnBegin()
STRING strLogonUser;
NUMBER numLogonUser
begin
//.
numLogonUser = 255;
GetUserNameA(strLogonUser, numLogonUser);
MessageBox(strLogonUser,INFORMATION);
//.
end;
2.用InstallShield Script调用其他Com
例1:使用DOM
function WriteConfig(info)
STRING fileName;
OBJECT configDoc, child1,child2;
begin
set configDoc = CoCreateObject("MSXML.DOMDocument");
fileName=INSTALLDIR^"oceswebs\\Web.config";
// MessageBox(fileName,INFORMATION);
if configDoc.Load(fileName) then
set child1 = configDoc.selectSingleNode("//configuration/appSettings/add[@key='CONNECTIONSTRINGFORPROVINCE']/@value");
set child2 = configDoc.selectSingleNode("//configuration/connectionStrings/add[@name='CONNECTIONSTRINGFORPROVINCE']/@connectionString");
child1.text=info;
child2.text=info;
//MessageBox(child.text,INFORMATION);
else
MessageBox("未找到Web.Config配置文件",INFORMATION) ;
endif;
configDoc.save(fileName);
set configDoc = NOTHING;
end;
用 set configDoc = CoCreateObject("MSXML.DOMDocument");创建对象后,即可轻松通过XPATH操作XML了
例2:使用ADO:
//参数:SERVERNAME,服务器名
//DBNAME,数据库名
//DBUSER 用户名
//PWD, 登陆密码
//ATHMODE 登陆模式 0为sql登陆模式,1为WINDOWS验证登陆模式
//返回值 -1,无法连接不上sql server
// 返回值 0 连接上sql server 但无此数据库
// 返回值 1 连接上sql server 并存在此数据库
#ifndef CONNDB
#define CONNDB 1
prototype ConnDB(STRING,STRING,STRING,STRING,NUMBER);
function NUMBER ConnDB(SERVERNAME,DBNAME,DBUSER,PWD,ATHMODE)
OBJECT oADOcnn, oADOrs;
STRING svCnn, svSQL;
NUMBER nvReturn, nvSize, nvFileHandle;
STRING svResult, szPath, szFileName, svBuffer, szTmp;
STRING gsSQLServer, gsSQLUID, gsSQLPass, gsSQLDBName;
BOOL gbWindowsLogin;
begin
//gsSQLServer = "192.168.75.80";
//gsSQLDBName="solotemp";
//gsSQLUID = "sa";
//gsSQLPass = "iflyecl";
gsSQLServer = SERVERNAME;
gsSQLDBName = DBNAME;
gsSQLUID = DBUSER;
gsSQLPass= PWD;
gbWindowsLogin = ATHMODE;
if (gbWindowsLogin = TRUE) then
svCnn = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=";
svCnn = svCnn + gsSQLUID + ";Initial Catalog=master;Data Source=" + gsSQLServer;
else
svCnn = "Provider=SQLOLEDB.1;Password=" + gsSQLPass + ";Persist Security Info=True;User ID=";
svCnn = svCnn + gsSQLUID + ";Initial Catalog=master;Data Source=" + gsSQLServer;
endif;
//login to the master database
try
set oADOcnn = CreateObject("ADODB.Connection");
catch
MessageBox ("Error :" + Err.Description , WARNING);
nvReturn= -1;
return nvReturn;
endcatch;
oADOcnn.ConnectionTimeout = 32;
oADOcnn.CommandTimeout = 8;
oADOcnn.CursorLocation = 3; //adUseClient
try
oADOcnn.Open(svCnn);
catch
MessageBox ("Error :" + Err.Description , WARNING);
nvReturn= -1;
return nvReturn;
endcatch;
oADOcnn.CommandTimeout = 8;
oADOcnn.CursorLocation = 3; //adUseClient
//adOpenDynamic =2 , adOpenForwardOnly =0
//adLockReadOnly =1, adLockOptimistic =3
svSQL = "Select * from sysdatabases where [name] = N'"+gsSQLDBName+"'";
//MessageBox(svSQL,INFORMATION);
set oADOrs = oADOcnn.Execute(svSQL);
if (!oADOrs.BOF) then
MessageBox( "Database "+gsSQLDBName+" exists", WARNING);
nvReturn=1;
else
MessageBox( "Database "+gsSQLDBName+" Does NOT exists,GO on to Creat it", INFORMATION);
nvReturn=0;
endif;
set oADOrs = NULL;
oADOcnn.Close();
set oADOcnn = NULL;
return nvReturn;
end;
SdShowMsg (sMsg, TRUE);
result = result + LaunchAppAndWait(INSTALLDIR^"Component\\config.bat","",LAAW_OPTION_WAIT|LAAW_OPTION_HIDDEN);
sCmdLine= sServerName+" \""+ sUserName+"\" \""+ sPWD+ "\"";
result = result + LaunchAppAndWait(INSTALLDIR^"Component\\setup2.bat",sCmdLine,LAAW_OPTION_WAIT|LAAW_OPTION_HIDDEN);
SdShowMsg (sMsg, FALSE);
setup2.bat的内容如下:
"sqlcmd.exe" -S %1 -U %2 -P %3 -i "c:\ecl\ecl_init2.sql"
我要把 server name,username,password传入,即sServerName+" \""+ sUserName+"\" \""+ sPWD+ "\""; 传入,关于LaunchAppAndWait提醒一点,开始以为它和LaunchApp一样,只要找到bat,或exe文件,运行即返回值即返回0,安装程序就控制不了它了,当我用它来启动一个服务时,结果服务启动失败,返回了-1。我到处MessageBox也找到这个原因。