Jumps_wind

导航

InstallShield For .Net制作.Net项目安装包之完整代码

这是我做过的一个B/S项目的安装包代码(InstallShield For .Net),安装界面非常漂亮,现在跟大家一起分享一下:

/////////////////////////////////////////////////////////////////////////////
//
//  File Name:    Setup.rul
//
//  Description:  InstallShield script
//
//  Comments:     This minimal project is intended for advanced users.
//                To create a robust project with step-by-step
//      instructions, use the Project Wizard.
//
/////////////////////////////////////////////////////////////////////////////

// Include header files /////////////////////////////////////////////////////
#include "ifx.h"
#include "CreateDB.Rul"  
#include "System.rul"
#include "IIS.rul"   
#include "CustomPassword.Rul" 
   
////////////////////// string defines ////////////////////////////
#define DBSERVER "(local)"//数据库服务器
#define WEBNAME "****.net"
#define ****NetReg "****\\****.net"
#define INSTALL_DB_FILE_DELETE "****.iss"      
#define DBFILENAME1 "****.MDF"  
#define DBFILENAME2 "****.LDF" 

//////////////////// installation declarations ///////////////////

    // In order to have your InstallScript function executed as a custom
    // action by the Windows Installer, it must be prototyped as an
    // entry-point function.

    // The keyword export identifies MyFunction() as an entry-point function.
    // The argument it accepts must be a handle to the Installer database.
   
      /* export prototype MyFunction(HWND); */ 

    // To Do:  Declare global variables, define constants, and prototype 
    //         defined and DLL functions here.


// ----- DLL function prototypes -----


    // your DLL function prototypes


// ---- script function prototypes -----


    // your script function prototypes

    // your global variables

//全局变量定义
STRING szSQLPATH, szDatabaseDestinationPath,svEdit1,szTemp,DBNAME;
STRING  svServer, svUser, svPassword, svDBName;
STRING  szVer,szParameter;
BOOL bDeleteDB, bHaveOldDB, bHaveSql ;
int i; 
VARIANT SQLOBJ;
NUMBER  nvFileHandle;

///////////////////////////////////////////////////////////////////////////
//                                                                          
//  FUNCTION:   OnFirstUIBefore                                           
//                                                                          
//  EVENT:      FirstUIBefore event is sent when installation is run for the first
//              time on given machine. In the handler installation usually displays
//              UI allowing end user to specify installation parameters. After this
//              function returns, ComponentTransferData is called to perform file
//              transfer.
//                                                                          
///////////////////////////////////////////////////////////////////////////
function OnFirstUIBefore() 
    number  nResult, sdResult, nSetupType;
    NUMBER  nRootKey,nType, nSize,abc;
    string  szTitle, szMsg;
    string  szLicenseFile, szQuestion;
    string  szDir;
    LIST    listStartCopy;
    STRING szQuestion1, szQuestion2;
begin
 //系统初始安装过程 
 DBNAME="*";
    SetTitle( @COMPANY_NAME + " - " + @PRODUCT_NAME + "  安装向导", 24, WHITE );
    SetTitle( @COMPANY_NAME + " - " + @PRODUCT_NAME + "  安装向导", 0, BACKGROUNDCAPTION );                    
    UNINSTALL_DISPLAYNAME = "*.net";

    SHELL_OBJECT_FOLDER = @PRODUCT_NAME;
   
    svServer = "localhost";
    svDBName = DBNAME;
   
    bHaveOldDB = FALSE;
    bDeleteDB = FALSE;
   
    szDir = INSTALLDIR;

