FormatMessage


Click here to change the theme.

Most Windows Platform SDK functions set error codes that GetLastError() will return, and the FormatMessage function can retrieve text for the error code.

void GetErrorMessage(const DWORD e, CString &Message) {
	LPTSTR lpMsgBuf=NULL;
	DWORD Size, fme;
Size = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
		NULL, e, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
		(LPTSTR)&lpMsgBuf, 0, NULL);
fme = GetLastError();
if (lpMsgBuf) {
	Message = lpMsgBuf;
	LocalFree(lpMsgBuf);
	if (Size)
		return;
	}
if (fme)
	Message.Format("FormatMessage error code %u", fme);
else
	Message.Format("unknown error code %u", e);
}