Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
536 views
in Technique[技术] by (71.8m points)

c - Why is FormatMessage() failing to find a message for WinINet errors?

I'm running this to test FormatMessage:

LPVOID lpMsgBuf;
errCode=12163;

FormatMessage(
    FORMAT_MESSAGE_ALLOCATE_BUFFER | 
    FORMAT_MESSAGE_FROM_SYSTEM ,
    0,
    errCode,
    0,
    (LPTSTR) &lpMsgBuf,
    0, NULL );

However, when it returns lpMsgBuf contains NULL... I was expecting something like ERROR_INTERNET_DISCONNECTED.

Anything look wrong? Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

That's a WinINet error, and so the message associated with it lives in WinINet.dll. You just need to tell FormatMessage() about this in order for it to retrieve the correct message:

FormatMessage( 
   // flags:
   FORMAT_MESSAGE_ALLOCATE_BUFFER  // allocate buffer (free with LocalFree())
   | FORMAT_MESSAGE_IGNORE_INSERTS // don't process inserts
   | FORMAT_MESSAGE_FROM_HMODULE,  // retrieve message from specified DLL
   // module to retrieve message text from
   GetModuleHandle(_T("wininet.dll")),
   // error code to look up
   errCode,
   // default language
   0, 
   // address of location to hold pointer to allocated buffer
   (LPTSTR)&lpMsgBuf, 
   // no minimum size
   0, 
   // no arguments
   NULL );

This is officially documented on MSDN under the "Handling Errors" section of the WinINet documentation.

Note that you can add the FORMAT_MESSAGE_FROM_SYSTEM flag back in if you want to use this routine for errors that may or may not have come from WinINet: with that flag in place, FormatMessage() will fall back on the system message table if the error isn't found in wininet.dll. However, do not remove the FORMAT_MESSAGE_IGNORE_INSERTS flag.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...