Menu

FormatMessageA(...FORMAT_MESSAGE_ALLOCATE_BUFFER..) seems not to be understood

2024-12-11
2024-12-12
  • Martin Poupě

    Martin Poupě - 2024-12-11

    Hello,
    have routine:

    static void PrintFailed(const char *pszText)
    {
        DWORD dwLastError = GetLastError();
        char *pszErrorMessage = NULL;
        const DWORD dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK;
        FormatMessageA(dwFlags,NULL, dwLastError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),(LPSTR)&pszErrorMessage, 0, NULL);
        fprintf(stderr,"%s failed. Error: %lu (%s)\n", pszText,dwLastError,pszErrorMessage ? pszErrorMessage : "?");
        LocalFree(pszErrorMessage);
    }
    

    cppcheck 2.16 says:

    tmp.c:12:72: style: Condition 'pszErrorMessage' is always false [knownConditionTrueFalse]
        fprintf(stderr,"%s failed. Error: %lu (%s)\n", pszText,dwLastError,pszErrorMessage ? pszErrorMessage : "?");
                                                                           ^
    tmp.c:9:29: note: Assignment 'pszErrorMessage=NULL', assigned value is 0
        char *pszErrorMessage = NULL;
                                ^
    tmp.c:12:72: note: Condition 'pszErrorMessage' is always false
        fprintf(stderr,"%s failed. Error: %lu (%s)\n", pszText,dwLastError,pszErrorMessage ? pszErrorMessage : "?");
    

    Also online demo complains:
    [test.cpp:7]: (style) Condition 'pszErrorMessage' is always false
    However the pszErrorMessage variable is allocated by FormatMessageA() call.

     
  • CHR

    CHR - 2024-12-12

    You should pass --library=windows, since windows.cfg has configuration for FormatMessageA.

     
  • Martin Poupě

    Martin Poupě - 2024-12-12

    Yes, it works now:-) Thank you for help.:-)

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.