Skip navigation

Winsock error codes on msdn

Besides going and looking it up on MSDN, how can I get the winsock error codes in my C++ program?

#ifdef UNICODE
#undef UNICODE
#endif

#include <stdio.h>
#include <stdlib.h>

#include <winsock2.h>
#include <windows.h>

#pragma comment(lib, "ws2_32.lib")


int main()
{
  // Here I am purposefully messing up:  I didn't call WSAStartup()!  Oh no!
  // whatever will winsock do?
  int sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ) ;

  if( sock == SOCKET_ERROR )
  {
    // Uh oh!  Something bad happened.  Let's
    // get the error code...
    int errCode = WSAGetLastError();

    // ..and the human readable error string!!
    // Interesting:  Also retrievable by net helpmsg 10060
    LPSTR errString = NULL;  // will be allocated and filled by FormatMessage
  
    int size = FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
                 FORMAT_MESSAGE_FROM_SYSTEM, // use windows internal message table
                 0,       // 0 since source is internal message table
                 errCode, // this is the error code returned by WSAGetLastError()
                          // Could just as well have been an error code from generic
                          // Windows errors from GetLastError()
                 0,       // auto-determine language to use
                 (LPSTR)&errString, // this is WHERE we want FormatMessage
                                    // to plunk the error string.  Note the
                                    // peculiar pass format:  Even though
                                    // errString is already a pointer, we
                                    // pass &errString (which is really type LPSTR* now)
                                    // and then CAST IT to (LPSTR).  This is a really weird
                                    // trip up.. but its how they do it on msdn:
                                    // http://msdn.microsoft.com/en-us/library/ms679351(VS.85).aspx
                 0,                 // min size for buffer
                 0 );               // 0, since getting message from system tables
     printf( "Error code %d:  %s\n\nMessage was %d bytes, in case you cared to know this.\n\n", errCode, errString, size ) ;

     LocalFree( errString ) ; // if you don't do this, you will get an
     // ever so slight memory leak, since we asked
     // FormatMessage to FORMAT_MESSAGE_ALLOCATE_BUFFER,
     // and it does so using LocalAlloc
     // Gotcha!  I guess.


  }

}

About these ads

One Trackback/Pingback

  1. […] Dans mon présent article, je souhaite mettre en avant un point qui n’est sûrement pas écrit assez gros dans le tutoriel : si les fonctions de manipulation des sockets sont les mêmes, la gestion des erreurs est différente ! En effet, les Unixoïdes positionnent correctement la variable magique errno et on peut donc utiliser perror() et strerror() pour avoir un message lisible décrivant l’erreur. Mais Windows ne positionne pas errno ! Le vilain ! Il faut alors utiliser la fonction WSAGetLastError() pour obtenir un code équivalent à errno et utiliser la fonction FormatMessage() pour obtenir un message lisible. […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 43 other followers

%d bloggers like this: