Link to home
Start Free TrialLog in
Avatar of zorker
zorker

asked on

Error codes for socket

I used GetLastError on a socket and it gives me a number.  Now I want to match the numbers with
the msgs like WSANETDOWN and WSANOTCONN, but it says they're undeclared.  How do I figure out
which errors correspond to the numbers that GetLastError is Spitting out?
Avatar of platiumstar
platiumstar

see MSDN article  "Turning API Errors into Basic Errors".
basically, you use either ApiRaiseIf() or the function here:

Function ApiError(ByVal e As Long) As String

    Dim s As String, c As Long

    s = String(256, 0)

    c = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM Or _

                      FORMAT_MESSAGE_IGNORE_INSERTS, _

                      pNull, e, 0&, s, Len(s), ByVal pNull)

    If c Then ApiError = Left$(s, c)

End Function

good luck.

Avatar of zorker

ASKER

Where can I find the MSDN article?
void ShowSystemError(const TCHAR* lpszTitle, const TCHAR* lpszText, DWORD dwErrorCode)
{
    const TCHAR* lpszWinError;
    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
        NULL,
        dwErrorCode,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &lpszWinError,
        1,
        NULL
      );

    CString s,s2;
    if(lpszText)
    {
        s = lpszText;
        s += "\n";
    }

    s2 = lpszWinError;
    LocalFree((HLOCAL)lpszWinError);

    s2.TrimLeft();
    s2.TrimRight();

    s += s2;

    if(AfxGetMainWnd())
    {
        MessageBox(AfxGetMainWnd()->m_hWnd, s, lpszTitle,
            MB_OK | MB_ICONERROR | MB_DEFBUTTON1 | MB_APPLMODAL | MB_TOPMOST);
    }
    else
    {
        MessageBox(NULL, s, lpszTitle,
            MB_OK | MB_ICONERROR | MB_DEFBUTTON1 | MB_APPLMODAL | MB_TOPMOST);
    }
}


Use the function this way:

void ShowSystemError("Title Of Dialog", "An error occured:",GetLastError());
Avatar of zorker

ASKER

I tried it, but it gave me a dialog box that said:
"An Error occurred." without the name of the app or the error type.

This is the code I used to call ShowSysErr:
ShowSystemError("IPSerial", "An Error Occurred: ", GSocket.GetLastError());
Where GSocket is the CAsyncSocket.
Avatar of zorker

ASKER

I tried it, but it gave me a dialog box that said:
"An Error occurred." without the name of the app or the error type.

This is the code I used to call ShowSysErr:
ShowSystemError("IPSerial", "An Error Occurred: ", GSocket.GetLastError());
Where GSocket is the CAsyncSocket.
Avatar of zorker

ASKER

I tried it, but it gave me a dialog box that said:
"An Error occurred." without the name of the app or the error type.

This is the code I used to call ShowSysErr:
ShowSystemError("IPSerial", "An Error Occurred: ", GSocket.GetLastError());
Where GSocket is the CAsyncSocket.
ASKER CERTIFIED SOLUTION
Avatar of zingo
zingo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial