{"id":14893,"date":"2010-02-18T07:00:00","date_gmt":"2010-02-18T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2010\/02\/18\/dont-forget-to-double-null-terminate-those-strings-you-pass-to-shfileoperation\/"},"modified":"2010-02-18T07:00:00","modified_gmt":"2010-02-18T07:00:00","slug":"dont-forget-to-double-null-terminate-those-strings-you-pass-to-shfileoperation","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20100218-00\/?p=14893","title":{"rendered":"Don&#039;t forget to double-null-terminate those strings you pass to SHFileOperation"},"content":{"rendered":"<p>\nAbout once every two months for the past six months\n(I stopped checking further back),\nsomebody reports a problem with the\n<code>SHFileOperation<\/code> function.\nOften, they don&#8217;t include very much information at all.\nThey just say, &#8220;I call the function and it doesn&#8217;t work.&#8221;\nHere&#8217;s an example:\n<\/p>\n<blockquote CLASS=\"m\">\n<p>\nI&#8217;m hitting a problem with <code>SHFileOperation<\/code>\nwhen using it to frob files in the gonzo directory\nwhen the user&#8217;s SID ends in an odd number.\n<\/p>\n<pre>\n    \/\/\n    \/\/ Delete the file.\n    \/\/ szDeletePath contains the full path to the file.\n    \/\/\n    shFileOp.hwnd = NULL;\n    shFileOp.wFunc = FO_DELETE;\n    shFileOp.pFrom = szDeletePath;\n    shFileOp.pTo = NULL;\n    shFileOp.fFlags = FOF_NO_UI;\n    iRet = SHFileOperation( &amp;shFileOp );\n<\/pre>\n<p>\nThe function returns file not found, but the file\nis definitely there.\n<\/p>\n<\/blockquote>\n<p>\nIf you read the variable names carefully, you can see the problem.\n<\/p>\n<p>\nThe <code>pFrom<\/code> and <code>pTo<\/code> members of the\n<code>SHFILEOPSTRUCT<\/code> structure are\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2009\/10\/08\/9904646.aspx\">\ndouble-null-terminated strings<\/a>.\n(There&#8217;s even a callout box for this in the MSDN documentation.)\nBut a variable named <code>szDeletePath<\/code> is probably\na single-null-terminated string.\n(The name for a double-null-terminated string would be\n<code>szzDeletePath<\/code>.)\n<\/p>\n<blockquote CLASS=\"m\"><p>\nMy psychic powers tell me that\n<code>szDeletePath<\/code> is not double-null-terminated.\n<\/p><\/blockquote>\n<p>\nSo far, my psychic powers haven&#8217;t failed.\n<\/p>\n<p>\nNow, you might say that the fact that people make this mistake\nso often is a sign that the function is flawed.\nAnd if the function were designed today, I would agree with you.\nBut this function in its public form is over fifteen years old\n(and in its private form, is around 20 years old),\nand back in those days,\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2005\/10\/21\/483438.aspx\">\nprogrammers were assumed to have the time to understand the subtleties\nof what they were doing<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>About once every two months for the past six months (I stopped checking further back), somebody reports a problem with the SHFileOperation function. Often, they don&#8217;t include very much information at all. They just say, &#8220;I call the function and it doesn&#8217;t work.&#8221; Here&#8217;s an example: I&#8217;m hitting a problem with SHFileOperation when using it [&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-14893","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>About once every two months for the past six months (I stopped checking further back), somebody reports a problem with the SHFileOperation function. Often, they don&#8217;t include very much information at all. They just say, &#8220;I call the function and it doesn&#8217;t work.&#8221; Here&#8217;s an example: I&#8217;m hitting a problem with SHFileOperation when using it [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/14893","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=14893"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/14893\/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=14893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=14893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=14893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}