您好,
当使用DirectX时,您可以使用名为DxErr9.h的头文件进行#include,它具有非常有用的函数,例如:
DXGetErrorString9
和
DXGetErrorDescription9
它们根据HR告诉您有关错误的所有信息。
但是现在在使用COM和OLE时,我发现我需要自己处理从COM函数返回的HRESULTS。现在只能依靠MSDN,或者我还没有找到类似的OLE DB辅助函数吗?
HRESULT
更有用的信息。HRESULT hr=something();
if (FAILED(hr))
{
CComPtr<IErrorInfo> err;
::GetErrorInfo(0, &err);
if (err)
{
CComBSTR description;
err->GetDescription(&description);
// description will be a more descriptive error message than just formatting the
// HRESULT because it is set by the COM server code at the point of the error
}
}
使用 _com_error 获取有意义的字符串:
#include <comdef.h>
HRESULT hr = SomeComFunc();
if ( FAILED(hr) )
{
_com_error err(hr);
LPTCSTR szErrMsg = err.ErrorMessage();
// log szErrMsg or whatever
}