tags:

views:

82

answers:

1

I am having a problem when arguments passed to FormatMessage are too long.

void testMessage(UINT id, ...)
{
    va_list argList;
    va_start(argList, id);

    LPTSTR buff = NULL;

    const char* str = "The following value is invalid: %1";

    DWORD success = FormatMessage(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
           str, 0, 0, (LPSTR) &buff, 0, &argList);

    if(0 == success)
    {
       DWORD err = GetLastError();

       //...
    }

    va_end(argList);

    //...
 }

 int main(int argc, char** argv)
 {
    const char* arg = NULL;

    // ...
    // Initialize arg to some big string about 33,000 bytes long.
    // ...

    test(0, arg);
 }

The error I get is ERROR_MORE_DATA (234). When I reduce the size of arg to about 32,000 bytes, the problem doesn't occur, but it's unclear whether the restriction has to do with the size of arguments passed in or the resulting total size of the string generated. The MSDN page on FormatMessage says about the lpBuffer parameter, "This buffer cannot be larger than 64K bytes."

I can easily get around this by doing a bit more error checking and putting some sane restrictions on the size of the arguments I pass into this function, but for my and others' future reference it would be great to know what the real limits are.

+1  A: 

Are you calling FormatMessageA or FormateMessageW ? If you call FormatMessageA, your 32K ASCII message will be marshalled to a 64K Unicode message. Windows today is internally Unicode, and the "A" series of functions are just wrappers around the "W" functions.

MSalters
Ah, I guess I'm calling FormatMessageA, since I don't have the UNICODE macro defined. So that sounds like an explanation. Thanks!
Owen