Hi,

On building a windows service, i m getting the 57 errors and 11 warnings. All the errors are about structure redefinition. Given below are some of them.
:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Include\winsock2.h(112) : error C2011: 'fd_set' : 'struct' type redefinition
C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Include\winsock2.h(147) : warning C4005: 'FD_SET' : macro redefinition
C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Include\winsock.h(88) : see previous definition of 'FD_SET'
C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Include\winsock2.h(156) : error C2011: 'timeval' : 'struct' type redefinition
C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Include\winsock2.h(212) : error C2011: 'hostent' : 'struct' type redefinition
C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Include\winsock2.h(225) : error C2011: 'netent' : 'struct' type redefinition
C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Include\winsock2.h(232) : error C2011: 'servent' : 'struct' type redefinition
C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Include\winsock2.h(244) : error C2011: 'protoent' : 'struct' type redefinition
C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Include\winsock2.h(400) : error C2011: 'sockaddr_in' : 'struct' type redefinition
C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Include\winsock2.h(410) : error C2011: 'WSAData' : 'struct' type redefinition
C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Include\winsock2.h(470) : warning C4005: 'SO_DONTLINGER' : macro redefinition
C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Include\winsock.h(411) : see previous definition of 'SO_DONTLINGER'
C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Include\winsock2.h(527) : warning C4005: 'AF_IPX' : macro redefinition
C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Include\winsock.h(469) : see previous definition of 'AF_IPX'

I have checked in the code, only winsock2.h is included. but from where winsock.h is getting included, i m not able to track. Also, windows.h is not included in the code. if i remove winsock2.h and include winsock.h then errors are thrown for the structures 'WSOVERLAPPED' used in the code.

Any pointers for the same????

Recommended Answers

All 10 Replies

IIRC, it's

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

hi,

do u mean to include windows.h after winsock2.h??
i tried that also but still the errors exist.
winsock2.h itself prevents the inclusion of winsock.h in windows.h.

i also tried using preprocessor macro WIN32_LEAN_AND_MEAN but then the complier shows error for strncpy(), wcscat() functions.

Why is the redefinition when the files are not included.

Hi,

I looked into winsock2.h where it prevents the inclusion of winsock.h in windows.h
#ifndef _WINSOCK2API_
#define _WINSOCK2API_
#define _WINSOCKAPI_ /* Prevent inclusion of winsock.h in windows.h */

then i checked windows.h. in that file it has included winsock.h without checking if WINSOCKAPI defined or not. so i modified windows.h as given below ( it should not be changed as its standard file but just a trial)
#ifndef _MAC
#include <winperf.h>
//to prevent inclusion of winsock.h. added ifndef and endif statements
#ifndef _WINSOCKAPI_
#include <winsock.h>
#endif

#endif

Now i get the followin errors.
error C2664: 'sprintf' : cannot convert parameter 1 from 'unsigned short [10]' to 'char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
XMLQuery.cpp
Communication Service\XMLQuery.cpp(2488) : error C2664: 'wcscat' : cannot convert parameter 2 from 'char [12]' to 'const unsigned short *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Communication Service\XMLQuery.cpp(2490) : error C2664: 'wcscat' : cannot convert parameter 2 from 'char [5]' to 'const unsigned short *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Communication Service\XMLQuery.cpp(2526) : error C2664: 'strncpy' : cannot convert parameter 1 from 'unsigned short [100]' to 'char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Communication Service\XMLQuery.cpp(2613) : error C2664: 'wcstok' : cannot convert parameter 2 from 'char [2]' to 'const unsigned short *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Generating Code...
Error executing cl.exe.

> error C2664: 'sprintf' : cannot convert parameter 1 from 'unsigned short [10]' to 'char *
Lemme guess, you're passing a TCHAR to sprintf ?

http://msdn2.microsoft.com/en-us/library/ybk95axf(VS.80).aspx
Scroll down to "Generic-Text Routine Mappings"
Then use the TCHAR macro name for the functions you want to use which automatically resolve to the right function name whether you're compiling with or without UNICODE.

hi,

yes you are right. iam passing TCHAR to sprintf. but i dint get what you want to say about using the macro for tchar and all..
can you please explain it more..

thanks a lot!!

Paste an example sprintf call or two, and I'll show you how to make it UNICODE friendly.

Hi,
Given below is the code snippet

BOOL CServiceControl::OpenLog()
{
TCHAR tsFileName[MAX_PATH];
SYSTEMTIME stSystemTime;
TCHAR tsNumber[10];


// Append the file and path
_tcscpy( tsFileName, m_szLogDirectory );
_tcscat( tsFileName, _T("\\") );
_tcscat( tsFileName, m_szLogFile );


// Get the System Time using the getsystem time function Get system time
// function will return the time in UTC format
GetLocalTime(&stSystemTime);


//////////////////////////////////////////////////////////////////////////////
// Append the date/time to the file name in order to create a unique file name
// for this log. Since the service is restarted at least once per day, or more
// often if it crashes or uses more than a specified memory threshold, this
// will make sure that there is a new log file for each run.
sprintf(tsNumber,"%02d",stSystemTime.wMonth);
_tcscat( tsFileName, tsNumber );
sprintf(tsNumber,"%02d",stSystemTime.wDay);
_tcscat( tsFileName, tsNumber );
sprintf(tsNumber,"%04d",stSystemTime.wYear);
_tcscat( tsFileName, tsNumber );
sprintf(tsNumber,"%02d",stSystemTime.wHour);
_tcscat( tsFileName, tsNumber );
sprintf(tsNumber,"%02d",stSystemTime.wMinute);
_tcscat( tsFileName, tsNumber );
_tcscat( tsFileName, _T(".txt") );

Well you managed to use _tcscat, so why the trouble using _stprintf ? _stprintf(tsNumber,_T("%02d"),stSystemTime.wMinute);

ya, its not giving any error for _tcscat() but for sprintf(). couldn't figure out why is it so?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.