msiGetLastErrorRecord 函式 (msiquery.h)

MsiGetLastErrorRecord 函式會傳回上次針對呼叫行程傳回的錯誤記錄。 此函式會傳回應該使用 MsiCloseHandle 關閉的句柄。

Syntax

MSIHANDLE MsiGetLastErrorRecord();

傳回值

錯誤記錄的句柄。 如果最後一個函式成功, MsiGetLastErrorRecord 會傳回 Null MSIHANDLE

備註

使用 MsiGetLastErrorRecord 函式 時,記錄的欄位 1 包含安裝程式錯誤碼。 其他欄位包含特定錯誤的特定數據。 執行此函式之後,錯誤記錄會在內部釋放。

如果記錄傳遞至 MsiProcessMessage,則會藉由查閱目前資料庫中的字串來格式化。 如果沒有安裝會話,但產品資料庫已開啟,則格式字串可能會由 Error 數據表 上的查詢使用錯誤碼取得,後面接著呼叫 MsiFormatRecord。 如果已知錯誤碼,可能會個別解譯參數。

下列函式會設定每一進程錯誤記錄,如果未發生任何錯誤,請將它重設為 null。 MsiGetLastErrorRecord 也會在傳回錯誤記錄之後清除錯誤記錄。

請注意,建議您使用 PMSIHANDLE 類型的變數,因為安裝程式會在 PMSIHANDLE 物件超出範圍時關閉 PMSIHANDLE 物件,而您必須呼叫 MsiCloseHandle 來關閉 MSIHANDLE 物件。 如需詳細資訊,請參閱 Windows Installer 最佳做法中的使用 PMSIHANDLE 而非 HANDLE 一節。

下列範例會使用 對 MsiDatabaseOpenView 的呼叫,示範如何從支援 MsiGetLastErrorRecord 的其中一個 Windows Installer 函式取得擴充錯誤資訊。 OpenViewOnDatabase 範例會嘗試在資料庫句柄上開啟檢視。 您可以透過呼叫 MsiOpenDatabase 來取得 hDatabase 句柄。 如果開啟檢視失敗,函式會嘗試使用 MsiGetLastErrorRecord 來取得擴充的錯誤資訊。

#include <windows.h>
#include <Msiquery.h>
#pragma comment(lib, "msi.lib")
//-------------------------------------------------------------------
// Function: OpenViewOnDatabase
//
// Arguments: hDatabase - handle to a MSI package obtained
//                                        via a call to MsiOpenDatabase
//
// Returns: UINT status code. ERROR_SUCCESS for success.
//--------------------------------------------------------------------------------------------------
UINT __stdcall OpenViewOnDatabase(MSIHANDLE hDatabase)
{
    if (!hDatabase)
        return ERROR_INVALID_PARAMETER;

    PMSIHANDLE hView = 0;
    UINT uiReturn = MsiDatabaseOpenView(hDatabase, 
                                TEXT("SELECT * FROM `UnknownTable`"),
                           &hView);

    if (ERROR_SUCCESS != uiReturn)
    {
        // try to obtain extended error information.

        PMSIHANDLE hLastErrorRec = MsiGetLastErrorRecord();

        TCHAR* szExtendedError = NULL;
        DWORD cchExtendedError = 0;
        if (hLastErrorRec)
        {
            // Since we are not currently calling MsiFormatRecord during an
            // install session, hInstall is NULL. If MsiFormatRecord was called
            // via a DLL custom action, the hInstall handle provided to the DLL
            // custom action entry point could be used to further resolve 
            // properties that might be contained within the error record.
            
            // To determine the size of the buffer required for the text,
            // szResultBuf must be provided as an empty string with
            // *pcchResultBuf set to 0.

            UINT uiStatus = MsiFormatRecord(NULL,
                             hLastErrorRec,
                             TEXT(""),
                             &cchExtendedError);

            if (ERROR_MORE_DATA == uiStatus)
            {
                // returned size does not include null terminator.
                cchExtendedError++;

                szExtendedError = new TCHAR[cchExtendedError];
                if (szExtendedError)
                {
                    uiStatus = MsiFormatRecord(NULL,
                                     hLastErrorRec,
                                     szExtendedError,
                                     &cchExtendedError);
                    if (ERROR_SUCCESS == uiStatus)
                    {
                        // We now have an extended error
                        // message to report.

                        // PLACE ADDITIONAL CODE HERE
                        // TO LOG THE ERROR MESSAGE
                        // IN szExtendedError.
                    }

                    delete [] szExtendedError;
                    szExtendedError = NULL;
                }
            }
        }
    }

    return uiReturn;
}

規格需求

需求
最低支援的用戶端 Windows Server 2012、Windows 8、Windows Server 2008 R2 或 Windows 7 上的 Windows Installer 5.0。 Windows Server 2008 或 Windows Vista 上的 Windows Installer 4.0 或 Windows Installer 4.5。 Windows Server 2003 或 Windows XP 上的 Windows Installer
目標平台 Windows
標頭 msiquery.h
程式庫 Msi.lib
Dll Msi.dll

另請參閱

安裝程式狀態存取函式