{"id":26663,"date":"2007-05-29T10:00:00","date_gmt":"2007-05-29T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2007\/05\/29\/psychic-debugging-why-does-formatmessage-say-the-resource-couldnt-be-found\/"},"modified":"2007-05-29T10:00:00","modified_gmt":"2007-05-29T10:00:00","slug":"psychic-debugging-why-does-formatmessage-say-the-resource-couldnt-be-found","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20070529-00\/?p=26663","title":{"rendered":"Psychic debugging: Why does FormatMessage say the resource couldn&#039;t be found?"},"content":{"rendered":"<p>\nSolving this next problem should be a snap with your\nnascent psychic powers:\n<\/p>\n<blockquote CLASS=\"q\"><p>\nI&#8217;m trying use <code>FormatMessage<\/code>\nto load a resource string with one insertion in it,\nand this doesn&#8217;t work for some reason.\nThe string is\n&#8220;Blah blah blah %1. Blah blah blah.&#8221;\nThe call to <code>FormatMessage<\/code> fails,\nand <code>GetLastError()<\/code> returns\n<code>ERROR_RESOURCE_TYPE_NOT_FOUND<\/code>.\nWhat am I doing wrong?<\/p>\n<pre>\nLPTSTR pszInsertion = TEXT(\"Sample\");\nLPTSTR pszResult;\nFormatMessage(\n        FORMAT_MESSAGE_ALLOCATE_BUFFER |\n        FORMAT_MESSAGE_FROM_HMODULE |\n        FORMAT_MESSAGE_ARGUMENT_ARRAY,\n        \/\/I also tried an instance handle and NULL.\n        GetModuleHandle(NULL),\n        IDS_MY_CUSTOM_MESSAGE,\n        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), \/\/ default language\n        (LPTSTR) &amp;pszResult,\n        0,\n        (va_list*) &amp;pszInsertion);\n<\/pre>\n<\/blockquote>\n<p>\nHint: Take a closer look at the parameter\n<code>IDS_MY_CUSTOM_MESSAGE<\/code>.\n<\/p>\n<p>\nHint&nbsp;2: What does &#8220;<code>IDS_<\/code>&#8221; tell you?\n<\/p>\n<p>\nResource identifiers that begin with &#8220;<code>IDS_<\/code>&#8221;\nare typically string resource identifiers, not message resource\nidentifiers.\nThere is no strong consensus on the naming convention for\nmessage resource identifiers,\nalthough I&#8217;ve seen &#8220;<code>MSG_<\/code>&#8220;.\nPart of the reason why there is no strong consensus on the naming\nconvention for message resource identifiers is that almost nobody\nuses message resources!\nI don&#8217;t understand why they were added to Win32, since there\nwas already a way of embedding strings in resources,\nnamely, string resources.\n<\/p>\n<p>\nThat&#8217;s why you&#8217;re getting <code>ERROR_RESOURCE_TYPE_NOT_FOUND<\/code>.\nThere is no message resource in your module.\nIf you&#8217;re not going to use a message resource, you&#8217;ll have to\nuse the <code>FORMAT_MESSAGE_FROM_STRING<\/code> flag and\npass the format string explicitly.\n<\/p>\n<pre>\nDWORD_PTR rgdwInsertions[1] = { (DWORD_PTR)TEXT(\"Sample\") };\nTCHAR szFormat[256];\nLoadString(hInstance, IDS_MY_CUSTOM_MESSAGE, szFormat, 256);\nLPTSTR pszResult;\nFormatMessage(\n        FORMAT_MESSAGE_ALLOCATE_BUFFER |\n        FORMAT_MESSAGE_FROM_STRING |\n        FORMAT_MESSAGE_ARGUMENT_ARRAY,\n        szFormat,\n        0,\n        0,\n        (LPTSTR) &amp;pszResult,\n        0,\n        (va_list*) &amp;rgdwInsertions);\n<\/pre>\n<p>\nI also made a slight change to the final parameter.\nWhen you use <code>FORMAT_MESSAGE_ARGUMENT_ARRAY<\/code>,\nthe last parameter must be an array of <code>DWORD_PTR<\/code>s.\n(The parameter must be cast to <code>va_list*<\/code> to keep\nthe compiler happy.)\nIt so happens that the original code got away with this mistake\nsince <code>sizeof(DWORD_PTR) == sizeof(LPTSTR)<\/code> and they\nboth have the same alignment requirements.\nOn the other hand, if the insertion were a <code>DWORD<\/code>,\npassing <code>(va_list*)&amp;dwValue<\/code> is definitely wrong\nand can crash if you&#8217;re sufficiently unlucky.\n(Determining the conditions under which your luck runs out\nis left as an exercise.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Solving this next problem should be a snap with your nascent psychic powers: I&#8217;m trying use FormatMessage to load a resource string with one insertion in it, and this doesn&#8217;t work for some reason. The string is &#8220;Blah blah blah %1. Blah blah blah.&#8221; The call to FormatMessage fails, and GetLastError() returns ERROR_RESOURCE_TYPE_NOT_FOUND. What am [&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-26663","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Solving this next problem should be a snap with your nascent psychic powers: I&#8217;m trying use FormatMessage to load a resource string with one insertion in it, and this doesn&#8217;t work for some reason. The string is &#8220;Blah blah blah %1. Blah blah blah.&#8221; The call to FormatMessage fails, and GetLastError() returns ERROR_RESOURCE_TYPE_NOT_FOUND. What am [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/26663","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=26663"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/26663\/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=26663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=26663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=26663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}