MsiGetLastErrorRecord function (msiquery.h)

The MsiGetLastErrorRecord function returns the error record that was last returned for the calling process. This function returns a handle that should be closed using MsiCloseHandle.


MSIHANDLE MsiGetLastErrorRecord();

Return value

A handle to the error record. If the last function was successful, MsiGetLastErrorRecord returns a null MSIHANDLE.


With the MsiGetLastErrorRecord function, field 1 of the record contains the installer error code. Other fields contain data specific to the particular error. The error record is released internally after this function is run.

If the record is passed to MsiProcessMessage, it is formatted by looking up the string in the current database. If there is no installation session but a product database is open, the format string may be obtained by a query on the Error table using the error code, followed by a call to MsiFormatRecord. If the error code is known, the parameters may be individually interpreted.

The following functions set the per-process error record or reset it to null if no error occurred. MsiGetLastErrorRecord also clears the error record after returning it.

Note that it is recommended to use variables of type PMSIHANDLE because the installer closes PMSIHANDLE objects as they go out of scope, whereas you must close MSIHANDLE objects by calling MsiCloseHandle. For more information see Use PMSIHANDLE instead of HANDLE section in the Windows Installer Best Practices.

The following sample uses a call to MsiDatabaseOpenView to show how to obtain extended error information from one of the Windows Installer functions that supports MsiGetLastErrorRecord. The example, OpenViewOnDatabase, attempts to open a view on a database handle. The hDatabase handle can be obtained by a call to MsiOpenDatabase. If opening the view fails, the function then tries to obtain extended error information by using 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)

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

    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,

            if (ERROR_MORE_DATA == uiStatus)
                // returned size does not include null terminator.

                szExtendedError = new TCHAR[cchExtendedError];
                if (szExtendedError)
                    uiStatus = MsiFormatRecord(NULL,
                    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;


Minimum supported client Windows Installer 5.0 on Windows Server 2012, Windows 8, Windows Server 2008 R2 or Windows 7. Windows Installer 4.0 or Windows Installer 4.5 on Windows Server 2008 or Windows Vista. Windows Installer on Windows Server 2003 or Windows XP
Target Platform Windows
Header msiquery.h
Library Msi.lib
DLL Msi.dll

See also

Installer State Access Functions