Partager via


Winsock Header Weirdness

There are two main Winsock headers used depending on which version your application needs. There is winsock.h for Winsock v1.x and winsock2.h for Winsock v2.x and they are mutually exclusive. That is, if you include both header files you will see a lot of compilation errors.

 As you may know, windows.h includes a lot of other header files if you don't define WIN32_LEAN_AND_MEAN in your source code. One of the headers it does include without this define is winsock.h. This can cause problems if you are intending to use Winsock 2 features and are linking to ws2_32.lib. In addition to causing obvious compilation errors (due to structure redefinitions or missing definitions) there is a subtle issue regarding IP multicast support. Winsock 1 defined the IP multicast socket options IP_ADD_MEMBERSHIP and IP_DROP_MEMBERSHIP as 5 and 6; however, when Winsock 2 was defined these values changed to 12 and 13. By including winsock.h and linking to wsock32.lib, these values are fixed up since the Windows operating system will only recognize the Winsock 2 values. By mismatching the header file and link library, the application would see failures when calling setsockopt.

Winsock developers should define WIN32_LEAN_AND_MEAN and explicitly include the Winsock header for the version they require as well as link with the appropriate lib -- winsock.h goes with wsock32.lib while winsock2.h goes with ws2_32.lib.

Comments

  • Anonymous
    December 29, 2006
    I apologize profusely, but this reply is to another post you made called "Debugging Winsock LSPs". New comments to that post are disabled, and "wndpteam" has no contact information, so this is the only way I could find to leave a reply. In that post, you wrote: Use a debugger to step through the expected Winsock calls if known (e.g. Internet Explorer would call socket, connect, select, send, and recv.) However, Internet Explorer does not seem to use any of those functions. It uses wininet, which does not appear to use winsock at all. Is there any way to get it to use winsock, so an LSP can monitor the traffic, or must it be monitored another way? Thanks, Sean

  • Anonymous
    December 29, 2006
    Wininet does use Winsock. On Vista, however, IE launches iexplore.exe and ieuser.exe. Only iexplore.exe performs any kind of networking. --Anthony Jones (AJones)