VC實現程序自動安裝

 
[cpp]
      /********************************************
        /* 頭文件 */  
  1. #include <Windows.h>   
  2. #include <Setupapi.h>   
  3. #include <shlobj.h>   
  4. /* 庫 */  
  5. #pragma comment (lib, "shell32.lib")   
  6. #pragma comment (lib, "Setupapi.lib")   
  7.   
  8. /************************************* 
  9. * VOID GetSourceDirectory(LPSTR szPath) 
  10. * 功能 獲得當前路徑 
  11. * szPath,返回路徑 
  12. **************************************/  
  13. VOID GetSourceDirectory(LPSTR szPath)  
  14. {  
  15.     int i;  
  16.   
  17.     GetModuleFileName(NULL,szPath,MAX_PATH);  
  18.   
  19.     i=strlen(szPath);  
  20.     while ((i>0)&&(szPath[i-1]!='\\'))  
  21.     {  
  22.         szPath[--i]=0;  
  23.     }  
  24. }  
  25.   
  26. /************************************* 
  27. * WinMain 
  28. * 功能 調用相關Setup API進行安裝 
  29. **************************************/  
  30. INT WinMain(  
  31.             HINSTANCE hInstance,  
  32.             HINSTANCE hPrevInstance,  
  33.             LPSTR lpCmdLine,  
  34.             int nCmdShow  
  35.             )  
  36. {  
  37.     HINF hInf; // INF文件句柄   
  38.     CHAR szSrcPath[MAX_PATH];// 源路徑   
  39.     CHAR szDisPath[MAX_PATH];// 目的路徑   
  40.     BOOL bResult;  
  41.     PVOID pContext;  
  42.     // 與本程序在同一目錄下的Setup.inf   
  43.     GetSourceDirectory(szSrcPath);  
  44.     lstrcat(szSrcPath,"setup.inf");  
  45.     // 打開 inf 文件   
  46.     hInf = SetupOpenInfFile(szSrcPath, NULL, INF_STYLE_WIN4, NULL);  
  47.     // 是否成功   
  48.     if (hInf == INVALID_HANDLE_VALUE)  
  49.     {  
  50.         MessageBox(NULL,  
  51.             "Error: Could not open the INF file.",  
  52.             "ERROR",  
  53.             MB_OK|MB_ICONERROR);  
  54.         return FALSE;  
  55.     }  
  56.     // 獲得Program Files的路徑   
  57.     SHGetSpecialFolderPath(NULL,   
  58.         szDisPath, CSIDL_PROGRAM_FILES , FALSE);  
  59.     // 構造目的路徑   
  60.     lstrcat(szDisPath,"\\MyInstall");  
  61.   
  62.     // 給inf配置文件中的路徑ID賦值,使用路徑替換路徑ID   
  63.     bResult = SetupSetDirectoryId(hInf, 32768, szDisPath);  
  64.     if (!bResult)  
  65.     {  
  66.         MessageBox(NULL,  
  67.             "Error: Could not associate a directory ID with the destination directory.",  
  68.             "ERROR",  
  69.             MB_OK|MB_ICONERROR);  
  70.         SetupCloseInfFile(hInf);  
  71.         return FALSE;  
  72.     }  
  73.   
  74.     // 設置默認callback函數的參數   
  75.     pContext=SetupInitDefaultQueueCallback(NULL);  
  76.   
  77.     // 進行安裝   
  78.     bResult=SetupInstallFromInfSection(  
  79.         NULL, // 父窗口句柄   
  80.         hInf, // INF文件句柄   
  81.         "Install"// INF文件中,配置了安裝信息的節名   
  82.         SPINST_FILES | SPINST_REGISTRY , // 安裝標誌   
  83.         NULL, // 安裝鍵值   
  84.         NULL, // 源文件和路徑,可以在INF文件中配置   
  85.         0, // 複製時的動作   
  86.         (PSP_FILE_CALLBACK)SetupDefaultQueueCallback, // 回調函數   
  87.         pContext, // 回調函數的參數   
  88.         NULL, // 設備信息   
  89.         NULL // 設備信息   
  90.         );  
  91.     // 安裝是否成功   
  92.     if (!bResult)  
  93.     {  
  94.         // 失敗,輸出錯誤信息   
  95.         MessageBox(NULL,  
  96.             "SetupInstallFromInfSection",  
  97.             "ERROR",  
  98.             MB_OK|MB_ICONERROR);  
  99.         //關閉   
  100.         SetupTermDefaultQueueCallback(pContext);  
  101.         SetupCloseInfFile(hInf);  
  102.         return FALSE;  
  103.     }  
  104.   
  105.     // 關閉   
  106.     SetupTermDefaultQueueCallback(pContext);  
  107.     SetupCloseInfFile(hInf);  
  108.   
  109.     return TRUE;  
  110. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章