{"id":32833,"date":"2006-01-03T03:15:36","date_gmt":"2006-01-03T03:15:36","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2006\/01\/03\/theres-more-to-calling-a-function-than-just-getting-the-types-to-match\/"},"modified":"2006-01-03T03:15:36","modified_gmt":"2006-01-03T03:15:36","slug":"theres-more-to-calling-a-function-than-just-getting-the-types-to-match","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20060103-36\/?p=32833","title":{"rendered":"There&#039;s more to calling a function than just getting the types to match"},"content":{"rendered":"<p>\nHere&#8217;s a classic novice error.\nYou want to call a function, say\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/fileio\/fs\/getbinarytype.asp\">\nGetBinaryType<\/a>.\n<\/p>\n<pre>\nvoid sample()\n{\n if (GetBinaryType(TEXT(\"explorer.exe\"), <font COLOR=\"blue\">????<\/font>)) {\n  ...\n }\n}\n<\/pre>\n<p>\nWhat should you write for those question marks?\nWell, the prototype says that the second parameter is an LPDWORD,\nso let&#8217;s pass it one.\n<\/p>\n<pre>\nvoid sample()\n{\n if (GetBinaryType(TEXT(\"explorer.exe\"), <font COLOR=\"blue\">(LPDWORD)NULL<\/font>)) {\n  ...\n }\n}\n<\/pre>\n<p>\nHm, but that crashes.  Well, maybe we can pass it an LPDWORD this way:\n<\/p>\n<pre>\nvoid sample()\n{\n <font COLOR=\"blue\">LPDWORD lpdw;<\/font>\n if (GetBinaryType(TEXT(\"explorer.exe\"), <font COLOR=\"blue\">lpdw<\/font>)) {\n  ...\n }\n}\n<\/pre>\n<p>\nHm, that still crashes.  Oh wait, it&#8217;s because of the uninitialized\nvariable.\n<\/p>\n<pre>\nvoid sample()\n{\n <font COLOR=\"blue\">LPDWORD lpdw = NULL;<\/font>\n if (GetBinaryType(TEXT(\"explorer.exe\"), <font COLOR=\"blue\">lpdw<\/font>)) {\n  ...\n }\n}\n<\/pre>\n<p>\nNo, that still crashes.\nHang on, I know where I can get an LPDWORD.\n<\/p>\n<pre>\nvoid sample()\n{\n <font COLOR=\"blue\">LPDWORD lpdw = new DWORD;<\/font>\n if (GetBinaryType(TEXT(\"explorer.exe\"), <font COLOR=\"blue\">lpdw<\/font>)) {\n  ...\n }\n}\n<\/pre>\n<p>\nThis code finally works!\nOkay, on to the next problem&#8230;\n<\/p>\n<p>\nOf course, the seasoned programmer still shakes his head.\nJust because the function prototype says that the parameter is an\nLPDWORD doesn&#8217;t mean you have to have a variable whose type is LPDWORD.\nYou merely need an <strong>expression<\/strong> whose type is\nLPDWORD.\n<\/p>\n<pre>\nvoid sample()\n{\n <font COLOR=\"blue\">DWORD dw;<\/font>\n if (GetBinaryType(TEXT(\"explorer.exe\"), <font COLOR=\"blue\">&amp;dw<\/font>)) {\n  ...\n }\n}\n<\/pre>\n<p>\nWhy am I telling you this?\n<\/p>\n<p>\nNo, I&#8217;m not trying to insult your intelligence.\nI&#8217;m trying to get you to think like a novice.\nSometimes you&#8217;ll be reading a chunk of code and\nfind something bizarro, like the fragments above\nwith the <code>new DWORD<\/code>.\nWhen you do (for example, when chasing the memory leak\nin that code sequence),\ndon&#8217;t tear your hair out trying to find\nsome deep meaning for the seemingly roundabout way\nof accomplishing a simple task.\nIf you can think like a novice, you will\nmerely recognize it as a classic beginner mistake,\nfix it, and get on with your life.\n<\/p>\n<p>\n[While Raymond was on vacation,\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2004\/01\/26\/63163.aspx\">\nthe autopilot stopped working<\/a>\ndue to\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2005\/09\/19\/471240.aspx\">\na power outage<\/a>.\nThis entry has been backdated.]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a classic novice error. You want to call a function, say GetBinaryType. void sample() { if (GetBinaryType(TEXT(&#8220;explorer.exe&#8221;), ????)) { &#8230; } } What should you write for those question marks? Well, the prototype says that the second parameter is an LPDWORD, so let&#8217;s pass it one. void sample() { if (GetBinaryType(TEXT(&#8220;explorer.exe&#8221;), (LPDWORD)NULL)) { &#8230; [&hellip;]<\/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-32833","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Here&#8217;s a classic novice error. You want to call a function, say GetBinaryType. void sample() { if (GetBinaryType(TEXT(&#8220;explorer.exe&#8221;), ????)) { &#8230; } } What should you write for those question marks? Well, the prototype says that the second parameter is an LPDWORD, so let&#8217;s pass it one. void sample() { if (GetBinaryType(TEXT(&#8220;explorer.exe&#8221;), (LPDWORD)NULL)) { &#8230; [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/32833","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=32833"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/32833\/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=32833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=32833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=32833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}