Dlg_Start:
    SdShowMsg("正在检测操作系统版本...",TRUE);
 Delay(1);
 nResult = CheckSystem(2000);
 
 if ( nResult != 0 ) then
  switch( nResult )
   case 1:
    szMsg = "请使用 windows 2000 版本及以上";
   case 2:
    szMsg = "初始化检测函数失败";
  endswitch;
  MessageBox(szMsg, SEVERE);
  abort;
 endif;
  
 SdShowMsg("正在检测 Internet 信息服务...", TRUE);
 Delay(1);
 nResult = CheckIIS(5);
 
 if ( nResult != 0 ) then
  switch ( nResult )
         case 1:
          szMsg = "初始化检测函数失败";
         case 2:
          szMsg = "本机没安装IIS";
         case 3:
          szMsg = "本机 IIS 版本不是5及以上";
  endswitch;
  MessageBox(szMsg, SEVERE);
  abort;
 endif;
 
 SdShowMsg("正在检测 Microsoft DotNet Framework...",TRUE);
 Delay(2);
 nResult = CheckSystem(3705);
 
 if ( nResult != 0 ) then
  switch ( nResult )
         case 1:
          szMsg = "初始化检测函数失败"; 
    MessageBox(szMsg, SEVERE);
    abort;
         case 2:
          //szMsg = "本机没安装.NetFrameWork,请先安装.NetFrameWork软件包";
          //运行.NetFrameWork安装包   
    SdShowMsg("正在安装 Microsoft DotNet Framework...",TRUE);
          if LaunchAppAndWait(SUPPORTDIR ^ "dotnetfx.exe", "", WAIT) < 0 then
        szMsg = "安装.NetFrameWork软件包出错,请手工安装!";
          MessageBox(szMsg, SEVERE);
          abort;
    endif;
    SdShowMsg("成功安装 Microsoft DotNet Framework...",TRUE);
    Delay(2);
  endswitch;
 endif;
       
 SdShowMsg("正在检测数据库服务...", TRUE);
 Delay(1);
 nResult = CheckSQLServer(svServer, szSQLPATH, szDatabaseDestinationPath, 8);

 if ( nResult != 0 ) then
  switch( nResult )
      case 1:
       szMsg = "初始化检测函数失败";
      case 2:
       szMsg = "本机没安装SQL Server";
      case 3:
       szMsg = "本机SQL Server版本不是SQL Server 2000";
      case 4:
       szMsg = "无法检测到SQL数据库服务器安装路径";
      case 4:
       szMsg = "无法检测到SQL数据库文件存放路径";     
  endswitch;
  MessageBox(szMsg, SEVERE);
  abort;
 else
  bHaveSql=TRUE;
 endif;

    SdShowMsg("...", FALSE);

Dlg_SdWelcome:
 //显示欢迎界面

    szTitle = "*.net";
    szMsg   = "    ***软件发展有限公司 *.net 安装向导将在计算机中安装 *.net。
                若要继续,请单击“下一步”。";
    sdResult = SdWelcome( szTitle, szMsg );
    if (sdResult = BACK) goto Dlg_Start;

Dlg_SdLicense:
 //显示许可协议
    szLicenseFile = SUPPORTDIR ^ "许可协议.txt";
    szTitle    = "";
    szMsg      = "";
    szQuestion = "";
    sdResult    = SdLicense( szTitle, szMsg, szQuestion, szLicenseFile );
    if (sdResult = BACK) goto Dlg_SdWelcome;

AskDatabasePwd:
 //询问数据库访问密码  
    SdShowMsg("正在检测数据库...", TRUE);
 Delay(1);     
   nResult= SdCustomPassword("PasswordDlh","    安装程序需要SQL数据库服务器
            "超级用户权限,请输入SQL数据库服务器超级用户登录信息",
            "输入SQL数据库服务器超级用户登录信息","用  户","口  令",
            svUser,svPassword);
   if (nResult = BACK) goto Dlg_SdLicense;

   SdShowMsg("安装程序正在启动SQL数据库服务器...",TRUE);
   SQLOBJ=CreateObject("SQLDMO.SQLServer");
   if IsObject(SQLOBJ) then
   try 
    try
     SQLOBJ.Start(TRUE, "(local)",svUser,svPassword);
    catch                                  
     SQLOBJ.Connect("(local)", svUser, svPassword);     
    endcatch; 
    if !SQLOBJ.VerifyConnection then
      MessageBox("SQL数据库服务器不能启动,安装程序将退出!",SEVERE);  
      abort;
    endif;    
   catch
      MessageBox("SQL数据库服务器不能启动,安装程序将退出!",SEVERE);  
      abort;
   endcatch;  
   else
     MessageBox("没有找到SQL数据库服务器,安装程序将退出!",SEVERE);  
     abort;
   endif;   
   SdShowMsg("...",FALSE); 
             
   bHaveSql=TRUE;
           
Dlg_InputDBName:   
   svEdit1=DBNAME;    
   // Get a target folder name from the user.
   nResult=SdShowDlgEdit1 ("请输入安装数据库名", "请输入安装数据库名:",
                           "数据库名:", svEdit1);
   if (nResult< 0) then
      MessageBox ("创建对话窗口失败!", SEVERE);                      
   elseif (nResult==BACK)then  
      goto AskDatabasePwd;
   elseif(StrLength(svEdit1)==0) then
      MessageBox ("数据库名不能为空!", SEVERE);                      
      goto Dlg_InputDBName;
   endif;
   DBNAME=svEdit1;

   //检查数据库是否存在
 
    for i=1 to SQLOBJ.Databases.Count
      szTemp=SQLOBJ.Databases.Item(i).Name;
      StrToUpper(szTemp,szTemp);
      if (szTemp==svEdit1) then 
       MessageBox ("数据库"+svEdit1+"已经存在,点击“确定”继续安装,
                   "不会对现有数据库产生任何影响……", INFORMATION);  
        /*if (AskYesNo("数据库"+svEdit1+"已经存在,如果继续,
                       "现存的数据库将被删除,是否继续?",NO)==NO) then
          goto Dlg_InputDBName;
         else
           bDeleteDB = TRUE;
         endif;*/
      endif;
    endfor;        
    if (sdResult = BACK) goto AskDatabasePwd;
         
