none
how to use messagebox to show GetLastError ?

    Question

  • TCHAR cur_dir[MAX_PATH];
       DWORD success;
       success = GetCurrentDirectory(MAX_PATH,cur_dir ); // -->cannot compile at multi-thread
       if (success == 0 ) 
       {  
           
        HWND handle3 ;
        
        MessageBox(handle3, "GetCurrentDirectory! error code = ", GetLastError(),"GetCurrentDirectory error" , MB_OK|MB_ICONWARNING  ); 
           return;  
       } 

    i get an error of
    error C2660: 'MessageBoxW' : function does not take 5 arguments
    at this line :    MessageBox(handle3, "GetCurrentDirectory! error code = ", GetLastError(),"GetCurrentDirectory error" , MB_OK|MB_ICONWARNING  ); 

    how to amend it  ?

    do the error message is just error code (numbers) or error message ?

    btw, i am a beginner~ ~
    • Edited by chiwing15 Tuesday, June 16, 2009 11:35 AM
    Tuesday, June 16, 2009 11:29 AM

Answers

  • Hello Chiwing,

    1. At first you have to convert GetLastError() to string by using FormatMessage() .
    2. Then display it by calling MessageBox() properly.
    3. Have a look at following code snippet.
    // Get last error.
    DWORD err = GetLastError();
    
    // Translate ErrorCode to String.
    LPTSTR Error = 0;
    if(::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
                        NULL,
                        err,
                        0,
                        (LPTSTR)&Error,
                        0,
                        NULL) == 0)
    {
       // Failed in translating.
    }
    
    // Display message.
    MessageBox( Error, _T("GetCurrentDirectory Error"), MB_OK|MB_ICONWARNING );
    
    // Free the buffer.
    if( Error )
    {
       ::LocalFree( Error );
       Error = 0;
    }
    

    PS: If you just want to watch the error, then add @err,hr to debugger watch window.

    Regards,
    Jijo.
    http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
    Tuesday, June 16, 2009 11:43 AM
  • OH, I FIND OUT THE ANSWER MYSELF

    LPCTSTR , ths c is represent const

    LPCTSTR is a 32-bit pointer, which point to a const string,

    each char may use 1 /2 Bytes, it depends on use unicode or not~ ~ 
    Tuesday, June 16, 2009 12:46 PM

All replies


  • please use simple code , complicated code will make me puzzle
    thanks
    Tuesday, June 16, 2009 11:41 AM
  • Hello Chiwing,

    1. At first you have to convert GetLastError() to string by using FormatMessage() .
    2. Then display it by calling MessageBox() properly.
    3. Have a look at following code snippet.
    // Get last error.
    DWORD err = GetLastError();
    
    // Translate ErrorCode to String.
    LPTSTR Error = 0;
    if(::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
                        NULL,
                        err,
                        0,
                        (LPTSTR)&Error,
                        0,
                        NULL) == 0)
    {
       // Failed in translating.
    }
    
    // Display message.
    MessageBox( Error, _T("GetCurrentDirectory Error"), MB_OK|MB_ICONWARNING );
    
    // Free the buffer.
    if( Error )
    {
       ::LocalFree( Error );
       Error = 0;
    }
    

    PS: If you just want to watch the error, then add @err,hr to debugger watch window.

    Regards,
    Jijo.
    http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
    Tuesday, June 16, 2009 11:43 AM
  • BIG BIG THANKS To Jijo Raj~ ~

    i want to ask MessageBox( Error, _T("GetCurrentDirectory Error"
    ), MB_OK|MB_ICONWARNING );

    what is _T("GetCurrentDirectory Error")

    _T doing?

    thanks
    Tuesday, June 16, 2009 12:01 PM
  • _T is a preprocessor directive that allows the same code to be used on wide and narrow strings
    Tuesday, June 16, 2009 12:23 PM
  • O, learn it

    one more thing that i want to know is

    LPTSTR Error = 0;  --> is this ok

    i search the api manual :
    the messagebox syntax is
    int MessageBox(
        HWND hWnd, // handle of owner window
        LPCTSTR lpText, // address of text in message box
        LPCTSTR lpCaption, // address of title of message box 
        UINT uType  // style of message box
       );

    do i need to change LPTSTR Error = 0; 
    to
    LPCTSTR Error = 0;



    • Edited by chiwing15 Tuesday, June 23, 2009 9:58 AM
    Tuesday, June 16, 2009 12:28 PM
  • OH, I FIND OUT THE ANSWER MYSELF

    LPCTSTR , ths c is represent const

    LPCTSTR is a 32-bit pointer, which point to a const string,

    each char may use 1 /2 Bytes, it depends on use unicode or not~ ~ 
    Tuesday, June 16, 2009 12:46 PM
  • how ever i get below compile error:

    MessageBox(Error_message, L"access error", MB_OK|MB_ICONWARNING  ); // 'MessageBoxW' : function does not take 3 arguments


    when i add HWND mbox ;

    it say 
      error c4700: uninitialized local variable "mbox" used;

    then when i add HWND mbox = 0 ; // the problem solved
    Tuesday, June 23, 2009 10:19 AM