使用GetNetworkParams()检索网络信息时出错

时间:2018-06-26 20:09:07

标签: c++ c winapi networking

这是我用C ++编写的用于检索网络信息的代码:

#ifndef WIN_32_LEAN_AND_MEAN
#define WIN_32_LEAN_AND_MEAN
#endif

#include<windows.h>
#include<winsock2.h>
#include<iphlpapi.h>
#include<stdio.h>
#include<iostream>

#pragma comment(lib,"iphlpapi.lib")
using namespace std;

int main()
{
    FIXED_INFO *pFixedInfo;
    IP_ADDR_STRING *pIPAddr;

    ULONG ulOutBuflen;
    DWORD dwRetVal;

    pFixedInfo = (FIXED_INFO *)malloc(sizeof(FIXED_INFO));
    ulOutBuflen = sizeof(FIXED_INFO);

    if (GetNetworkParams(pFixedInfo, &ulOutBuflen) == ERROR_BUFFER_OVERFLOW) {
        free(pFixedInfo);
        pFixedInfo = (FIXED_INFO *)malloc(sizeof(ulOutBuflen));
        if (pFixedInfo == NULL)
            cout << "Error allocating memory needed to call GetNetworkParams \n";
    }

    if(dwRetVal=GetNetworkParams(pFixedInfo,&ulOutBuflen)!=NO_ERROR){
        cout << "GetNetwrokParams failed : " << dwRetVal << endl;
        if (pFixedInfo)
            free(pFixedInfo);
    }

    cout << "Host Name : " << pFixedInfo->HostName;
    cout << "Domain Name : " << pFixedInfo->DomainName;
    cout << "DNS Server s : " << endl;
    cout << pFixedInfo->DnsServerList.IpAddress.String<<endl;
    pIPAddr = pFixedInfo->DnsServerList.Next;
    while (pIPAddr)
    {
        cout << pIPAddr->IpAddress.String << endl;
        pIPAddr = pIPAddr->Next;
    }
    cout << "Node Type : ";
    switch (pFixedInfo->NodeType)
    {
        case 1:
            cout << "Broadcast " << endl;
            break;
        case 2:
            cout << "Peer to Peer " << endl;
            break;
        case 4:
            cout << "Mixed " << endl;
            break;
        case 8:
            cout << "Hybrid " << endl;
            break;
        default :
            cout << endl;
    }
    cout << "NetBios Scope ID : " << pFixedInfo->ScopeId<<endl;

    if (pFixedInfo->EnableRouting)
        cout << "Routing enabled on " << endl;
    else
        cout << "Routing enabled of" << endl;
    if (pFixedInfo->EnableProxy)
        cout << "Proxy enabled " << endl;
    else
        cout << "Proxy disabled " << endl;
    if (pFixedInfo->EnableDns)
        cout << "NETBIOS Resolution uses dns " << endl;
    else
        cout << "NETBIOS Resolution does not uses dns " << endl;

    if (pFixedInfo) {
        free(pFixedInfo);
        pFixedInfo = NULL;
    }

    return 0;
}

每次运行代码时,都会显示出很多错误。我已经从MSDN取得了代码,但是仍然无法正常工作。

有人可以建议对代码进行任何改进吗?

以下是错误:

Severity    Code    Description Project File    Line    Suppression State
Warning C4005   'IN_CLASSB': macro redefinition Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\shared\ws2def.h 578 
Error (active)  E0040   expected an identifier  Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\shared\ws2def.h 437 
Error (active)  E0040   expected an identifier  Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\shared\ws2def.h 438 
Error (active)  E0040   expected an identifier  Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\shared\ws2def.h 439 
Error (active)  E0040   expected an identifier  Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\shared\ws2def.h 446 
Error (active)  E0040   expected an identifier  Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\shared\ws2def.h 452 
Error (active)  E0040   expected an identifier  Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\shared\ws2def.h 453 
Error (active)  E0040   expected an identifier  Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\shared\ws2def.h 454 
Error (active)  E0040   expected an identifier  Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\shared\ws2def.h 470 
Error (active)  E0040   expected an identifier  Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\shared\ws2def.h 480 
Error (active)  E0040   expected an identifier  Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\shared\ws2def.h 482 
Error (active)  E0338   more than one instance of overloaded function "__WSAFDIsSet" has 'C' linkage    Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   141 
Error (active)  E0338   more than one instance of overloaded function "accept" has 'C' linkage  Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   1624    
Error (active)  E0338   more than one instance of overloaded function "bind" has 'C' linkage    Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   1646    
Error (active)  E1389   redeclaration cannot add dllexport/dllimport to "closesocket" (declared at line 749 of "c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\winsock.h")  Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   1667    
Error (active)  E0338   more than one instance of overloaded function "connect" has 'C' linkage Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   1684    
Error (active)  E1389   redeclaration cannot add dllexport/dllimport to "ioctlsocket" (declared at line 756 of "c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\winsock.h")  Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   1705    
Error (active)  E0338   more than one instance of overloaded function "getpeername" has 'C' linkage Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   1728    
Error (active)  E0338   more than one instance of overloaded function "getsockname" has 'C' linkage Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   1749    
Error (active)  E1389   redeclaration cannot add dllexport/dllimport to "getsockopt" (declared at line 771 of "c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\winsock.h")   Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   1770    
Error (active)  E1389   redeclaration cannot add dllexport/dllimport to "htonl" (declared at line 778 of "c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\winsock.h")    Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   1795    
Error (active)  E1389   redeclaration cannot add dllexport/dllimport to "htons" (declared at line 780 of "c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\winsock.h")    Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   1812    
Error (active)  E1389   redeclaration cannot add dllexport/dllimport to "inet_addr" (declared at line 782 of "c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\winsock.h")    Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   1830    
Error (active)  E1389   redeclaration cannot add dllexport/dllimport to "inet_ntoa" (declared at line 784 of "c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\winsock.h")    Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   1848    
Error (active)  E1389   redeclaration cannot add dllexport/dllimport to "listen" (declared at line 786 of "c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\winsock.h")   Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   1948    
Error (active)  E1389   redeclaration cannot add dllexport/dllimport to "ntohl" (declared at line 790 of "c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\winsock.h")    Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   1967    
Error (active)  E1389   redeclaration cannot add dllexport/dllimport to "ntohs" (declared at line 792 of "c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\winsock.h")    Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   1984    
Error (active)  E1389   redeclaration cannot add dllexport/dllimport to "recv" (declared at line 794 of "c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\winsock.h") Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   2001    
Error (active)  E0338   more than one instance of overloaded function "recvfrom" has 'C' linkage    Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   2024    
Error (active)  E0338   more than one instance of overloaded function "select" has 'C' linkage  Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   2051    
Error (active)  E1389   redeclaration cannot add dllexport/dllimport to "send" (declared at line 815 of "c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\winsock.h") Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   2076    
Error (active)  E0338   more than one instance of overloaded function "sendto" has 'C' linkage  Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   2099    
Error (active)  E1389   redeclaration cannot add dllexport/dllimport to "setsockopt" (declared at line 829 of "c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\winsock.h")   Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   2126    
Error (active)  E1389   redeclaration cannot add dllexport/dllimport to "shutdown" (declared at line 836 of "c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\winsock.h") Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   2151    
Error (active)  E1389   redeclaration cannot add dllexport/dllimport to "socket" (declared at line 840 of "c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\winsock.h")   Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   2171    
Error (active)  E0311   cannot overload functions distinguished by return type alone    Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   2196    
Error (active)  E0311   cannot overload functions distinguished by return type alone    Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   2218    
Error (active)  E1389   redeclaration cannot add dllexport/dllimport to "gethostname" (declared at line 854 of "c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\winsock.h")  Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   2235    
Error (active)  E0311   cannot overload functions distinguished by return type alone    Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   2275    
Error (active)  E0311   cannot overload functions distinguished by return type alone    Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   2294    
Error (active)  E0311   cannot overload functions distinguished by return type alone    Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   2313    
Error (active)  E0311   cannot overload functions distinguished by return type alone    Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   2330    
Error (active)  E0338   more than one instance of overloaded function "WSAStartup" has 'C' linkage  Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   2350    
Error (active)  E1389   redeclaration cannot add dllexport/dllimport to "WSACleanup" (declared at line 876 of "c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\winsock.h")   Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   2370    
Error (active)  E1389   redeclaration cannot add dllexport/dllimport to "WSASetLastError" (declared at line 878 of "c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\winsock.h")  Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   2387    
Error (active)  E1389   redeclaration cannot add dllexport/dllimport to "WSAGetLastError" (declared at line 880 of "c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\winsock.h")  Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   2404    
Error (active)  E1389   redeclaration cannot add dllexport/dllimport to "WSAIsBlocking" (declared at line 882 of "c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\winsock.h")    Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   2425    
Error (active)  E1389   redeclaration cannot add dllexport/dllimport to "WSAUnhookBlockingHook" (declared at line 884 of "c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\winsock.h")    Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   2443    
Error (active)  E1389   redeclaration cannot add dllexport/dllimport to "WSASetBlockingHook" (declared at line 886 of "c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\winsock.h")   Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   2461    
Error (active)  E1389   redeclaration cannot add dllexport/dllimport to "WSACancelBlockingCall" (declared at line 888 of "c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\winsock.h")    Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   2479    
Error (active)  E1389   redeclaration cannot add dllexport/dllimport to "WSAAsyncGetServByName" (declared at line 890 of "c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\winsock.h")    Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   2497    
Error (active)  E1389   redeclaration cannot add dllexport/dllimport to "WSAAsyncGetServByPort" (declared at line 898 of "c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\winsock.h")    Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   2525    
Error (active)  E1389   redeclaration cannot add dllexport/dllimport to "WSAAsyncGetProtoByName" (declared at line 906 of "c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\winsock.h")   Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   2553    
Error (active)  E1389   redeclaration cannot add dllexport/dllimport to "WSAAsyncGetProtoByNumber" (declared at line 913 of "c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\winsock.h") Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   2579    
Error (active)  E1389   redeclaration cannot add dllexport/dllimport to "WSAAsyncGetHostByName" (declared at line 920 of "c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\winsock.h")    Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   2605    
Error (active)  E1389   redeclaration cannot add dllexport/dllimport to "WSAAsyncGetHostByAddr" (declared at line 927 of "c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\winsock.h")    Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   2631    
Error (active)  E1389   redeclaration cannot add dllexport/dllimport to "WSACancelAsyncRequest" (declared at line 936 of "c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\winsock.h")    Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   2661    
Error (active)  E1389   redeclaration cannot add dllexport/dllimport to "WSAAsyncSelect" (declared at line 938 of "c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\winsock.h")   Project1    c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinSock2.h   2679    
Warning C4005   'AF_IPX': macro redefinition    Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\shared\ws2def.h 103 
Warning C4005   'AF_MAX': macro redefinition    Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\shared\ws2def.h 147 
Warning C4005   'SO_DONTLINGER': macro redefinition Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\shared\ws2def.h 185 
Error   C2011   'sockaddr': 'struct' type redefinition  Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\shared\ws2def.h 235 
Error   C2059   syntax error: 'constant'    Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\shared\ws2def.h 437 
Error   C3805   'constant': unexpected token, expected either '}' or a ','  Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\shared\ws2def.h 437 
Warning C4005   'IN_CLASSA': macro redefinition Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\shared\ws2def.h 572 
Warning C4005   'IN_CLASSC': macro redefinition Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\shared\ws2def.h 584 
Warning C4005   'INADDR_ANY': macro redefinition    Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\shared\ws2def.h 595 
Warning C4005   'INADDR_BROADCAST': macro redefinition  Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\shared\ws2def.h 597 
Error   C2011   'sockaddr_in': 'struct' type redefinition   Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\shared\ws2def.h 633 
Error   C2011   'fd_set': 'struct' type redefinition    Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   136 
Warning C4005   'FD_CLR': macro redefinition    Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   156 
Warning C4005   'FD_SET': macro redefinition    Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   171 
Error   C2011   'timeval': 'struct' type redefinition   Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   180 
Error   C2011   'hostent': 'struct' type redefinition   Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   236 
Error   C2011   'netent': 'struct' type redefinition    Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   249 
Error   C2011   'servent': 'struct' type redefinition   Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   256 
Error   C2011   'protoent': 'struct' type redefinition  Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   268 
Error   C2011   'WSAData': 'struct' type redefinition   Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   364 
Error   C2011   'sockproto': 'struct' type redefinition Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   462 
Error   C2011   'linger': 'struct' type redefinition    Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   504 
Warning C4005   'SOMAXCONN': macro redefinition Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   517 
Warning C4005   'FD_READ': macro redefinition   Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   551 
Warning C4005   'FD_WRITE': macro redefinition  Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   554 
Warning C4005   'FD_OOB': macro redefinition    Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   557 
Warning C4005   'FD_ACCEPT': macro redefinition Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   560 
Warning C4005   'FD_CONNECT': macro redefinition    Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   563 
Warning C4005   'FD_CLOSE': macro redefinition  Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   566 
Error   C2375   'accept': redefinition; different linkage   Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   1624    
Error   C2375   'bind': redefinition; different linkage Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   1646    
Error   C2375   'closesocket': redefinition; different linkage  Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   1667    
Error   C2375   'connect': redefinition; different linkage  Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   1684    
Error   C2375   'ioctlsocket': redefinition; different linkage  Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   1705    
Error   C2375   'getpeername': redefinition; different linkage  Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   1728    
Error   C2375   'getsockname': redefinition; different linkage  Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   1749    
Error   C2375   'getsockopt': redefinition; different linkage   Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   1770    
Error   C2375   'htonl': redefinition; different linkage    Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   1795    
Error   C2375   'htons': redefinition; different linkage    Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   1812    
Error   C2375   'inet_addr': redefinition; different linkage    Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   1830    
Error   C2375   'inet_ntoa': redefinition; different linkage    Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   1848    
Error   C2375   'listen': redefinition; different linkage   Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   1948    
Error   C2375   'ntohl': redefinition; different linkage    Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   1967    
Error   C2375   'ntohs': redefinition; different linkage    Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   1984    
Error   C2375   'recv': redefinition; different linkage Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   2001    
Error   C2375   'recvfrom': redefinition; different linkage Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   2024    
Error   C2375   'select': redefinition; different linkage   Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   2051    
Error   C2375   'send': redefinition; different linkage Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   2076    
Error   C2375   'sendto': redefinition; different linkage   Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   2099    
Error   C2375   'setsockopt': redefinition; different linkage   Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   2126    
Error   C2375   'shutdown': redefinition; different linkage Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   2151    
Error   C2375   'socket': redefinition; different linkage   Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   2171    
Error   C2375   'gethostbyaddr': redefinition; different linkage    Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   2196    
Error   C2375   'gethostbyname': redefinition; different linkage    Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   2218    
Error   C2375   'gethostname': redefinition; different linkage  Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   2235    
Error   C2375   'getservbyport': redefinition; different linkage    Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   2275    
Error   C2375   'getservbyname': redefinition; different linkage    Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   2294    
Error   C2375   'getprotobynumber': redefinition; different linkage Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   2313    
Error   C2375   'getprotobyname': redefinition; different linkage   Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   2330    
Error   C2375   'WSAStartup': redefinition; different linkage   Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   2350    
Error   C2375   'WSACleanup': redefinition; different linkage   Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   2370    
Error   C2375   'WSASetLastError': redefinition; different linkage  Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   2387    
Error   C2375   'WSAGetLastError': redefinition; different linkage  Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   2404    
Error   C2375   'WSAIsBlocking': redefinition; different linkage    Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   2425    
Error   C2375   'WSAUnhookBlockingHook': redefinition; different linkage    Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   2443    
Error   C2375   'WSASetBlockingHook': redefinition; different linkage   Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   2461    
Error   C2375   'WSACancelBlockingCall': redefinition; different linkage    Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   2479    
Error   C2375   'WSAAsyncGetServByName': redefinition; different linkage    Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   2497    
Error   C2375   'WSAAsyncGetServByPort': redefinition; different linkage    Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   2525    
Error   C2375   'WSAAsyncGetProtoByName': redefinition; different linkage   Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   2553    
Error   C2375   'WSAAsyncGetProtoByNumber': redefinition; different linkage Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   2579    
Error   C2375   'WSAAsyncGetHostByName': redefinition; different linkage    Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   2605    
Error   C2375   'WSAAsyncGetHostByAddr': redefinition; different linkage    Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   2631    
Error   C2375   'WSACancelAsyncRequest': redefinition; different linkage    Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   2661    
Error   C2375   'WSAAsyncSelect': redefinition; different linkage   Project1    c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\winsock2.h   2679    

1 个答案:

答案 0 :(得分:3)

如果_WIN32_WINNT< 0x0400,则windows.h包括winsock.h(如果尚未包括)和winsock2.h

如果_WIN32_WINNT>= 0x0400,则windows.h包括winsock2.h而不是winsock.h

winsock.hwinsock2.h不兼容,因此,如果winsock.h之前包含winsock2.h,则会发生各种错误,包括您所看到的错误。但是,如果在winsock2.h之前加入winsock.h,则会禁用winsock.h,以免发生错误。

因此,要禁用winsock.h以便可以使用winsock2.h,您需要执行以下任一操作:

  • 确保_WIN32_WINNT>= 0x0400

  • #include<winsock2.h>上方的#include<windows.h>

  • #define _WINSOCKAPI_上方添加#include<windows.h>


另外,请注意,WIN_32_LEAN_AND_MEAN需改为WIN32_LEAN_AND_MEAN