Dlg_SdAskVirtualDirectories:
 //询问虚拟目录
    if (sdResult = BACK) goto Dlg_SdAskVirtualDirectories;

Dlg_SdAskDestPath: 
    //读取注册表
 /*nRootKey = HKEY_CURRENT_USER;
 if (RegDBSetDefaultRoot (nRootKey) < 0) then
     MessageBox("初始化失败", SEVERE);
   else                        
     abc = RegDBGetKeyValueEx(****NetReg,"InstallPath",nType,szDir,nSize);
  if (abc < 0) then     */
      szTitle = "";
      szMsg   = "";
      sdResult = SdAskDestPath( szTitle, szMsg, szDir, 0 );
      INSTALLDIR = szDir;
      if (sdResult = BACK) goto AskDatabasePwd;
  /*else
   StrSub(szDir, szDir, 0, StrFind(szDir, "****.net"));
   INSTALLDIR = szDir;
  endif;
 endif;  */

Dlg_SdStartCopy:
    szTitle = "";
    szMsg   = "";
    listStartCopy = ListCreate( STRINGLIST );
   
    ListAddString(listStartCopy, "安装组件:", AFTER);
    ListAddString(listStartCopy, "\tWeb程序" , AFTER);
    ListAddString(listStartCopy, "\t帮助文档" , AFTER);
    ListAddString(listStartCopy, "\t软件狗驱动" , AFTER);
    ListAddString(listStartCopy, "\t数据库配置工具" , AFTER);
    ListAddString(listStartCopy, "", AFTER);
   
    ListAddString(listStartCopy, "目标目录:", AFTER);
    ListAddString(listStartCopy, "\t" + INSTALLDIR , AFTER);
    ListAddString(listStartCopy, "", AFTER);
   
    ListAddString(listStartCopy, "命令目录:", AFTER);
    ListAddString(listStartCopy, "\t***-****.net行业管理系统" , AFTER);
    ListAddString(listStartCopy, "", AFTER);
   
    StrToUpper(szMsg, svDBName);
    ListAddString(listStartCopy, "数据库:", AFTER);
    ListAddString(listStartCopy, "\t" + szMsg, AFTER);
    ListAddString(listStartCopy, "", AFTER);
   
    ListAddString(listStartCopy, "虚拟目录:", AFTER);
    ListAddString(listStartCopy, "\t" + WEBNAME , AFTER);
    ListAddString(listStartCopy, "\t本机使用 http://localhost/" +
    WEBNAME + " 进行访问", AFTER);
    ListAddString(listStartCopy, "", AFTER);
   
    szMsg = "";

    sdResult = SdStartCopy( szTitle, szMsg, listStartCopy );
    ListDestroy(listStartCopy);
    if (sdResult = BACK) goto Dlg_SdAskDestPath;

    Enable(STATUSEX);

    return 0;
end;

///////////////////////////////////////////////////////////////////////////////
//
//  FUNCTION:   OnFirstUIAfter
//
//  EVENT:      FirstUIAfter event is sent after file transfer, when installation
//              is run for the first time on given machine. In this event handler
//              installation usually displays UI that will inform end user that
//              installation has been completed successfully.
//
/////////////////////////////////////////////////////////////////////////////// 
function OnFirstUIAfter()
    STRING szTitle, szMsg, szMsg1, szMsg2, szOption1, szOption2;
    NUMBER bOpt1, bOpt2, nResult, nType, nSize;
    NUMBER nRootKey, SQLVersion;    
   STRING szTemp,svResult, svTime, ProductVersion;
