VC++创建msi文件的方法
采用VC++可以编写自己软件的安装程序。这里只是创建安装程序类型的msi文件,用orca打开是正确的文件格式,值得自己记录一下了,msi数据库里面的各种表关系复杂,不是一时半刻能研究清楚的。本文仅作一浅析,实现写一个程序附到软件程序的后面,这样可以在编译完成后直接会有安装程序msi文件。就像平常下载的软件,可以写注册表,创建桌面快捷方式,注册各种软件用到的组件和功能。
具体示例程序如下:
#pragmaonce
//CRTheaders.
#include<TCHAR.H>
//windowsplatformheaders.
#include<WINDOWS.H>
//msiheaders.
#pragmacomment(lib,"msi.lib")
#include<MSI.H>
#include<MSIQUERY.H>
INTAPIENTRY_tWinMain(
HINSTANCE,
HINSTANCE,
LPTSTR,
INT)
{
MSIHANDLEmsiHandle=NULL;
//createmsidatabase.
UINTopenResult=MsiOpenDatabase(
_T("Setup.msi"),
MSIDBOPEN_CREATEDIRECT,
&msiHandle);
//createmsildatabasefailed.
if(openResult!=ERROR_SUCCESS)
{
LPVOIDformatMsg=NULL;
MSIHANDLEerrorCode=MsiGetLastErrorRecord();
//formaterrorcodetostring.
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
errorCode,
MAKELANGID(LANG_NEUTRAL,SUBLANG_SYS_DEFAULT),
(LPTSTR)&formatMsg,
0,
NULL);
//outputerrormessage.
MessageBoxEx(
NULL,
(LPTSTR)formatMsg,
_T("tipwindow"),
MB_OK,
MAKELANGID(LANG_NEUTRAL,SUBLANG_SYS_DEFAULT));
//freemessagebuffer.
LocalFree(formatMsg);
formatMsg=NULL;
return-1;
}
//commitmsidatabase.
UINTcommitResult=MsiDatabaseCommit(msiHandle);
if(commitResult!=ERROR_SUCCESS)
{
LPVOIDformatMsg=NULL;
MSIHANDLEerrorCode=MsiGetLastErrorRecord();
//formaterrorcodetostring.
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
errorCode,
MAKELANGID(LANG_NEUTRAL,SUBLANG_SYS_DEFAULT),
(LPTSTR)&formatMsg,
0,
NULL);
//outputerrormessage.
MessageBoxEx(
NULL,
(LPTSTR)formatMsg,
_T("tipwindow"),
MB_OK,
MAKELANGID(LANG_NEUTRAL,SUBLANG_SYS_DEFAULT));
//freemessagebuffer.
LocalFree(formatMsg);
formatMsg=NULL;
return-1;
}
//closemsidatabasehandle.
UINTcloseResult=MsiCloseHandle(msiHandle);
if(closeResult!=ERROR_SUCCESS)
{
LPVOIDformatMsg=NULL;
MSIHANDLEerrorCode=MsiGetLastErrorRecord();
//formaterrorcodetostring.
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
errorCode,
MAKELANGID(LANG_NEUTRAL,SUBLANG_SYS_DEFAULT),
(LPTSTR)&formatMsg,
0,
NULL);
//outputerrormessage.
MessageBoxEx(
NULL,
(LPTSTR)formatMsg,
_T("tipwindow"),
MB_OK,
MAKELANGID(LANG_NEUTRAL,SUBLANG_SYS_DEFAULT));
//freemessagebuffer.
LocalFree(formatMsg);
formatMsg=NULL;
return-1;
}
return0;
}
</SPAN>
本程序仅实现简单的基本功能,读者可根据自身的需要进一步开发其他个性化功能,以满足自身需求。