c++ - 数字 ErrorMessage 的含义

标签 c++ windows visual-studio-2012 com atl

我正在尝试与 OEM 库交互。在一台计算机上一切正常,但我在另一台计算机上遇到了很多问题。

我的代码抛出一个 COM 异常,但我无法弄清楚没有 ErrorMessage(); 的错误代码的含义

代码

#include "stdafx.h"
#include <afx.h>
#include <iostream>
using namespace std;
#import "MTBApi.tlb" named_guids //raw_interfaces_only
using namespace MTBApi; 
void DisplayError(_com_error* e)
{
    CString message;
    // if it is an application error thrown by .NET
    if (e->Error() >= 0x80041000)
    {
        IErrorInfo* info;
        BSTR msg;
        info = e->ErrorInfo();
        info->GetDescription(&msg);
        info->Release();
        message = CString(msg);
    }
    // other com errors
    else
    {
        message = e->ErrorMessage();
    }
    cout << "MTB Error: " <<  message <<":"<<(unsigned int) e->Error()<<  endl;
}
int main(int argc, char **argv)
{
    for (int i = 0 ; i < 4 ; i++)
    {
    IMTBConnectionPtr m_MTBConnection;
    try
    {
        cout <<"1" << endl;
        HRESULT a = CoInitializeEx(NULL,COINIT_SPEED_OVER_MEMORY);
        cout <<"2" << endl;
        m_MTBConnection = IMTBConnectionPtr(CLSID_MTBConnection);
        cout <<"3" << endl;
        m_MTBConnection->Close();
        cout <<"4" << endl;
        CoUninitialize();
        cout <<"5" << endl;
    }
    catch(_com_error e)
    {
        DisplayError(&e);
    }
    cout << endl;
    }
}

运行时输出

1
2
MTB Error: 00000000002205F8:2147746132

1
2
MTB Error: 00000000002205F8:2147746132

1
2
MTB Error: 00000000002205F8:2147746132

1
2
MTB Error: 00000000002205F8:2147746132

Dependency Walker 的输出相当详细

http://pastebin.com/7Y33z3Pj

最佳答案

cout << "MTB Error: " <<  message <<":"<<(unsigned int) e->Error()<<  endl;

cout 不太擅长显示 Unicode 字符串,它仅显示字符串指针值。当然没有用,请改用 wcout。并支持以十六进制显示错误代码。 0x80040154 是一个非常常见的 COM 错误,“类未注册”。已经有成千上万的问题,您只需要正确注册 COM 服务器。如果您不知道该怎么做,请询问供应商或作者。

关于c++ - 数字 ErrorMessage 的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17219597/

相关文章:

c++ - unordered_map::find with key std::pair of pointers with custom hash crashes in VS2012

c++ - C++ 中使用 lambda 函数进行排序

c++ - CMake:在项目范围内传播编译选项

windows - 有没有办法在当前文件夹中打开命令提示符?

windows - 使用 portaudio 设置音频输入电平

windows - 一个进程如何访问外部 TIB?

c++ - 如何使operator=像operator+一样接受参数的导数?

c++ - DLL 导出 4 个 COM 函数但不导出 COM 对象?!我就是不明白!

c# - 如何使用特定参数计算零

visual-studio-2012 - 在获取最新文件时 - 服务器违反了协议(protocol)。部分=响应状态线 TFS