gpt4 book ai didi

c++ - 如何获取 WinRT/Windows 10 商店代码的 HRESULT 错误代码的说明?

转载 作者:太空狗 更新时间:2023-10-29 20:35:52 27 4
gpt4 key购买 nike

我正在将我的 Win32 应用程序转换为 UWP,现在正在使用 Windows.Services.Store 编写 Windows 应用商店集成代码命名空间。对于 Win32 C++,它主要通过 COM 接口(interface)方法实现,这些方法似乎通过 HRESULT 错误代码返回它们的失败。

所以我认为将那些 HRESULT 代码转换成可以显示给最终用户的描述会很好。

我尝试了以下方法:

int nOSError = (int)hresult;

LPVOID lpMsgBuf;
if(FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
nOSError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf, 0, NULL))
{
//Success

LocalFree(lpMsgBuf);
}

但不幸的是,它并不总是有效。例如,对于以下 HRESULT 值,FormatMessage 返回 FALSE 和错误代码 ERROR_MR_MID_NOT_FOUND Windows 商店集成代码:

  • 0x80072EFF
  • 0x803F6107

您知道如何获取 Windows 10 现在使用的大多数 HRESULT 代码的说明吗?

编辑按照Sunius的建议下面我想出了以下代码来检索 WinRT 错误代码的描述:

HRESULT hr;

ComPtr<IRestrictedErrorInfo> pErrInfo;
if (SUCCEEDED(hr = ::GetRestrictedErrorInfo(&pErrInfo)) &&
pErrInfo)
{
HRESULT hrErr;
CComBSTR strErrDesc, strErrRestr, strSid;
if (SUCCEEDED(hr = pErrInfo->GetErrorDetails(&strErrDesc, &hrErr, &strErrRestr, &strSid)))
{
//Set empty message
::RoOriginateError(-1, NULL);

//Get empty error message text
ComPtr<IRestrictedErrorInfo> pEmptyErrInfo;
if (SUCCEEDED(hr = ::GetRestrictedErrorInfo(&pEmptyErrInfo)) &&
pEmptyErrInfo)
{
HRESULT hrDummy;
CComBSTR strEmptyErrDesc, strDummy1, strDummy2;
if (SUCCEEDED(hr = pEmptyErrInfo->GetErrorDetails(&strEmptyErrDesc, &hrDummy, &strDummy1, &strDummy2)))
{
//Remove "The text associated with this error code could not be found" messages
if (strErrDesc.ByteLength() == strEmptyErrDesc.ByteLength() &&
memcmp(strErrDesc.operator LPWSTR(), strEmptyErrDesc.operator LPWSTR(), strErrDesc.ByteLength()) == 0)
{
strErrDesc.Empty();
}

if (strErrRestr.ByteLength() == strEmptyErrDesc.ByteLength() &&
memcmp(strErrRestr.operator LPWSTR(), strEmptyErrDesc.operator LPWSTR(), strErrRestr.ByteLength()) == 0)
{
strErrRestr.Empty();
}
}
}


LPCTSTR pS_ErrDesc = strErrDesc.operator LPWSTR();
LPCTSTR pS_Restr = strErrRestr.operator LPWSTR();

TCHAR buff[1024];
if(SUCCEEDED(::StringCchPrintf(buff,
1024,
L"ERROR hr=0x%X\n"
L"desc=\"%s\"\n"
L"restr=\"%s\""
,
hrErr,
pS_ErrDesc,
pS_Restr
)))
{
//Get message in 'buff'

}
}
else
ASSERT(NULL);
}
else
ASSERT(NULL);

最佳答案

Windows 运行时 API 上的 HRESULT 的工作方式类似于 SetLastError()/GetLastError()机制,除了不是将错误代码存储在线程本地存储中,而是这些 API 直接返回错误代码并将扩展错误信息存储在线程本地存储中。调用GetRestrictedErrorInfo()检索 IRestrictedErrorInfo界面,然后调用GetErrorDetails()在它上面获取错误字符串。

使用它时要小心——您不能在接收失败的 HRESULT 和检索 IRestrictedErrorInfo 接口(interface)之间调用任何 Windows 运行时 API,因为它可以随时被覆盖(与 GetLastError() 存在相同的限制。有两个函数可以设置受限制的错误信息:RoOriginateLanguageException()RoOriginateError()

我不建议使用 FormatMessage() 除非您在没有可用的 IRestrictedErrorInfo 时将其用作后备:在大多数情况下,您不会从中获得合理的消息。

关于c++ - 如何获取 WinRT/Windows 10 商店代码的 HRESULT 错误代码的说明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40572998/

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