Getlasterror () and formatmessage ()

Source: Internet
Author: User

Errors may inevitably occur during Win32 programming. The biggest headache is that you don't know where the error is and why it is wrong. Microsoft also thinks about this. Therefore, it provides a good function getlasterror (), this function returns the last error code.

For example, the following code:

# Include <windows. h>

Lresult callback wndproc (hwnd, uint message, wparam, lparam)
{
   Return defwindowproc (hwnd, message, wparam, lparam );
}

Bool initapp (hinstance, wndclass * wndclass)
{
   Wndclass-> cbclsextra = 0;// No additional window memory
   Wndclass-> cbwndextra = 0;// No additional window memory
   Wndclass-> hbrbackground = (hbrush) getstockobject (white_brush );
   Wndclass-> hcursor = loadcursor (null, idc_arrow );
   Wndclass-> hicon = loadicon (null, idi_application );
   Wndclass-> hinstance = hinstance;
   Wndclass-> lpfnwndproc = wndproc;
   Wndclass-> lpszclassname = text ("Newstart ");
   Wndclass-> lpszmenuname = NULL;
   Wndclass-> style = ws_hscroll | cs_hredraw;    // The error should be intentionally written but compiled

   Return registerclass (wndclass );
}

Int winapi winmain (hinstance, hinstance hprevinstance, lpstr lpcmdline, int nshowcmd)
{
   Char Buf [500];
   Bool Fok;
   Hwnd;
   DWORD dwerr, systemlocale;
   Hlocal = NULL;
   MSG;
   Wndclass;
   Int flag;
   If (initapp (hinstance, & wndclass) = NULL)
   {  
       Dwerr = getlasterror ();
       Wsprintf (BUF, l "% d", dwerr );
       MessageBox (null, Buf, text ("Error Report"), mb_ OK );
       Return 0;
   }
   Hwnd = createwindow (text ("Newstart"), text ("Hello! "), Ws_sysmenu | ws_visible, cw_usedefault, cw_usedefault, 400,300, null, null, hinstance, null );
   Showwindow (hwnd, nshowcmd );
   Updatewindow (hwnd );
   While (flag = getmessage (& MSG, hwnd, 0, 0 ))! = 0 & flag! =-1)
   {
       Translatemessage (& MSG );
       Dispatchmessage (& MSG );
   }
   Return msg. wparam;
}

Getlasterror () will return an error code 87, and you can use msdn to find this error number, which means that the parameter is incorrect, if you debug this code and check $ err or dwerr, you can get this error number. If you add these variables, HR can get the corresponding error explanation of 87.

When you want the program to display an error explanation during execution, you need to use another function formatmessage (). The Code is as follows:

Int winapi winmain (hinstance, hinstance hprevinstance, lpstr lpcmdline, int nshowcmd)
{
   Char Buf [500];
   Bool Fok;
   Hwnd;
   DWORD dwerr, systemlocale;
   Hlocal = NULL;
   MSG;
   Wndclass;
   Int flag;
   If (initapp (hinstance, & wndclass) = NULL)
   {  
       Dwerr = getlasterror ();
       Systemlocale = makelangid (lang_neutral, sublang_neutral );
       Fok = formatmessage (format_message_from_system | format_message_ignore_inserts | format_message_allocate_buffer,
                           Null, dwerr, systemlocale, (lpwstr) & hlocal, 0, null );
       MessageBox (null, (lpcwstr) locallock (hlocal), text ("hello"), mb_ OK );
       Return 0;
   }
   Hwnd = createwindow (text ("Newstart"), text ("Hello! "), Ws_sysmenu | ws_visible, cw_usedefault, cw_usedefault, 400,300, null, null, hinstance, null );
   Showwindow (hwnd, nshowcmd );
   Updatewindow (hwnd );
   While (flag = getmessage (& MSG, hwnd, 0, 0 ))! = 0 & flag! =-1)
   {
       Translatemessage (& MSG );
       Dispatchmessage (& MSG );
   }
   Return msg. wparam;
}

Here are some explanations:

Hlocal is a handle pointing to the memory block, initialized to 0;

Makelangid is interpreted on msdn as this macro creates a language Identifier from a primary language identifier and a sub-language identifier.

You can use this function to create a language identifier, which has two parameters: the first language identifier and the sub-language identifier. Here we use the lang_neutral, the value of sublang_neutral union is equal to 0, which is the default language of the operating system.

Format_message_from_system: This macro tells formatmessage that we want to obtain a string corresponding to a system-defined error code.

Format_message_ignore_inserts: This macro allows us to get messages containing % placeholders.

Format_message_allocate_buffer: This macro tells the function to allocate enough memory to accommodate the error text description.

Formatmessage stores the error message in hlocal.

Formatmessage this function is actually very powerful, other specific usage recommendations see msdn: http://msdn.microsoft.com/en-us/library/ms679351 (vs.85). aspx

Locallock () The description on msdn is locks a local memory object and returns a pointer to the first byte of the object's memory block.

That is to say, this function can be used to lock a memory object and return the first address of the memory block of this object.

# Include <windows. h> // using namespace STD; lresult callback wndproc (hwnd, uint message, wparam, lparam) {return defwindowproc (hwnd, message, wparam, lparam );} bool initapp (hinstance, wndclass * wndclass) {wndclass-> cbclsextra = 0; // No additional window class memory wndclass-> cbwndextra = 0; // No additional window class memory wndclass-> hbrbackground = (hbrush) getstockobject (white_brush); wndclass-> hcursor = loadcursor (null, idc_arr OW); wndclass-> hicon = loadicon (null, idi_application); wndclass-> hinstance = hinstance; wndclass-> lpfnwndproc = wndproc; wndclass-> lpszclassname = text ("Newstart"); wndclass-> lpszmenuname = NULL; wndclass-> style = ws_hscroll | cs_hredraw; return registerclass (wndclass );} int winapi winmain (hinstance, hinstance hprevinstance, lpstr lpcmdline, int nshowcmd) {tchar Buf [80]; bool Fok; hwnd; DWORD dwer R, languageid; hlocal = NULL; MSG; wndclass; int flag; If (null = initapp (hinstance, & wndclass) {dwerr = getlasterror (); /* wsprintf (BUF, l "% d", dwerr); MessageBox (null, Buf, text ("Error Report"), mb_ OK); */languageid = makelangid (lang_neutral, sublang_neutral); // default receivagefok = formatmessage (format_message_allocate_buffer | format_message_from_system | format_message_ignore_inserts, null, Dwerr, languageid, (lpwstr) & hlocal, 0, null); // MessageBox (null, (lpcwstr) locallock (hlocal), text ("hello"), mb_ OK ); wsprintf (BUF, l "error info. (error code number = % d): % s ", dwerr, locallock (hlocal); localfree (hlocal ); // remember to use localfree to release the memory that hlocal points to when it is no longer in use. Otherwise, memory leakage will occur! MessageBox (null, Buf, text ("Error Report"), mb_ OK); Return (0);} hwnd = createwindow (text ("Newstart "), text ("hello"), ws_sysmenu | ws_visible, cw_usedefault, null, null, hinstance, null); showwindow (hwnd, sw_shownormal); updatewindow (hwnd ); while (0! = (Flag = getmessage (& MSG, null) & flag! =-1) {translatemessage (& MSG); dispatchmessage (& MSG);} return msg. wparam ;}

Output: Error info. (error code number = 87): The parameter has an error.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.