Home > OS >  FormatMessage returning error code 8 (ERROR_NOT_ENOUGH_MEMORY)
FormatMessage returning error code 8 (ERROR_NOT_ENOUGH_MEMORY)

Time:10-23

I'm trying to print the last message from WinApi to the console. Sounds simple, or so I thought. I made this simple enough function, but I'm getting an error code 8 (ERROR_NOT_ENOUGH_MEMORY). I'm not sure how this could be. I didn't find anything online either. Here's what I've got:

static inline void printLastMessage()
{
    LPTSTR lpMsgBuf; 
    if(FormatMessage((FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS), NULL,
                 GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), lpMsgBuf, 0, NULL) == 0)
    {
        printf("Error Code: %d\n", GetLastError());
    }
    wprintf(L"Error: %s\n", lpMsgBuf);
    LocalFree(lpMsgBuf);
}

The resulting buffer looks like this in the console:

Error: (null)

I've also tried FormatMessageW, but it didn't make a difference. (And it seems like FormatMessage is defined as FormatMessageW with UNICODE)

I'm using MinGW's GCC with C11, Windows SDK 10.0.22621.

Edit: Updated (LPTSTR)&lpMsgBuf. Current code:

static inline void printLastMessage()
{
    LPTSTR lpMsgBuf; 
    DWORD err = GetLastError();
    printf("Error code: %d\n", err);
    if(FormatMessage((FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS), NULL,
                 err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL) == 0)
    {
        printf("Error Code (FormatMessage): %d\n", GetLastError());
    }
    wprintf(L"Error: %s\n", lpMsgBuf);
    LocalFree(lpMsgBuf);
}

And output:

Error code: 87
Error: 

CodePudding user response:

I'm using MinGW's GCC with C11, Windows SDK 10.0.22621.

That doesn't make sense. MinGW comes with it's own Win32 API libraries. You shouldn't mix that with Windows SDK. This may result in undefined behaviour.

Build your code purely with MinGW and stay away from Windows SDK, or use MSVC with Windows SDK.

I tried your code with MinGW-w64 from https://winlibs.com/ and see no issue. Here's how I made your code into a standalone program test.cc:

#include <windows.h>
#include <stdio.h>

static inline void printLastMessage()
{
    LPTSTR lpMsgBuf; 
    DWORD err = GetLastError();
    printf("Error code: %d\n", err);
    if(FormatMessage((FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS), NULL,
                 err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL) == 0)
    {
        printf("Error Code (FormatMessage): %d\n", GetLastError());
    }
    wprintf(L"Error: %s\n", lpMsgBuf);
    LocalFree(lpMsgBuf);
}

int main ()
{
    printLastMessage();
    return 0;
}

Then I built the code like this:

g   test.cc

After running the resulting a.exe I get:

Error code: 126
Error: The specified module could not be found.

So it looks like your code is working, you just need to build it in pure MinGW/MinGW-w64.

CodePudding user response:

As it turns out, it was a compiler issue. Changing from MinGW from https://www.mingw-w64.org/ to the version over at https://winlibs.com/ makes the exact same code run just fine...

  • Related