FormatErrorMsg

Returns the system description for the specified error code.

Archived content: This source code is currently inactive and may be outdated or no longer maintained or functional.

Wenn ein Fehler auftritt, gibt es mit der GetLastError-Funktion eine entsprechende Nummer. Deren Bedeutung ist aber kein bisschen offensichtlich und sie so dem Benutzer zu präsentieren macht ihn auch nicht glücklicher. Für diesen Zweck lässt sich in Windows zu jeder Fehlernummer eine Beschreibung abfragen, die die Umstände des Fehlers ein wenig erklärt. Sogar in der Sprache, in der das Betriebssystem installiert oder eingestellt ist. Da das in C++ aber etwas umständlich ist, gibt es hier den Code, der das alles erledigt.

Note on the code quality: The content on this page is possibly a bit outdated. I’m not using C++ anymore for quite some time, but I would like to keep the existing and once useful functions available.

Download

FormatErrorMsg.cpp528 BQuelltext der FormatErrorMsg-Funktion

Contents of the file FormatErrorMsg.cpp:

// Returns the system description for the specified error code.
//
// Dependencies: CString
//
// Web: http://unclassified.software/source/formaterrormsg
//
CString FormatErrorMsg(DWORD dwErr)
{
    LPVOID lpMsgBuf;
    CString msg;

    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER |
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        dwErr,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),   // Default language
        (LPTSTR) &lpMsgBuf,
        0,
        NULL
    );
    msg = (char *) lpMsgBuf;
    LocalFree(lpMsgBuf);
    return msg;
}

Licence and terms of use

This software is freely available as source code and compiled version, without restrictions (“public domain”). There is no warranty, not even for merchantability or fitness for a particular purpose. I am not liable for any damage caused through appropriate or inappropriate use.

Statistic data

  • Created on 2007-04-02.