VC++ through the API to see the procedure error message method

  • 2020-04-02 02:33:08
  • OfStack

The example of this paper introduced VC++ through the API to view the error message method, you can encounter an error, will display the error message and exit processing, the specific implementation code is as follows:


if((m_hBitMap=(HBITMAP)::LoadImage(NULL,filepath,IMAGE_BITMAP,0,0,LR_DEFAULTSIZE|LR_LOADFROMFILE))==NULL)
{
LPVOID lpMsgBuf;
DWORD dw = ::GetLastError(); 
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | 
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf,
0, NULL );
//Displays the error message and exits the handler
::MessageBox(NULL,(LPCTSTR)lpMsgBuf,TEXT("ERROR"),MB_ICONERROR); 
LocalFree(lpMsgBuf);
return;
}
if((m_hBitMap=(HBITMAP)::LoadImage(NULL,filepath,IMAGE_BITMAP,0,0,LR_DEFAULTSIZE|LR_LOADFROMFILE))==NULL)
{
LPVOID lpMsgBuf;
DWORD dw = ::GetLastError(); 
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | 
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf,
0, NULL );
//Displays an error message and exits the process
::MessageBox(NULL,(LPCTSTR)lpMsgBuf,TEXT("ERROR"),MB_ICONERROR); 
LocalFree(lpMsgBuf);
return;

Interested friends can test the effect in the project development, hope to help you.


Related articles: