WinSock2.h Compilation Errors - c++

I am trying to run a project using Visual studio 2003. But I am getting lot of compilation errors similar to the following.
The errors are pointing to WinSock2.h file. I am copying couple of code snippets from WinSock2.h file and the corresponding errors
typedef struct fd_set {
u_int fd_count; /* how many are SET? */
SOCKET fd_array[FD_SETSIZE]; /* an array of SOCKETs */
} fd_set;
C:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\PlatformSDK\Include\WinSock2.h(114): error C2065: 'fd_set' :
undeclared identifier
struct sockaddr {
u_short sa_family; /* address family */
char sa_data[14]; /* up to 14 bytes of direct address */
};
C:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\PlatformSDK\Include\WinSock2.h(109): error C2143: syntax
error : missing ';' before '{'
The ws2_32.lib file is added to "Configuration properties - Linker - Input - Additional Dependencies". The build configuration platform is win32.
Thanks in advance for your help.

a typical basic Winsock Application with the good order of header files can be found here:
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h>
#pragma comment(lib, "Ws2_32.lib")
int main() {
return 0;
}
The order of including header files is important

Related

compiling a win32 c++ console application in visual studio 2015 [duplicate]

I am using Visual Studio 2005. I created an MFC based console application named "StdAfx dependancy". The IDE created the following files for me.
Resource.h
StdAfx Dependancy.h
stdafx.h
StdAfx Dependancy.cpp
stdafx.cpp
I added another class CHelper with Helper.h and Helper.cpp as below.
Helper.h:
#pragma once
class CHelper
{
public:
CHelper(void);
~CHelper(void);
};
Helper.cpp
#include "StdAfx.h"
#include "Helper.h"
CHelper::CHelper(void)
{
}
CHelper::~CHelper(void)
{
}
I created an object for CHelper in the main function; to achieve this I added Header.h file in the first line of StdAfx Dependancy.cpp as below; and I got the following errors.
d:\codes\stdafx dependancy\stdafx
dependancy\stdafx dependancy.cpp(33) :
error C2065: 'CHelper' : undeclared
identifier
d:\codes\stdafx
dependancy\stdafx dependancy\stdafx
dependancy.cpp(33) : error C2146:
syntax error : missing ';' before
identifier 'myHelper'
d:\codes\stdafx
dependancy\stdafx dependancy\stdafx
dependancy.cpp(33) : error C2065:
'myHelper' : undeclared identifier
But when I include it after stdafx.h, the error vanishes. Why?
// Stdafx dependancy.cpp : Defines the entry point for the console application.
//
#include "Helper.h"
#include "stdafx.h"
#include "Stdafx dependancy.h"
// #include "Helper.h" --> If I include it here, there is no compilation error
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// The one and only application object
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
CHelper myHelper;
}
return nRetCode;
}
This link must give you some clue.
Purpose of stdafx.h
The lines defined before the
#include "stdafx.h" are ignored by the compiler. So if you want to actually include those files then you need to include them after the #include "stdafx.h".
Hope it is clear.
In addition to ckv's answer, it makes little sense to include header files before "stdafx.h" as any non-trivial header file will contain framework types that are included there (e.g. any MFC types).

Why do I get a flood of compiler errors when I include WinSock2.h? [duplicate]

