{"id":91801,"date":"2015-08-17T07:00:00","date_gmt":"2015-08-17T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20150817-00\/?p=91801\/"},"modified":"2019-03-13T12:18:34","modified_gmt":"2019-03-13T19:18:34","slug":"20150817-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20150817-00\/?p=91801","title":{"rendered":"p\/invoke gotcha: C++ bool is not Win32 BOOLEAN is not UnmanagedType.Bool"},"content":{"rendered":"<p>Welcome to CLR Week. I hope you enjoy your stay. <\/p>\n<p>A customer reported that their p\/invoke was not working. <!--more--><\/p>\n<blockquote CLASS=\"q\">\n<p>We aren&#8217;t getting the proper return codes from the <code>Audit&shy;Set&shy;System&shy;Policy<\/code>. When the call succeeds, the return code is 1, as expected. But in our tests, when we force the call to fail (insufficient access), the return code is not zero. Instead, the return code is some value of the form 0xFFxxxxxx, where the x&#8217;s vary, but the high byte is always 0xFF. <\/p>\n<p>For reference, the DllImport declaration we are using is<\/p>\n<pre>\n[DllImport(\"advapi32.dll\", SetLastError=true)]\npublic static extern UInt32 AuditSetSystemPolicy(\n    IntPtr pAuditPolicy,\n    UInt32 policyCount);\n<\/pre>\n<p>The corresponding Win32 declaration is <\/p>\n<pre>\nBOOLEAN WINAPI <a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/aa375712(v=vs.85).aspx\">AuditSetSystemPolicy<\/a>(\n  _In_  PCAUDIT_POLICY_INFORMATION pAuditPolicy,\n  _In_  ULONG PolicyCount\n);\n<\/pre>\n<\/blockquote>\n<p>Alas, the customer fell into one of the <a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2009\/08\/13\/9867383.aspx\">common gotchas when writing p\/invoke<\/a>: They confused <code>BOOLEAN<\/code> and <code>BOOL<\/code>. <\/p>\n<p><code>BOOL<\/code> is a 32-bit integer, whereas <code>BOOLEAN<\/code> is an 8-bit integer. <\/p>\n<p>Since they were marshaling the return code as a <code>UInt32<\/code>, they were getting the byte returned by the function, plus three bonus uninitialized garbage bytes. If they studied more closely, they would have found that the erroneous return codes were all of the form <code>0xFFxxxx00<\/code> where the bottom 8 bits are all zero. That&#8217;s because the bottom 8 bits are the actual value; the rest are garbage. <\/p>\n<p>The correct declaration is to use <code>Unmanaged&shy;Type.U1<\/code> aka <code>byte<\/code> rather than <code>Unmanaged&shy;Type.U4<\/code> aka <code>UInt32<\/code>. <\/p>\n<pre>\n[DllImport(\"advapi32.dll\", SetLastError=true)]\npublic static extern byte AuditSetSystemPolicy(\n    IntPtr pAuditPolicy,\n    UInt32 policyCount);\n<\/pre>\n<p>The customer confirmed that switching to <code>Unmanaged&shy;Type.U1<\/code> fixed the problem. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Watch the sizes.<\/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-91801","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Watch the sizes.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/91801","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=91801"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/91801\/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=91801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=91801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=91801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}