{"id":90851,"date":"2015-07-22T07:00:00","date_gmt":"2015-07-22T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20150722-00\/?p=90851\/"},"modified":"2019-03-13T12:17:33","modified_gmt":"2019-03-13T19:17:33","slug":"20150722-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20150722-00\/?p=90851\/","title":{"rendered":"When you think you found a problem with a function, make sure you&#8217;re actually calling the function, episode 2"},"content":{"rendered":"<p>A customer reported that the <code>Duplicate&shy;Handle<\/code> function was failing with <code>ERROR_INVALID_HANDLE<\/code> even though the handle being passed to it seemed legitimate: <\/p>\n<pre>\n  \/\/ Create the handle here\n  m_Event =\n    ::CreateEvent(NULL, FALSE\/*bManualReset*\/,\n                       FALSE\/*bInitialState*\/, NULL\/*lpName*\/));\n  ... error checking removed ...\n\n\n\/\/ Duplicate it here\nHRESULT MyClass::CopyTheHandle(HANDLE *pEvent)\n{\n HRESULT hr = S_OK;\n \n if (m_Event != NULL) {\n  BOOL result = ::DuplicateHandle(\n                GetCurrentProcess(),\n                m_Event,\n                GetCurrentProcess(),\n                pEvent,\n                0,\n                FALSE,\n                DUPLICATE_SAME_ACCESS\n                );\n  if (!result) {\n    \/\/ always fails with ERROR_INVALID_HANDLE\n    return HRESULT_FROM_WIN32(GetLastError());\n  }\n } else {\n  *pEvent = NULL;\n }\n \n return hr;\n}\n<\/pre>\n<p>The handle in <code>m_Event<\/code> appears to be valid. It is non-null, and we can still set and reset it. But we can&#8217;t duplicate it. <\/p>\n<p>Now, before claiming that a function doesn&#8217;t work, <a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2014\/08\/01\/10546597.aspx\">you should check what you&#8217;re passing to it and what it returns<\/a>. The customer checked the <code>m_Event<\/code> parameter, but what about the other parameters? The function takes <i>three<\/i> handle parameters, after all, and they checked only one of them. According to the debugger, <code>Duplicate&shy;Handle<\/code> was called with the parameters <\/p>\n<table>\n<tr>\n<td><code>hSourceProcessHandle<\/code><\/td>\n<td><code>&nbsp;= 0x0aa15b80<\/code><\/td>\n<\/td>\n<tr>\n<td><code>hSourceHandle<\/code><\/td>\n<td><code>&nbsp;= 0x00000ed8<\/code><\/td>\n<\/td>\n<td>&larr; <code>m_Event<\/code>, appears to be valid <\/p>\n<tr>\n<td><code>hTargetProcessHandle<\/code><\/td>\n<td><code>&nbsp;= 0x0aa15b80<\/code><\/td>\n<\/td>\n<tr>\n<td><code>lpTargetHandle<\/code><\/td>\n<td><code>&nbsp;= 0x00b0d914<\/code><\/td>\n<\/td>\n<tr>\n<td><code>dwDesiredAccess<\/code><\/td>\n<td><code>&nbsp;= 0x00000000<\/code><\/td>\n<\/td>\n<tr>\n<td><code>bInheritHandle<\/code><\/td>\n<td><code>&nbsp;= 0x00000000<\/code><\/td>\n<\/td>\n<tr>\n<td><code>dwOptions<\/code><\/td>\n<td><code>&nbsp;= 0x00000002<\/code><\/td>\n<\/td>\n<\/table>\n<p>Upon sharing this information, the customer immediately saw the problem: The other two handle parameters come from the <code>Get&shy;Current&shy;Process<\/code> function, and that function was returning <code>0x0aa15b80<\/code> rather than the expected pseudo-handle (which is currently <code>-1<\/code>, but that is not contractual). <\/p>\n<p>The customer explained that their <code>My&shy;Class<\/code> has a method with the name <code>Get&shy;Current&shy;Process<\/code>, and it was that method which was being called rather than the Win32 function <code>Get&shy;Current&shy;Process<\/code>. They left off the leading <code>::<\/code> and ended up calling the wrong <code>Get&shy;Current&shy;Process<\/code>. <\/p>\n<p>By default, Visual Studio colors member functions and global functions the same, but you can change this in <a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/vstudio\/kw7t0545%28v=vs.110%29.aspx\">the <i>Fonts and Colors<\/i> options dialog<\/a>. Under <i>Show settings for<\/i>, select <i>Text Editor<\/i>, and then under <i>Display items<\/i> you can customize the colors to use for various language elements. In particular, you can choose a special color for static and instance member functions. <\/p>\n<p>Or, as a matter of style, you could have a policy of not giving member functions the same name as global functions. (This has the bonus benefit of reducing false positives when grepping.) <\/p>\n<p><b>Bonus story<\/b>: A different customer reported a problem with visual styles in the common tab control. After a few rounds of asking questions, coming up with theories, testing the theories, disproving the theories, the customer wrote back: &#8220;We figured out what was happening when we tried to step into the call to <code>Create&shy;Dialog&shy;Indirect&shy;ParamW<\/code>. Someone else in our code base redefined all the dialog creation functions in an attempt to enforce a standard font on all of them, but in doing so, they effectively made our code no longer isolation aware, because in the overriding routines, they called <code>Create&shy;Dialog&shy;Indirect&shy;ParamW<\/code> instead of <code>Isolation&shy;Aawre&shy;Create&shy;Dialog&shy;Indirect&shy;ParamW<\/code>. Thanks for all the help, and apologies for the false alarm.&#8221; <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Watch out for the local overrides.<\/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-90851","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Watch out for the local overrides.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/90851","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=90851"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/90851\/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=90851"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=90851"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=90851"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}