{"id":105609,"date":"2021-08-26T07:00:00","date_gmt":"2021-08-26T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=105609"},"modified":"2021-08-26T07:21:41","modified_gmt":"2021-08-26T14:21:41","slug":"20210826-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20210826-00\/?p=105609","title":{"rendered":"In C#, how do I get the descriptive text for an HRESULT?"},"content":{"rendered":"<p>Suppose your C# code manages to find in its hands a COM <code>HRESULT<\/code> value. How do you convert that to a descriptive string?<\/p>\n<p>One idea is to p\/invoke to the <code>Format\u00adMessage<\/code> function, but that&#8217;s awfully cumbersome. Maybe we can live off the land.<\/p>\n<pre>using System;\r\nusing System.Runtime.InteropServices;\r\n\r\nclass Program\r\n{\r\n    public static string MessageFromHResult(int hr)\r\n    {\r\n        return Marshal.GetExceptionForHR(hr).Message;\r\n    }\r\n\r\n    public static void Main()\r\n    {\r\n        var E_BOUNDS = unchecked((int)0x8000000B);\r\n        System.Console.WriteLine(MessageFromHResult(E_BOUNDS));\r\n    }\r\n}\r\n<\/pre>\n<p>The result:<\/p>\n<pre style=\"white-space: pre-wrap;\">The operation attempted to access data outside the valid range (Exception from HRESULT: 0x8000000B)\r\n<\/pre>\n<p>We ask the Marshal class to create an exception from the <code>HRESULT<\/code> and extract the <code>Message<\/code> from it. This does do more work than necessary, because it also does additional exception-related stuff, like capturing the COM error context into the exception object. This is wasted work, but on the other hand, the convenience is hard to beat.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can pretend it was an exception.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-105609","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>You can pretend it was an exception.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/105609","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=105609"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/105609\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=105609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=105609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=105609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}