The issue is rather simple when I try to compile a DLL with winsock code it produces a huge number of errors. This does not happen when it is compiled as a console app and reason as to why are beyond me. If anyone has advice please give it.
A small fraction of them below
i:\software stuff\c++ code\ircclientgui\ircclientbackend\code.h(17): error C2079
: 'theClient' uses undefined struct 'sockaddr_in'
i:\software stuff\c++ code\ircclientgui\ircclientbackend\code.h(19): error C2079
: 'theWSA' uses undefined struct 'WSAData'
i:\software stuff\c++ code\ircclientgui\ircclientbackend\code.h(36): error C2568
: '=': unable to resolve function overload
i:\software stuff\c++ code\ircclientgui\ircclientbackend\code.h(36): note: could
be 'SOCKET socket(int,int,int)'
i:\software stuff\c++ code\ircclientgui\ircclientbackend\code.h(41): error C2228
: left of '.sin_addr' must have class/struct/union
i:\software stuff\c++ code\ircclientgui\ircclientbackend\code.h(41): note: type
is 'int'
i:\software stuff\c++ code\ircclientgui\ircclientbackend\code.h(41): error C2228
: left of '.S_un' must have class/struct/union
i:\software stuff\c++ code\ircclientgui\ircclientbackend\code.h(41): error C2228
: left of '.S_addr' must have class/struct/union
i:\software stuff\c++ code\ircclientgui\ircclientbackend\code.h(42): error C2228
: left of '.sin_family' must have class/struct/union
i:\software stuff\c++ code\ircclientgui\ircclientbackend\code.h(42): note: type
is 'int'
i:\software stuff\c++ code\ircclientgui\ircclientbackend\code.h(43): error C2228
: left of '.sin_port' must have class/struct/union
i:\software stuff\c++ code\ircclientgui\ircclientbackend\code.h(43): note: type
is 'int'
The code in question
#ifndef _code_h_
#define _code_h_
#include <Windows.h>
#include <Winsock2.h>
#include <WS2tcpip.h>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <sstream>
#include <thread>
#include <chrono>
#include <future>
namespace classClient
{
struct sockaddr_in theClient;
char *buffer = new char[1002];
WSADATA theWSA;
SOCKET theSocket,theOtherSocket;
DWORD WINAPI ThreadProc(__in LPVOID lpParameter)
{
char *in = new char[1002];
SOCKET AcceptSocket=(SOCKET) lpParameter;
while(true)
{
recv(AcceptSocket,in,1000,0);
}
return 0;
}
void makeSocketLoad()
{
WSAStartup(MAKEWORD(2,2),&theWSA);
theSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
}
void sockConnect(std::string ip,int port)
{
theClient.sin_addr.s_addr = inet_addr(ip.c_str());
theClient.sin_family = AF_INET;
theClient.sin_port = htons(port);
if(connect(theSocket,(SOCKADDR*)(&theClient),sizeof(theClient))!=0)
{
WSACleanup();
system("PAUSE");
}
}
void sendData(std::string data)
{
data[data.length()+1] = '\0';
strcpy(buffer,data.c_str());
send(theSocket,buffer,1000,0);
}
void getData2()
{
DWORD dwThread;
HANDLE hThread = CreateThread(NULL,0,ThreadProc,(LPVOID)theSocket,0,&dwThread);
CloseHandle(hThread);
}
void getData()
{
auto go = std::async (getData2);
go.get();
}
void killWinsock()
{
closesocket(theSocket);
WSACleanup();
}
}
#endif
The full code if needed puu.sh/oqyle/ef30e7c575.zip
What I have tried:
Moving the includes around in accordance with other threads made on this subject.