首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在C++中正确使用FormatMessage()?

如何在C++中正确使用FormatMessage()?
EN

Stack Overflow用户
提问于 2009-01-19 00:43:37
回答 8查看 62K关注 0票数 92

不带的

  • MFC
  • ATL

如何使用FormatMessage()获取HRESULT的错误文本

代码语言:javascript
运行
复制
 HRESULT hresult = application.CreateInstance("Excel.Application");

 if (FAILED(hresult))
 {
     // what should i put here to obtain a human-readable
     // description of the error?
     exit (hresult);
 }
EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2009-01-18 17:32:16

下面是从系统获取错误消息的正确方法(本例中名为hresult,或者您可以将其替换为GetLastError()):

代码语言:javascript
运行
复制
LPTSTR errorText = NULL;

FormatMessage(
   // use system message tables to retrieve error text
   FORMAT_MESSAGE_FROM_SYSTEM
   // allocate buffer on local heap for error text
   |FORMAT_MESSAGE_ALLOCATE_BUFFER
   // Important! will fail otherwise, since we're not 
   // (and CANNOT) pass insertion parameters
   |FORMAT_MESSAGE_IGNORE_INSERTS,  
   NULL,    // unused with FORMAT_MESSAGE_FROM_SYSTEM
   hresult,
   MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
   (LPTSTR)&errorText,  // output 
   0, // minimum size for output buffer
   NULL);   // arguments - see note 
   
if ( NULL != errorText )
{
   // ... do something with the string `errorText` - log it, display it to the user, etc.

   // release memory allocated by FormatMessage()
   LocalFree(errorText);
   errorText = NULL;
}

这与David Hanak的答案之间的关键区别在于FORMAT_MESSAGE_IGNORE_INSERTS标志的使用。MSDN不太清楚应该如何使用插入,但是在检索系统消息时使用Raymond Chen notes that you should never use them,因为您无法知道系统需要哪些插入。

顺便说一句,如果你使用的是Visual C++,你可以通过使用_com_error类让你的工作变得更容易一些:

代码语言:javascript
运行
复制
{
   _com_error error(hresult);
   LPCTSTR errorText = error.ErrorMessage();
   
   // do something with the error...

   //automatic cleanup when error goes out of scope
}

据我所知,它不是MFC或ATL的一部分。

票数 137
EN

Stack Overflow用户

发布于 2009-09-16 22:15:29

请记住,您不能执行以下操作:

代码语言:javascript
运行
复制
{
   LPCTSTR errorText = _com_error(hresult).ErrorMessage();

   // do something with the error...

   //automatic cleanup when error goes out of scope
}

因为类是在堆栈上创建和销毁的,所以让errorText指向无效的位置。在大多数情况下,此位置仍然包含错误字符串,但在编写线程化应用程序时,这种可能性很快就会消失。

所以总是按照上面的Shog9回答的那样做:

代码语言:javascript
运行
复制
{
   _com_error error(hresult);
   LPCTSTR errorText = error.ErrorMessage();

   // do something with the error...

   //automatic cleanup when error goes out of scope
}
票数 14
EN

Stack Overflow用户

发布于 2009-01-18 16:57:41

试试这个:

代码语言:javascript
运行
复制
void PrintLastError (const char *msg /* = "Error occurred" */) {
        DWORD errCode = GetLastError();
        char *err;
        if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
                           NULL,
                           errCode,
                           MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // default language
                           (LPTSTR) &err,
                           0,
                           NULL))
            return;

        static char buffer[1024];
        _snprintf(buffer, sizeof(buffer), "ERROR: %s: %s\n", msg, err);
        OutputDebugString(buffer); // or otherwise log it
        LocalFree(err);
}
票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/455434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档