從程式呼叫資料庫函式

從程式呼叫下列任何 Database Functions 之前,例如自訂動作或自動化程式,安裝程式必須先執行 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;  
}