Checking GetLastError() from C

Post Reply
User avatar
Antonio Linares
Site Admin
Posts: 42599
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 41 times
Been thanked: 88 times
Contact:

Checking GetLastError() from C

Post by Antonio Linares »

Sometimes we have the need to check the value that returns Windows GetLastError(), so this code is required:

Code: Select all | Expand

void ShowErrorTxt( DWORD dwError )
{
   LPVOID lpMsgBuf;
   char buffer[ 6 ];

   FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
                  NULL,
                  dwError,
                  MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language
                  ( LPTSTR ) &lpMsgBuf,
                  0,
                  NULL );
   sprintf_s( buffer, "%i", dwError );
   MessageBox( 0, ( char * ) lpMsgBuf, buffer, 0 );              
   LocalFree( lpMsgBuf );
}


I post it here so it is usefull for all and I can find it next time :-)
regards, saludos

Antonio Linares
www.fivetechsoft.com
Post Reply