{"id":23263,"date":"2008-02-29T10:00:00","date_gmt":"2008-02-29T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2008\/02\/29\/why-cant-i-get-formatmessage-to-load-my-resource-string\/"},"modified":"2008-02-29T10:00:00","modified_gmt":"2008-02-29T10:00:00","slug":"why-cant-i-get-formatmessage-to-load-my-resource-string","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20080229-00\/?p=23263","title":{"rendered":"Why can&#039;t I get FormatMessage to load my resource string?"},"content":{"rendered":"<p>\nA common mistake I see is people confusing message tables\nwith string resources.\nThis basically shows up in one place:\n<code>FormatMessage<\/code>.\n<\/p>\n<p>\nThe folks who were designing Win32\ndecided that plain string resources weren&#8217;t fancy enough,\nso they invented message tables, which is just another way\nof storing strings in resources.\nWhy string resources weren&#8217;t good enough I don&#8217;t know.\n<\/p>\n<p>\nThe <code>FormatMessage<\/code> function accepts a message number,\nwhich is looked up in a message table,\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2007\/05\/29\/2955171.aspx\">\nnot in a string resource<\/a>.\nIn other words, if you have something like this:\n<\/p>\n<pre>\n#define IDS_MYMESSAGE 100\nSTRINGTABLE BEGIN\n    IDS_MYMESSAGE \"Hello, %1\"\nEND\nTCHAR szBuffer[256];\nFormatMessage(FORMAT_MESSAGE_FROM_HMODULE, g_hInstance,\n              IDS_MYMESSAGE, 0, szBuffer, 256, Arguments);\n<\/pre>\n<p>\nthe call to <code>FormatMessage<\/code> will fail\nbecause you passed in a string resource identifier, not a message\nidentifier.\nSince\n<a HREF=\"http:\/\/en.wikipedia.org\/wiki\/Hyperbole\">\nnobody<\/a>\nactually uses message tables,\nyou almost certainly want to pass the\n<code>FORMAT_MESSAGE_FROM_STRING<\/code> flag.\n<\/p>\n<pre>\nTCHAR szMessage[256];\nLoadString(g_hInstance, IDS_MYMESSAGE, szMessage, 256);\nTCHAR szBuffer[256];\nFormatMessage(FORMAT_MESSAGE_FROM_STRING, szMessage,\n              IDS_MYMESSAGE, 0, szBuffer, 256, Arguments);\n<\/pre>\n<p>\nThe only place you will normally want to format a message from\na message table is when you want to access the system message table\nto get the text associated with an error code.\n(But\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2007\/11\/28\/6564257.aspx\">\ndon&#8217;t forget the\n<code>FORMAT_MESSAGE_IGNORE_INSERTS<\/code> flag<\/a>!)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A common mistake I see is people confusing message tables with string resources. This basically shows up in one place: FormatMessage. The folks who were designing Win32 decided that plain string resources weren&#8217;t fancy enough, so they invented message tables, which is just another way of storing strings in resources. Why string resources weren&#8217;t good [&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-23263","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A common mistake I see is people confusing message tables with string resources. This basically shows up in one place: FormatMessage. The folks who were designing Win32 decided that plain string resources weren&#8217;t fancy enough, so they invented message tables, which is just another way of storing strings in resources. Why string resources weren&#8217;t good [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/23263","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=23263"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/23263\/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=23263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=23263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=23263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}