采用VC++可以編寫自己軟件的安裝程序。這里只是創建安裝程序類型的msi文件,用orca打開是正確的文件格式,值得自己記錄一下了,msi數據庫里面的各種表關系復雜,不是一時半刻能研究清楚的。本文僅作一淺析,實現寫一個程序附到軟件程序的后面,這樣可以在編譯完成后直接會有安裝程序msi文件。就像平常下載的軟件,可以寫注冊表,創建桌面快捷方式,注冊各種軟件用到的組件和功能。
具體示例程序如下:
#pragma once//CRT headers.#include <TCHAR.H>//windows platform headers.#include <WINDOWS.H>//msi headers.#pragma comment(lib,"msi.lib")#include <MSI.H>#include <MSIQUERY.H>INT APIENTRY _tWinMain(HINSTANCE,HINSTANCE,LPTSTR,INT){MSIHANDLE msiHandle=NULL;//create msi database.UINT openResult=MsiOpenDatabase(_T("Setup.msi"),MSIDBOPEN_CREATEDIRECT,&msiHandle);//create msil database failed.if(openResult != ERROR_SUCCESS){LPVOID formatMsg=NULL;MSIHANDLE errorCode=MsiGetLastErrorRecord();//format error code to string.FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,NULL,errorCode,MAKELANGID(LANG_NEUTRAL,SUBLANG_SYS_DEFAULT),(LPTSTR)&formatMsg,0,NULL);//output error message.MessageBoxEx(NULL,(LPTSTR)formatMsg,_T("tip window"),MB_OK,MAKELANGID(LANG_NEUTRAL,SUBLANG_SYS_DEFAULT));//free message buffer.LocalFree(formatMsg);formatMsg=NULL;return -1;}//commit msi database.UINT commitResult=MsiDatabaseCommit(msiHandle);if(commitResult != ERROR_SUCCESS){LPVOID formatMsg=NULL;MSIHANDLE errorCode=MsiGetLastErrorRecord();//format error code to string.FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,NULL,errorCode,MAKELANGID(LANG_NEUTRAL,SUBLANG_SYS_DEFAULT),(LPTSTR)&formatMsg,0,NULL);//output error message.MessageBoxEx(NULL,(LPTSTR)formatMsg,_T("tip window"),MB_OK,MAKELANGID(LANG_NEUTRAL,SUBLANG_SYS_DEFAULT));//free message buffer.LocalFree(formatMsg);formatMsg=NULL;return -1;}//close msi database handle.UINT closeResult=MsiCloseHandle(msiHandle);if(closeResult != ERROR_SUCCESS){LPVOID formatMsg=NULL;MSIHANDLE errorCode=MsiGetLastErrorRecord();//format error code to string.FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,NULL,errorCode,MAKELANGID(LANG_NEUTRAL,SUBLANG_SYS_DEFAULT),(LPTSTR)&formatMsg,0,NULL);//output error message.MessageBoxEx(NULL,(LPTSTR)formatMsg,_T("tip window"),MB_OK,MAKELANGID(LANG_NEUTRAL,SUBLANG_SYS_DEFAULT));//free message buffer.LocalFree(formatMsg);formatMsg=NULL;return -1;}return 0;}</SPAN>
本程序僅實現簡單的基本功能,讀者可根據自身的需要進一步開發其他個性化功能,以滿足自身需求。
新聞熱點
疑難解答
圖片精選