windows错误码返回文本信息的封装类

写个实用的小类,用于输出win的错误码文本信息,因为直接使用FormatMessage确实会繁琐一点。所以将类设计成
函数对象的形式。默认以英文、方式输出,省去了自己设定语言类别的麻烦。当然自己可以设定语言类别(参看
MSDN)。

/// 声明
  1. /**
  2. * @author   roofalison
  3. * @date     2008-09-24-14:03
  4. * @class        windows错误码显示类
  5. */
  6. #ifndef WIN_ERR_MESSAGE_H
  7. #define WIN_ERR_MESSAGE_H
  8. #include <windows.h>
  9. #include <string>
  10. using std::string;
  11. class WinErrMessage
  12. {
  13. public :
  14.     WinErrMessage(
  15.         DWORD dwErrCode = 0, 
  16.         DWORD dwLanguageID = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US) );
  17.     ~WinErrMessage();
  18.     void setErrCode(DWORD dwErrCode);
  19.     void setLangID(DWORD dwLangID);
  20.     operator string();
  21.     
  22. private :
  23.     DWORD   m_dwErrCode;
  24.     DWORD   m_dwLanguageID;
  25.     bool    m_bChanged;
  26.     string  m_strErrMessage;
  27. };
  28. #endif
/// 实现

  1. /**
  2. * @author   roofalison
  3. * @date     2008-09-24-14:03
  4. */
  5. #include "WinErrMessage.h"
  6. WinErrMessage::WinErrMessage(DWORD dwErrCode, DWORD dwLanguageID)
  7.     : m_dwErrCode(dwErrCode), m_dwLanguageID(dwLanguageID), m_bChanged(true)
  8. {
  9. }
  10. WinErrMessage::~WinErrMessage()
  11. {
  12. }
  13. void WinErrMessage::setErrCode(DWORD dwErrCode)
  14. {
  15.     if (m_dwErrCode != dwErrCode)
  16.     {
  17.         m_bChanged = true;
  18.         m_dwErrCode = dwErrCode;
  19.     }
  20. }
  21. void WinErrMessage::setLangID(DWORD dwLanguageID)
  22. {
  23.     if (m_dwLanguageID != dwLanguageID)
  24.     {
  25.         m_bChanged = true;
  26.         m_dwLanguageID = dwLanguageID;
  27.     }
  28. }
  29. WinErrMessage::operator string()
  30. {
  31.     if (m_bChanged)
  32.     {
  33.         HLOCAL hLocal = NULL;
  34.         BOOL bOK = ::FormatMessage(
  35.             FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
  36.             NULL,
  37.             m_dwErrCode,
  38.             m_dwLanguageID,
  39.             (LPTSTR)&hLocal,
  40.             0,
  41.             NULL
  42.         );
  43.         m_strErrMessage = bOK ? string(static_cast<const char*>(hLocal)) : "";
  44.         if (NULL != hLocal)
  45.         {
  46.             ::LocalFree(hLocal);
  47.         }
  48.         m_bChanged = false;
  49.     }
  50.     return m_strErrMessage;
  51. }
// 示例

  1. #include "WinErrMessage.h"
  2. #include <iostream>
  3. #include <windows.h>
  4. using namespace std;
  5. int main()
  6. {
  7.     WinErrMessage win(0x32);
  8.     cout << static_cast<string>(win) << endl;
  9.     win.setErrCode(0x11);
  10.     cout << static_cast<string>(win) << endl;
  11.     win.setLangID( MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED) );
  12.     cout << static_cast<string>(win) << endl;
  13.     string str = win;
  14.     cout << str << endl;
  15.     cout << static_cast<string>(WinErrMessage(0x11)) << endl;
  16.     return 0;
  17. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值