从程序调用数据库函数

在从某个程序(例如自定义操作或自动化过程)调用以下任一数据库函数之前,安装程序必须先运行 CostInitialize 操作FileCost 操作CostFinalize 操作

下面是 Windows Installer 中使用的数据库函数列表:

在从程序调用 MsiSetFeatureAttributes 之前,安装程序必须先运行 CostInitialize 操作。 然后,安装程序在 MsiSetFeatureAttributes 之后运行 CostFinalize 操作。

以下示例说明了在程序中使用 MsiGetTargetPath 时函数操作的调用顺序。

#include <windows.h>
#include <Msiquery.h>
#include <tchar.h>
#pragma comment(lib, "msi.lib") 

int main()  
{ 

MSIHANDLE hInstall;
TCHAR *szBuf;
DWORD cch  = 0 ;
 
if(MsiOpenPackage(_T("PathToPackage...."), &hInstall) == ERROR_SUCCESS)
{
    if(MsiDoAction(hInstall, _T("CostInitialize"))==ERROR_SUCCESS  
        && MsiDoAction(hInstall, _T("FileCost"))==ERROR_SUCCESS  
        && MsiDoAction(hInstall, _T("CostFinalize"))==ERROR_SUCCESS)   
    { 
        if(MsiGetTargetPath(hInstall, _T("FolderName"), _T(""),&cch)==ERROR_MORE_DATA)
        { 
            cch++; // add 1 to include null terminator since MsiGetTargetPath does not include it on return 
            szBuf = (TCHAR *) malloc(cch*sizeof(TCHAR));
            if(szBuf)
            {
                if(MsiGetTargetPath(hInstall, _T("FolderName"), szBuf,&cch)==ERROR_SUCCESS)
                {
                    // Add code to use szBuf here
                }
                free(szBuf);
            }
        } 
    } 
    MsiCloseHandle(hInstall);
}

return 0;  
}