gpt4 book ai didi

c++ - 处理 COM 相关错误的有效方法 (C++)

转载 作者:行者123 更新时间:2023-11-30 02:13:22 25 4
gpt4 key购买 nike

C++ 中处理 COM 相关错误的有效方法。

例如:

 switch (HRESULT_CODE(hresult)) {
case NOERROR:
cout << "Object instantiated and "
"pointer to interface IS8Simulation "
"obtained" << endl;
break;
//Specifed Class not registered
case REGDB_E_CLASSNOTREG:
cerr << "Specified Class not registered." << endl;
exit (EXIT_FAILURE);
break;
case CLASS_E_NOAGGREGATION:
cerr << "The Class does not support aggregation "
"(or class object is remote)." << endl;
exit (EXIT_FAILURE);
break;
//Interface not supported - exit with error
case E_NOINTERFACE:
cerr << "No such interface supported." << endl;
exit (EXIT_FAILURE);
break;
case E_UNEXPECTED:
default:
cerr << "Catastrophic failure." << endl;
exit (EXIT_FAILURE);
break;
}

前者相比:

if (SUCCEEDED(hresult))
{
cout << "The COM library was initialised"
" successfully on this thread" << endl;
} else {
cerr << "Fatal Error: COM library was not"
" initialised" << endl;
exit (EXIT_FAILURE);
}

问题:

  • 还有其他更适用的方法吗?

问候

最佳答案

使用FormatMessage获取错误文本——它已经知道如何查找大多数 HRESULT 和 Win32 结果代码的本地化文本。

使用 FAILEDSUCCEEDED 宏来判断某件事是否有效。

exit 采用 32 位数字。您可以使用 HRESULT 作为进程退出代码:

HRESULT hr;
if (FAILED(hr = p->QueryInterface(...)))
{
cerr << MessageFromHResult(hr); // left as an exercise for the reader
exit(hr);
}

关于c++ - 处理 COM 相关错误的有效方法 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/444726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com