這裡只是創建安裝程序類型的文件,當然創建出來也是不能用的。找了好多天資料,看了好多天英語,終於能創建出msi文件了,用orca打開是正確的文件格式,值得自己記錄一下了,msi數據庫裡面的各種表關系復雜,不是一時半刻能研究清楚的,現在先記錄一個開頭吧。希望研究過msi c++ 編程的大神們給點指導。想實現的目標是寫一個程序附到軟件程序後面,這樣可以在編譯完成後直接會有安裝程序msi文件。就像平常下載的軟件,可以寫注冊表,創建桌面快捷方式,注冊各種軟件用到的組件和功能。就簡單的hello world 程序也是麻雀雖小,五髒俱全就顯得專業了。
#pragma once //CRT headers. #include//windows platform headers. #include //msi headers. #pragma comment(lib,"msi.lib") #include #include 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; }