3

The following errors are from a file with just windows and winsock2 included.

C:\Users\ioil\Desktop\dm\bin>dmc sockit.c
typedef struct fd_set {
                      ^
C:\Users\ioil\Desktop\dm\bin\..\include\win32\WINSOCK2.H(85) : Error: 'fd_set' is already defined
} fd_set;
^
C:\Users\ioil\Desktop\dm\bin\..\include\win32\WINSOCK2.H(88) : Error: identifier or '( declarator )' expected
struct timeval {
               ^
C:\Users\ioil\Desktop\dm\bin\..\include\win32\WINSOCK2.H(129) : Error: 'timeval' is already defined
};
^
C:\Users\ioil\Desktop\dm\bin\..\include\win32\WINSOCK2.H(132) : Error: identifier or '( declarator )' expected
struct  hostent {
                ^
C:\Users\ioil\Desktop\dm\bin\..\include\win32\WINSOCK2.H(185) : Error: 'hostent' is already defined
Fatal error: too many errors
--- errorlevel 1

C:\Users\ioil\Desktop\dm\bin>

What's already been tried : placing the winsock.dll file in the same directory as the compiler and program to be compiled, placing it in the system32 directory, and entering it in the registry with the regsrv32 command. Don't really know where to go from here, appreciate any advice . . .

mtvec
  • 17,846
  • 5
  • 52
  • 83
ioil
  • 101
  • 2
  • 3
  • 1
    It won't be anything to do with your DLL. Can you post your code? – Anthony Jun 17 '10 at 05:46
  • 2
    Try including winsock2.h before windows.h – INS Jun 17 '10 at 07:18
  • Don't move winsock.dll. It's part of Windows. Any advice you may have found recommending that as a solution dates back to the days of Windows 3.1. For that matter, so does winsock.dll -- dates from Windows 3.1, I mean; on 32-bit platforms, you use wsock32.dll (Winsock 1.1) or ws2_32.dll (Winsock 2.x). And as Duracell says, your problem is something else anyway. – Warren Young Jun 17 '10 at 19:34

3 Answers3

2

windows.h includes winsock.h, which collides with the winsock2.h include file. prevent the first inclusion by defining WINSOCKAPI before you include windows.h:

:

#define _WINSOCKAPI_ 
#include "windows.h"
#include "winsock2.h"
Alon
  • 4,862
  • 19
  • 27
  • Thanks very much. Changing the order of the header includes fixed the compile error. Again, thank you. ioil – ioil Jun 18 '10 at 08:50
2

You should place the winsock2.h before the windows.h as suggested by Iulian Şerbănoiu

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

You could also use the lean and mean macro:

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

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

The reason is described on msdn: Creating a Basic Winsock Application

Also make sure that you link your program to the WS2_32.lib file (this is depending on what IDE you are using, for instance Visual Studio?)
In Visual Studio you go to Project > Properties > Linker > Additional includes (or something like that, not at a computer with visual studio at the moment..) This is also described in the link above.

default
  • 11,485
  • 9
  • 66
  • 102
1
#pragma comment(lib, "wininet.lib")
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")

use this avoid compilation errors

McDowell
  • 107,573
  • 31
  • 204
  • 267
khader
  • 11
  • 1