This question already has answers here:
C++ Redefinition Header Files (winsock2.h)
(15 answers)
Closed 6 years ago.
I'm trying to make a UDP Flood using WinSock2.h in c++ but I'm getting over 70 errors and 17 warnings on just WinSock2.h and all the errors are redefinitions, syntax errors from ws2def.h, and "different linkages". Am I doing something wrong or is this a problem with WinSock2? If it is of any use I am using 64 bit Windows 10, Visual Studio 2015
#include "stdafx.h"
#include <WinSock2.h>
#include <windows.h>
#include <fstream>
#include <time.h>
#include "wtypes.h"
#include "Functions.h"
#pragma comment(lib, "ws2_32.lib")
//Get IP
cin.getline(TargetIP, 17);
//Get IP
cout << "Enter the Port: ";
cin >> nPort;
cout << endl;
//Initialize WinSock 2.2
WSAStartup(MAKEWORD(2, 2), &wsaData);
//Create our UDP Socket
s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
//Setup the target address
targetAddr.sin_family = AF_INET;
targetAddr.sin_port = htons(nPort);
targetAddr.sin_addr.s_addr = inet_addr(TargetIP);
//Get input from user
cout << "Please specify the buffer size:";
cin >> bufferSize;
//Create our buffer
char * buffer = new char[bufferSize];
while(true){
//send the buffer to target
sendto(s, buffer, strlen(buffer), NULL, (sockaddr *)&targetAddr, sizeof(targetAddr));
}
//Close Socket
closesocket(s);
//Cleanup WSA
WSACleanup();
//Cleanup our buffer (prevent memory leak)
delete[]buffer;
I guess you may have a problem in the order of inclusions.
You are probably getting many errors along the lines of:
1>c:\program files (x86)\windows kits\8.1\include\um\winsock2.h(2373): error C2375: 'WSAStartup': redefinition; different linkage
1> c:\program files (x86)\windows kits\8.1\include\um\winsock.h(867): note: see declaration of 'WSAStartup'
That's because <windows.h> includes <winsock.h> by default, and <winsock.h> provides many declarations that overlap with those in <winsock2.h>, which causes errors when <winsock2.h> is included after <windows.h>.
So, you may want to include <winsock2.h> before <windows.h>:
#include <winsock2.h>
#include <windows.h>
Or, as an alternative, you may try to define _WINSOCKAPI_ to prevent the inclusion of <winsock.h> in <windows.h> with this preprocessor #undef-#define-#include "dance":
#undef _WINSOCKAPI_
#define _WINSOCKAPI_ /* prevents <winsock.h> inclusion by <windows.h> */
#include <windows.h>
#include <winsock2.h>
I have to say that the definition of _WINSOCKAPI_ macro to interfere in the ordinary header inclusion guard mechanics to prevent <windows.h> to include <winsock.h> sounds like an implementation-details-based fragile "hack", so I would probably prefer the first option.
But all in all this order of inclusion bug sounds to me like a bug in the Win32's headers, so the best thing would be for Microsoft to fix that.
EDIT
As suggested in the comments, a further alternative may be to #define WIN32_LEAN_AND_MEAN before including <windows.h>. However, please note that this would prevent the inclusions of other Windows headers as well.
P.S.
If you are using precompiled headers ("stdafx.h" in the newly showed code in your question), you may want to pay attention to order of inclusions in there as well.

NtCurrentTeb() on C++ Rad Studio 10

I've made this declaration in my include file:
__inline struct _PEB * NtCurrentPeb() { return NtCurrentTeb()->ProcessEnvironmentBlock; }
Leading the file also includes these values:
#include <stdio.h>
#include <windows.h>
#include <WinNT.h>
#pragma comment(lib, "ntdll.lib")
But the compiler (Rad Studio XE 10) gives me this error:
Use of undeclared identifier 'NtCurrentTeb'
Where am I wrong? What is missing?

C++ code that look perfectly fine causes error

I ran this code on Visual studio Professional 2013 and it worked, but it doesn't work on Visual Studio Express 2013.
#include "cli_tcp.h"
cli_tcp::cli_tcp()
// CLIENT TCP PROGRAM
// Revised and tidied up by
// J.W. Atwood
// 1999 June 30
char* getmessage(char *);
/* send and receive codes between client and server */
/* This is your basic WINSOCK shell */
#pragma comment( linker, "/defaultlib:ws2_32.lib" )
#include <winsock2.h>
#include <ws2tcpip.h>
#include <winsock.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <windows.h>
Error 1 error C2144: syntax error : 'char' should be preceded by ';'
Error 2 error C2761: '{ctor}' : member function redeclaration not allowed
3 IntelliSense: expected a '{'
I didn't post the entire code, because it was irrelevant. The only line that seems to be wrong is: char* getmessage(char *);
You are simply not doing what you should when you state "cli_tcp::cli_tcp()".
The compiler is expecting you to provide the implementation for the constructor of the class, and you are not providing anything.
this
cli_tcp::cli_tcp()
should be
cli_tcp::cli_tcp() { /*code*/ }
or else it won't work anywhere.

timeBeginPeriod identifier not found error

When I compile my project I get this error.
C:\src\libs\nvrtpaudio\FileRtpSource.
cpp(61) : error C3861: 'timeBeginPeriod': identifier not found
C:\src\libs\nvrtpaudio\FileRtpSource.
cpp(71) : error C3861: 'timeEndPeriod': identifier not found
gmake[5]: *** [_out/win7_x86_debug/FileRtpSource.obj] Error 2
I included windows.h but this error still persists. Anyone know how to resolve this?
MSDN says:
Header: Mmsystem.h (include Windows.h)
So you are expected to include "windows.h" and be fine, but what MSDN does not say is that this assumes you don't have WIN32_LEAN_AND_MEAN defined, which - when defined, and this also can be the case with project created from template - exlcudes "mmsystem.h" you need.
So you have to either make sure you don't have WIN32_LEAN_AND_MEAN in your project, or otherwise include directly:
#include "stdafx.h"
#include <mmsystem.h> // <<--- Here we go
#pragma comment(lib, "winmm.lib")
int _tmain(int argc, _TCHAR* argv[])
{
timeBeginPeriod(0);
return 0;
}