Page 1 of 1

Checking GetLastError() from C

Posted: Sun Feb 06, 2022 8:42 am
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 :-)