CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2009
    Posts
    31

    GetLastError() in a MessageBox()?

    Hi,
    can you tell me how to display the GetLastError() in a MessageBox()?
    Thanks!

  2. #2
    Join Date
    Aug 2008
    Location
    India
    Posts
    186

    Re: GetLastError() in a MessageBox()?

    Use FormatMessage() function to get error code string...

    Thanks,

  3. #3
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: GetLastError() in a MessageBox()?

    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  4. #4
    Join Date
    Sep 2005
    Location
    London
    Posts
    208

    Re: GetLastError() in a MessageBox()?

    GetLastError() return the calling thread's last-error code.
    Error codes are 32-bit values so to display the error code you will have to construct a string representation of the error code, something like:

    Code:
    std::ostringstream ss;
    DWORD dwLastError = ::GetLastError();
    ss << dwLastError ;
    
    YourMessageBox( ss.str(), ss.str().c_str() );
    As indicated, You can obtain error message strings for system error codes using FormatMessage(), something like:

    Code:
    int main()
    {
        void* lpBuffer;
    
        FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
    	               NULL,
    	              ::GetLastError(),
    	              MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
    	              (LPTSTR) &lpBuffer,
    	              0,
    	              NULL );
    
        MessageBox( NULL, (LPCTSTR)lpBuffer, __T("LastRrror"), MB_OK );
        LocalFree( lpBuffer );
    
        return 0;
    }


    Regards
    Doron Moraz
    Last edited by Doron Moraz; June 12th, 2009 at 01:56 PM.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured