<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://codehq.net/cs/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>The Emission Locus : FormatMessage</title><link>http://codehq.net/cs/blogs/john/archive/tags/FormatMessage/default.aspx</link><description>Tags: FormatMessage</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.2)</generator><item><title>Workaround for bug in FormatMessage API</title><link>http://codehq.net/cs/blogs/john/archive/2005/11/28/workaround-for-bug-in-formatmessage-api.aspx</link><pubDate>Mon, 28 Nov 2005 13:59:00 GMT</pubDate><guid isPermaLink="false">0d78e8a6-5d58-4271-ba38-c33afed2bfdd:8</guid><dc:creator>john</dc:creator><slash:comments>0</slash:comments><comments>http://codehq.net/cs/blogs/john/comments/8.aspx</comments><wfw:commentRss>http://codehq.net/cs/blogs/john/commentrss.aspx?PostID=8</wfw:commentRss><wfw:comment>http://codehq.net/cs/blogs/john/rsscomments.aspx?PostID=8</wfw:comment><description>&lt;P&gt;The FormatMessage API appears to have a bug that causes its behaviour to not match its documentation.&lt;/P&gt;
&lt;P&gt;This occurs when the specified module does not have a MESSAGETABLE resource and both the FORMAT_MESSAGE_FROM_HMODULE and FORMAT_MESSAGE_FROM_SYSTEM flags are specified. Instead of continuing the search in the system modules the API fails and GetLastError() returns ERROR_RESOURCE_TYPE_NOT_FOUND (1813).&lt;/P&gt;
&lt;P&gt;Here's the workaround:&lt;/P&gt;&lt;PRE&gt;HRESULT hr= E_ACCESSDENIED; //or whatever
_bstr_t rv; //for the error text

&lt;FONT color=#008000&gt;//
//NOTE: FormatMessage will fail if the FORMAT_MESSAGE_FROM_HMODULE
//      flag is specified and the module does not have a MESSAGETABLE
//      resource, even if FORMAT_MESSAGE_FROM_SYSTEM is specified!
//
&lt;/FONT&gt;&lt;FONT color=#000000&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;const &lt;/FONT&gt;&lt;FONT color=#000000&gt;DWORD moduleFlags= (  FORMAT_MESSAGE_ALLOCATE_BUFFER  |
                            FORMAT_MESSAGE_FROM_HMODULE     |
                            FORMAT_MESSAGE_MAX_WIDTH_MASK);

&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;const &lt;/FONT&gt;&lt;FONT color=#000000&gt;DWORD systemFlags= (  FORMAT_MESSAGE_ALLOCATE_BUFFER  |
                            FORMAT_MESSAGE_FROM_SYSTEM      |
                            FORMAT_MESSAGE_MAX_WIDTH_MASK);

CHeapPtr&amp;lt;WCHAR, CLocalAllocator&amp;gt; msg;
&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;if &lt;/FONT&gt;&lt;FONT color=#000000&gt;(!::FormatMessage(moduleFlags, GetModuleHandle(&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;NULL&lt;/FONT&gt;&lt;FONT color=#000000&gt;),
        (DWORD)errorCode, &lt;/FONT&gt;&lt;FONT color=#ff0000&gt;0&lt;/FONT&gt;&lt;FONT color=#000000&gt;, (LPWSTR)&amp;amp;msg, &lt;/FONT&gt;&lt;FONT color=#ff0000&gt;0&lt;/FONT&gt;&lt;FONT color=#000000&gt;, &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;NULL&lt;/FONT&gt;&lt;FONT color=#000000&gt;))
{
    ::FormatMessage(systemFlags, &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;NULL&lt;/FONT&gt;&lt;FONT color=#000000&gt;, (DWORD)errorCode, &lt;/FONT&gt;&lt;FONT color=#ff0000&gt;0&lt;/FONT&gt;&lt;FONT color=#000000&gt;,
            (LPWSTR)&amp;amp;msg, &lt;/FONT&gt;&lt;FONT color=#ff0000&gt;0&lt;/FONT&gt;&lt;FONT color=#000000&gt;, &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;NULL&lt;/FONT&gt;&lt;FONT color=#000000&gt;);
}

rv= msg;
&lt;/FONT&gt;
&lt;/PRE&gt;
&lt;P&gt;Note: the code uses the ATL CHeapPtr and CLocalAllocator classes.&lt;/P&gt;&lt;img src="http://codehq.net/cs/aggbug.aspx?PostID=8" width="1" height="1"&gt;</description><category domain="http://codehq.net/cs/blogs/john/archive/tags/Code/default.aspx">Code</category><category domain="http://codehq.net/cs/blogs/john/archive/tags/FormatMessage/default.aspx">FormatMessage</category><category domain="http://codehq.net/cs/blogs/john/archive/tags/Workaround/default.aspx">Workaround</category></item></channel></rss>