VC++ method to create msi files

  • 2020-04-02 02:26:31
  • OfStack

VC++ can be used to write their own software installation procedures. Here is just to create an installer type of msi file, open with orca is the correct file format, it is worth recording their own, msi database in the various table relations complex, not a half an hour to be able to study clearly. This article is just a brief analysis, the implementation of writing a program attached to the software program behind, so that after the compilation is completed directly will have the installation program msi file. Just like any software you download, you can write a registry, create a desktop shortcut, and register the components and functions that various software USES.

The specific sample program is as follows:


#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>

This procedure only realizes the simple basic function, the reader may further develop the other personalized function according to own need, in order to satisfy own demand.


Related articles: