+ Reply to Thread
Results 1 to 2 of 2

Thread: What is FormatMessage? How to use it?

  1. #1

    What is FormatMessage? How to use it?

    What is FormatMessage? How to use it?

  2. #2
    Sometimes it may happen that you will need to get the error message text corresponding to the last error code returned by GetLastError() API.

    In that case, FormatMessage can be useful.

    Following is the example:

    Code:
    CString GetExtendedErrorMessage(DWORD dwLastError)
    {
        LPVOID lpvBuffer ;
    
        FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
    					NULL, dwLastError, 
    					MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
    					(LPTSTR)&lpvBuffer, 0, NULL) ;
    
        CString strExtendedError = (LPTSTR) lpvBuffer ;
    	//
        // Free the buffer allocated by the system
        //
        LocalFree(lpvBuffer) ;
    
    	return strExtendedError ;
    }
    
    CString strExtendedErrorMessage = GetExtendedErrorMessage(GetLastError()) ;

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts