Study/C++

winapi의 오류를 문자열로 출력하기.

붕대마음 2010. 3. 13. 20:59
반응형
win api의 실패시 오류값은 dword.
내부로 들어가보면 알수없는 숫자들이 많다.
이를 문자열로 전화해서 출력하기 위해 GetLastError으로 에러 숫자코드를 받아
FormatMessage를 사용하여 바꾸어 준다.

#include < window.h >

void PrintError(DWORD dwErrorNo);
...

void PrintError(DWORD dwErrorNo)
{
  LPVOID lpMsgBuf;
  if (!FormatMessage(
    FORMAT_MESSAGE_ALLOCATE_BUFFER |
    FORMAT_MESSAGE_FROM_SYSTEM |
    FORMAT_MESSAGE_IGNORE_INSERTS,
    NULL,
    dwErrorNo,
    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),// Default language
    (LPTSTR) &lpMsgBuf,
    0,
    NULL ))
  {
     // Handle the error.
     return;
  }

  
  // Free the buffer.
  LocalFree( lpMsgBuf );
}