{"id":353,"date":"2014-08-01T07:00:00","date_gmt":"2014-08-01T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2014\/08\/01\/before-claiming-that-a-function-doesnt-work-you-should-check-what-youre-passing-to-it-and-what-it-returns\/"},"modified":"2014-08-01T07:00:00","modified_gmt":"2014-08-01T07:00:00","slug":"before-claiming-that-a-function-doesnt-work-you-should-check-what-youre-passing-to-it-and-what-it-returns","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20140801-00\/?p=353","title":{"rendered":"Before claiming that a function doesn&#039;t work, you should check what you&#039;re passing to it and what it returns"},"content":{"rendered":"<p>\nBefore claiming that a function doesn&#8217;t work,\nyou should check what you&#8217;re passing to it and what it returns,\nbecause it may be that the function is behaving just fine\nand the problem is elsewhere.\n<\/p>\n<blockquote CLASS=\"q\">\n<p>\nThe <code>Get&shy;Current&shy;DirectoryW<\/code> function does\nnot appear to support directories with Unicode characters in their\nnames.\n<\/p>\n<pre>\nwchar_t currentDirectory[MAX_PATH];\nGetCurrentDirectoryW(MAX_PATH, currentDirectory);\nwcout &lt;&lt; currentDirectory &lt;&lt; endl;\n<\/pre>\n<p>\nThe correct directory name is obtained if it contains only ASCII\ncharacters in its name,\nbut it truncates the string at the first non-ASCII character.\n<\/p>\n<\/blockquote>\n<p>\nIf you step through the code in the debugger,\nyou&#8217;ll see that the\n<code>Get&shy;Current&shy;DirectoryW<\/code> function is\nworking just fine.\nThe buffer is filled with the current directory,\nincluding the non-ASCII characters.\nThe problem is that the <code>wcout<\/code> stream stops printing\nthe directory name at the first\nnon-ASCII characters.\nAnd that&#8217;s because the default locale for <code>wcout<\/cODE>\nis the <code>\"C\"<\/code> locale, and the\n<code>\"C\"<\/code> locale is\n\"the minimal environment for C translation.\"\nThe <code>\"C\"<\/code> locale is\nuseless for actual work involving, you know, locales.\nYou will have to do some language-specific munging to get\nthe characters to reach the screen in the format you want,\nthe details of which are not the point of today's topic.\n<\/p>\n<p>\nIn other words, the bug was not in the\n<code>Get&shy;Current&shy;DirectoryW<\/code> function.\nIt was in what you did with the result of the\n<code>Get&shy;Current&shy;DirectoryW<\/code> function.\n<\/p>\n<p>\nHere's another example of thinking the problem is in a\nfunction when it isn't:\n<\/p>\n<blockquote CLASS=\"q\">\n<p>\nThe <code>Set&shy;Window&shy;TextW<\/code> function does not appear\nto support Unicode, despite its name.\n<\/p>\n<pre>\nwstring line;\nwifstream file(\"test\"); \/\/ this file is in Unicode\ngetline(file, line);\nSetWindowTextW(hwnd, line.c_str());\n<\/pre>\n<\/blockquote>\n<p>\nIf you look at the <code>line<\/code> variable before you\neven get around to calling\n<code>Set&shy;Window&shy;TextW<\/code>,\nyou'll see that it does not contain the text from your Unicode file.\nThe problem is that the default <code>wifstream<\/code> reads\nthe text as an 8-bit file, and then internally converts it\n(according to the lame <code>\"C\"<\/code> locale) to Unicode.\nIf the original file is already Unicode, you're doing a double\nconversion and things don't go well.\nYou then pass this incorrectly-converted string to\n<code>Set&shy;Window&shy;TextW<\/code>,\nwhich naturally displays something different from what you intended.\n<\/p>\n<p>\nAgain, the point is not to delve into the intricacies of\n<code>wifstream<\/code>.\nThe point is that the problem occurred even before you called\n<code>Set&shy;Window&shy;TextW<\/code>.\nThe observed behavior, then, is simple a case of\nGarbage In, Garbage Out.\n<\/p>\n<p>\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2010\/05\/20\/10013612.aspx\">\nHere's another example from a few years ago<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Before claiming that a function doesn&#8217;t work, you should check what you&#8217;re passing to it and what it returns, because it may be that the function is behaving just fine and the problem is elsewhere. The Get&shy;Current&shy;DirectoryW function does not appear to support directories with Unicode characters in their names. wchar_t currentDirectory[MAX_PATH]; GetCurrentDirectoryW(MAX_PATH, currentDirectory); wcout [&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-353","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Before claiming that a function doesn&#8217;t work, you should check what you&#8217;re passing to it and what it returns, because it may be that the function is behaving just fine and the problem is elsewhere. The Get&shy;Current&shy;DirectoryW function does not appear to support directories with Unicode characters in their names. wchar_t currentDirectory[MAX_PATH]; GetCurrentDirectoryW(MAX_PATH, currentDirectory); wcout [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/353","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=353"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/353\/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=353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}