《windows核心编程》读书笔记1-chapter1
//WinErrCodeformater.h
/************************************************************************/
/*CWinErrCodeFormatter,用于将错误码翻译成文本,可以指定文本的语言 */
/*这是一个functor
/************************************************************************/
class CWinErrCodeFormatter
{
public:
CWinErrCodeFormatter(long errorCode=-1, LCID lang=MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED));
public:
virtual ~CWinErrCodeFormatter(void);
public:
CString operator()();
CString operator()(long errorCode, LCID lang=MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED));
private:
///<summary>
///字符串的语言
///</summary>
LCID m_lang;
///<summary>
///错误码
///</summary>
long m_errorCode;
};
// WinErrCodeformater.cpp
#include "StdAfx.h"
#include "WinErrCodeFormatter.h"
CWinErrCodeFormatter::CWinErrCodeFormatter(long errorCode, LCID lang):m_errorCode(errorCode),m_lang(lang)
{
}
CWinErrCodeFormatter::~CWinErrCodeFormatter(void)
{
}
CString CWinErrCodeFormatter::operator()()
{
HLOCAL hlocal = NULL;
BOOL bOK = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM| FORMAT_MESSAGE_ALLOCATE_BUFFER,
NULL, m_errorCode, m_lang, (PTSTR)&hlocal, 0, NULL);
if(!bOK)
{
HMODULE hDll = LoadLibraryEx(_T("netmsg.dll"), NULL, DONT_RESOLVE_DLL_REFERENCES);
if(hDll)
{
FormatMessage(FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_FROM_SYSTEM, hDll, m_errorCode, m_lang, (PTSTR)&hlocal, 0, NULL);
FreeLibrary(hDll);
}
}
if(hlocal)
{
CString result((PCTSTR)LocalLock(hlocal));
LocalFree(hlocal);
return result;
}
else
return _T("get error message failed!");
}
CString CWinErrCodeFormatter::operator()(long errorCode, LCID lang)
{
m_errorCode = errorCode;
m_lang = lang;
return operator();
}