프로그래밍

FormatMessage (Win32)

디비노 2012. 12. 5. 23:15

GetLastError() 함수로 얻은 에러 코드를 에러 문자열로 변환해 주는 함수다.

 std::tstring GetErrorMessage(int ec)
{
	LPVOID lpMsgBuf = 0;
	DWORD retval = ::FormatMessage( 
		FORMAT_MESSAGE_ALLOCATE_BUFFER | 
		FORMAT_MESSAGE_FROM_SYSTEM | 
		FORMAT_MESSAGE_IGNORE_INSERTS,
		NULL,
		ec,
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
		(LPTSTR) &lpMsgBuf,
		0,
		NULL 
		);

	if (retval == 0)
		return std::string(_T("Unknown error"));

	return std::tstring(static_cast<LPCTSTR>(lpMsgBuf));
}

이런 함수를 만들어 놓으면 편하다.