- #include <Windows.h>
- #include <Setupapi.h>
- #include <shlobj.h>
- /* 庫 */
- #pragma comment (lib, "shell32.lib")
- #pragma comment (lib, "Setupapi.lib")
- /*************************************
- * VOID GetSourceDirectory(LPSTR szPath)
- * 功能 獲得當前路徑
- * szPath,返回路徑
- **************************************/
- VOID GetSourceDirectory(LPSTR szPath)
- {
- int i;
- GetModuleFileName(NULL,szPath,MAX_PATH);
- i=strlen(szPath);
- while ((i>0)&&(szPath[i-1]!='\\'))
- {
- szPath[--i]=0;
- }
- }
- /*************************************
- * WinMain
- * 功能 調用相關Setup API進行安裝
- **************************************/
- INT WinMain(
- HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPSTR lpCmdLine,
- int nCmdShow
- )
- {
- HINF hInf; // INF文件句柄
- CHAR szSrcPath[MAX_PATH];// 源路徑
- CHAR szDisPath[MAX_PATH];// 目的路徑
- BOOL bResult;
- PVOID pContext;
- // 與本程序在同一目錄下的Setup.inf
- GetSourceDirectory(szSrcPath);
- lstrcat(szSrcPath,"setup.inf");
- // 打開 inf 文件
- hInf = SetupOpenInfFile(szSrcPath, NULL, INF_STYLE_WIN4, NULL);
- // 是否成功
- if (hInf == INVALID_HANDLE_VALUE)
- {
- MessageBox(NULL,
- "Error: Could not open the INF file.",
- "ERROR",
- MB_OK|MB_ICONERROR);
- return FALSE;
- }
- // 獲得Program Files的路徑
- SHGetSpecialFolderPath(NULL,
- szDisPath, CSIDL_PROGRAM_FILES , FALSE);
- // 構造目的路徑
- lstrcat(szDisPath,"\\MyInstall");
- // 給inf配置文件中的路徑ID賦值,使用路徑替換路徑ID
- bResult = SetupSetDirectoryId(hInf, 32768, szDisPath);
- if (!bResult)
- {
- MessageBox(NULL,
- "Error: Could not associate a directory ID with the destination directory.",
- "ERROR",
- MB_OK|MB_ICONERROR);
- SetupCloseInfFile(hInf);
- return FALSE;
- }
- // 設置默認callback函數的參數
- pContext=SetupInitDefaultQueueCallback(NULL);
- // 進行安裝
- bResult=SetupInstallFromInfSection(
- NULL, // 父窗口句柄
- hInf, // INF文件句柄
- "Install", // INF文件中,配置了安裝信息的節名
- SPINST_FILES | SPINST_REGISTRY , // 安裝標誌
- NULL, // 安裝鍵值
- NULL, // 源文件和路徑,可以在INF文件中配置
- 0, // 複製時的動作
- (PSP_FILE_CALLBACK)SetupDefaultQueueCallback, // 回調函數
- pContext, // 回調函數的參數
- NULL, // 設備信息
- NULL // 設備信息
- );
- // 安裝是否成功
- if (!bResult)
- {
- // 失敗,輸出錯誤信息
- MessageBox(NULL,
- "SetupInstallFromInfSection",
- "ERROR",
- MB_OK|MB_ICONERROR);
- //關閉
- SetupTermDefaultQueueCallback(pContext);
- SetupCloseInfFile(hInf);
- return FALSE;
- }
- // 關閉
- SetupTermDefaultQueueCallback(pContext);
- SetupCloseInfFile(hInf);
- return TRUE;
- }
VC實現程序自動安裝
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.