title: How does WSAStartup function initiates use of the Winsock DLL?
body: <p>How does WSAStartup function initiates use of the Winsock DLL?</p>
  
  <p>According to the documentation</p>
  
  <blockquote>
    <p>The WSAStartup function must be the
    first Windows Sockets function called
    by an application or DLL. It allows an
    application or DLL to specify the
    version of Windows Sockets required
    and retrieve details of the specific
    Windows Sockets implementation. The
    application or DLL can only issue
    further Windows Sockets functions
    after successfully calling WSAStartup.</p>
  </blockquote>
  
  <p>This function initializes <code>WSADATA</code> data structure, but in socket programming we don't pass <code>WSDATA</code> to any function so how does the program comes to know about the Windows Sockets version and other details?</p>
  
  <p><strong>For example in this code</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;winsock2.h&gt;
  #pragma comment(lib, "ws2_32")
  
  void Run(int argc, char* argv[])
  {
      char* host = argc &lt; 2 ? "" : argv[1];
      struct hostent* entry = gethostbyname(host);
  
      if(entry)
      {
          struct in_addr* addr = (struct in_addr*) entry-&gt;h_addr;
          printf("IP Address: %s\n", inet_ntoa(*addr));
      }
      else
          printf("ERROR: Resolution failure.\n");
  }
  
  int main(int argc, char* argv[])
  {
      WSADATA wsaData;
  
      if(WSAStartup(0x202, &amp;wsaData) == 0)
      {
          Run(argc, argv);
          WSACleanup();
      }
      else
          printf("ERROR: Initialization failure.\n");
  }
  </code></pre>
  
  <p>In this example I am initializing <code>WSADATA</code> data structure using <code>WSAStartup()</code> function and after wards I'm not passing <code>wsaData</code> anywhere.</p>
  
  <p>So how does my program comes to know about <code>wsaData</code> details?</p>
  
  <p>Thanks.</p>