1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

FormatMessage() with WSAGetLastError()

Discussion in 'MFC' started by RipRage, Aug 28, 2010.

  1. RipRage

    RipRage New Member

    Joined:
    Aug 28, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi Experts
    I am trying to print a error string using FormatMessage(), ive been at this for a while now, and cant seem to solve it. it prints the error code, but not the string, instead it ouputs "N"? if you can help with this i will be very grateful..
    Code:
     	void handle_error(const char *msg)
    {
            LPVOID lpMsgBuf;
            int e;
    
            lpMsgBuf = (LPVOID)"Unknown error";
            e = WSAGetLastError();
            if (FormatMessage(
                            FORMAT_MESSAGE_ALLOCATE_BUFFER |
                            FORMAT_MESSAGE_FROM_SYSTEM |
                            FORMAT_MESSAGE_IGNORE_INSERTS,
                            NULL, e,
                            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                                    // Default language
                            (LPTSTR)&lpMsgBuf, 0, NULL)) {
                    fprintf(stderr, "%s: Error %d: %s\n", msg, e, lpMsgBuf);
                    LocalFree(lpMsgBuf);
            } else
                    fprintf(stderr, "%s: Error %d\n", msg, e);
    } 
    Output:
    Code:
    Log: Failed to connect!: Error 10061: N
     

Share This Page