【问题标题】:get a text from the error code returns from the GetLastError() function从 GetLastError() 函数返回的错误代码中获取文本
【发布时间】:2011-03-01 16:16:16
【问题描述】:

我需要获取从 GetLastError 函数获得的错误代码的文本。 我看到了一些例子,但我想要一个获取代码并返回字符串的函数。 谢谢大家

【问题讨论】:

标签: c++ winapi error-handling


【解决方案1】:

我猜你想要这样的东西:

DWORD   dwLastError = ::GetLastError();
TCHAR   lpBuffer[256] = _T("?");
if(dwLastError != 0)    // Don't want to see a "operation done successfully" error ;-)
    ::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,                 // It´s a system error
                     NULL,                                      // No string to be formatted needed
                     dwLastError,                               // Hey Windows: Please explain this error!
                     MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),  // Do it in the standard language
                     lpBuffer,              // Put the message here
                     STR_ELEMS(lpBuffer)-1,                     // Number of bytes to store the message
                     NULL);

另见:http://msdn.microsoft.com/en-us/library/ms679351(VS.85).aspx

【讨论】:

  • 介意设置这个回答吗?谢谢!
  • 真的必须丢掉那个static_cast,它只会生成中文或一个字母的消息。
  • 实际上,它工作得很好。我从我们的一个项目中复制了这个。但是不要问我为什么有人把演员表放在那里……我把它去掉了。
  • 编译时出现错误:'FormatMessageW': function does not accept arguments 6。奇怪的是,我正在使用 FormatMessagenot FormatMessageW
  • 恐怕这个实现有太多问题了。请参阅proposed duplicate 了解更多信息。
猜你喜欢
  • 2010-11-26
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 2017-06-10
  • 2014-05-04
  • 2022-11-07
  • 2015-11-06
相关资源
最近更新 更多