MsiGetLastErrorRecord Function

Windows Installer

MsiGetLastErrorRecord Function

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.

Syntax

C++MSIHANDLE MsiGetLastErrorRecord(void);

Parameters

This function has no parameters.

Return Value

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

Remarks

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)
		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;
}

Requirements

VersionWindows Installer 5.0 on 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, Windows XP, and Windows 2000
HeaderMsiquery.h
LibraryMsi.lib
DLLMsi.dll

See Also

Installer State Access Functions

Send comments about this topic to Microsoft

Build date: 8/13/2009

© 2009 Microsoft Corporation. All rights reserved.