begin
 
 /*SdShowMsg("安装软件狗驱动...", TRUE);
    nResult = InstallDogDriver();
    if ( nResult != 0 ) then
    MessageBox("安装软件狗驱动程序失败,请手工安装软件狗驱动程序!\n"
              +'手工安装,请执行'+INSTALLDIR ^ '\\****\\
              软件狗驱动程序\\****.exe',SEVERE);
    endif; */
          
    SdShowMsg("安装产品数据库...", TRUE);
    if (bDeleteDB) then
        nResult = DeleteDatabase(svDBName);
        if ( nResult != 0 ) then
   szMsg = "删除旧数据库失败";
   MessageBox(szMsg, SEVERE);
     endif;
    endif;

  Disable(STATUSEX);             
  OpenFileMode (FILE_MODE_APPEND);
  //创建数据库安装文件
  if (CreateFile (nvFileHandle, SUPPORTDIR, INSTALL_DB_FILE_DELETE) < 0) then
     MessageBox ("创建文件失败!", SEVERE);
     abort;
  else
     szMsg = "declare @dttm varchar(55) \n"
           +"select  @dttm=convert(varchar,getdate(),113)\n"
           +"raiserror('Beginning InstPubs.SQL at %s ....',1,1,
           @dttm) with nowait \n"
           +"GO \n"
           +"use master \n"
           +"go \n"

           +"raiserror('Creating "+svEdit1+" database....',0,1) \n"
           +"go \n"
   
           +" sp_attach_db '"+svEdit1+"',@filename1='"+
           INSTALLDIR+"****.net\\DataBase\\"+DBFILENAME1+"',"+"@filename2='"
           +INSTALLDIR+"****.net\\DataBase\\"+DBFILENAME2+"' \n"
           +"go\n";

     if (WriteLine(nvFileHandle, szMsg) < 0) then
        MessageBox("写入文件发生错误!", SEVERE);
        abort;
     endif;
   endif;
   CloseFile (nvFileHandle);

   szParameter=" -U "+svUser+" -P "+svPassword+" -i "+
   SUPPORTDIR+INSTALL_DB_FILE_DELETE+" -o c:\\Errlog.txt -n";


   if bHaveSql then
    SdShowMsg("开始安装数据库,请等待...",TRUE);
    Delay(5);
    szSQLPATH=INSTALLDIR^"****.net\\DataBase\\OSQL.EXE";
    if LaunchAppAndWait(szSQLPATH,szParameter,WAIT)<0 then
      MessageBox("安装数据库失败,请手工安装数据库!",SEVERE);
    endif;     
     SdShowMsg("...",FALSE);
    endif;  

    if LaunchAppAndWait(INSTALLDIR ^ "****.net\\bin\\tools\\****.exe",
    "", NOWAIT) < 0 then
     szMsg = "运行数据库配置工具出错,请手工安装!";
       MessageBox(szMsg, SEVERE);
 endif; 
            
    if LaunchAppAndWait(SUPPORTDIR ^ "iewebcontrols.msi", "", WAIT) < 0 then
     szMsg = "安装浏览器组件出错,请手工安装!";
       MessageBox(szMsg, SEVERE);
 endif;
             
    //修改注册表
 nRootKey = HKEY_CURRENT_USER;
 if (RegDBSetDefaultRoot (nRootKey) < 0) then
     MessageBox("初始化失败", SEVERE);
   endif;
    nType = REGDB_STRING;
  
   ProductVersion = @PRODUCT_VERSION;
  
  RegDBSetKeyValueEx(****NetReg,"DatabaseName",nType,svDBName,nSize);
  RegDBSetKeyValueEx(****NetReg,"InstallPath",nType,INSTALLDIR^"****.net",nSize);
  
  //StrSub(svResult, ProductVersion, 0, StrFind(ProductVersion, "."));
  //StrSub(ProductVersion, ProductVersion, (StrFind(ProductVersion, ".") + 1), 
  (StrLength(ProductVersion) - StrFind(ProductVersion, ".")));
  RegDBSetKeyValueEx(****NetReg,"Ver",nType,ProductVersion,nSize);

   GetSystemInfo (DATE, nResult, svResult);
   svTime = svResult;
   GetSystemInfo (TIME, nResult, svResult);
   svTime = svTime + " " + svResult;
  RegDBSetKeyValueEx(****NetReg,"Date",nType,svTime,nSize); 
           
 Disable(STATUSEX);

    szTitle = "";
    szMsg1 = SdLoadString(IFX_SDFINISH_MSG1);
    szMsg1 = szMsg1 + "\n\n客户端访问请使用 IE 6.0 及以上版本!";
 szMsg2 = "";
    szOption1 = "";
    szOption2 = "";
    bOpt1  = FALSE;
    bOpt2  = FALSE;

    SdFinishEx(szTitle, szMsg1, szMsg2, szOption1, szOption2, bOpt1, bOpt2);
end;

// --- include script file section ---

 


 


为了避免不必要的麻烦,以上代码我特地用****代替了某些关键字。

相关IncludeFiles

posted on 2004-10-12 12:52  守兔待猪  阅读(2201)  评论(8编辑  收藏  举报