c++ - How to get descriptions for HRESULT error codes for WinRT / Windows 10 Store code? -


i'm converting win32 app uwp , writing windows store integration code using windows.services.store namespace. win32 c++ implemented via com interface methods seem return failures via hresult error codes.

so thought nice convert hresult codes descriptions can displayed end-users.

i tried following method:

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); } 

but unfortunately doesn't work. instance, formatmessage returns false , error code error_mr_mid_not_found following hresult values got experimentally running windows store integration code:

  • 0x80072eff
  • 0x803f6107

do have idea how descriptions hresult codes used in windows 10?

edit: following suggested sunius below came following code retrieve descriptions winrt error codes:

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 error code not 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); 

hresults on windows runtime apis work similar setlasterror()/getlasterror() mechanism, except rather storing error code in thread local storage, these apis instead return error code directly , store extended error information in thread local storage. call getrestrictederrorinfo() retrieve irestrictederrorinfo interface, , call geterrordetails() on error string.

be careful when using - must not call windows runtime apis between receiving failed hresult , retrieving irestrictederrorinfo interface, can overwritten @ time (the same limitation exists getlasterror(). there 2 functions set restricted error info: rooriginatelanguageexception() , rooriginateerror().

i don't recommend using formatmessage() unless you're using fallback when there's no irestrictederrorinfo available: in cases, not reasonable message it.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -