Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/165.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用“时出错”;IP“适配器地址”;_C++_Eclipse - Fatal编程技术网

C++ 使用“时出错”;IP“适配器地址”;

C++ 使用“时出错”;IP“适配器地址”;,c++,eclipse,C++,Eclipse,当我构建我的项目时,我会遇到很多错误,我不知道为什么 我准确地复制了示例代码,并包含了必要的标题 我正在使用Eclipse作为我的IDE 这是我的代码: #include <windows.h> #include <winsock2.h> #include <iphlpapi.h> #include <iptypes.h> #include <stdio.h> #include <iostream> #pragma comm

当我构建我的项目时,我会遇到很多错误,我不知道为什么

我准确地复制了示例代码,并包含了必要的标题

我正在使用Eclipse作为我的IDE

这是我的代码:

#include <windows.h>
#include <winsock2.h>
#include <iphlpapi.h>
#include <iptypes.h>
#include <stdio.h>
#include <iostream>
#pragma comment(lib, "IPHLPAPI.lib")

#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))

/* Note: could also use malloc() and free() */

int __cdecl main(int argc, char **argv)
{

    /* Declare and initialize variables */

    DWORD dwSize = 0;
    DWORD dwRetVal = 0;

    unsigned int i = 0;

    // Set the flags to pass to GetAdaptersAddresses
    ULONG flags = GAA_FLAG_INCLUDE_PREFIX;

    // default to unspecified address family (both)
    ULONG family = AF_UNSPEC;

    LPVOID lpMsgBuf = NULL;

    PIP_ADAPTER_ADDRESSES pAddresses = NULL;
    ULONG outBufLen = 0;

    PIP_ADAPTER_ADDRESSES pCurrAddresses = NULL;
    PIP_ADAPTER_UNICAST_ADDRESS pUnicast = NULL;
    PIP_ADAPTER_ANYCAST_ADDRESS pAnycast = NULL;
    PIP_ADAPTER_MULTICAST_ADDRESS pMulticast = NULL;
    IP_ADAPTER_DNS_SERVER_ADDRESS *pDnServer = NULL;
    IP_ADAPTER_PREFIX *pPrefix = NULL;

    if (argc != 2) {
        printf(" Usage: getadapteraddresses family\n");
        printf("        getadapteraddresses 4 (for IPv4)\n");
        printf("        getadapteraddresses 6 (for IPv6)\n");
        printf("        getadapteraddresses A (for both IPv4 and IPv6)\n");
        exit(1);
    }

    if (atoi(argv[1]) == 4)
        family = AF_INET;
    else if (atoi(argv[1]) == 6)
        family = AF_INET6;

    outBufLen = sizeof (IP_ADAPTER_ADDRESSES);
    pAddresses = (IP_ADAPTER_ADDRESSES *) MALLOC(outBufLen);

    // Make an initial call to GetAdaptersAddresses to get the
    // size needed into the outBufLen variable
    if (GetAdaptersAddresses(family, flags, NULL, pAddresses, &outBufLen)
        == ERROR_BUFFER_OVERFLOW) {
        FREE(pAddresses);
        pAddresses = (IP_ADAPTER_ADDRESSES *) MALLOC(outBufLen);
    }

    if (pAddresses == NULL) {
        printf("Memory allocation failed for IP_ADAPTER_ADDRESSES struct\n");
        exit(1);
    }
    // Make a second call to GetAdapters Addresses to get the
    // actual data we want
    printf("Memory allocated for GetAdapterAddresses = %d bytes\n", outBufLen);
    printf("Calling GetAdaptersAddresses function with family = ");
    if (family == AF_INET)
        printf("AF_INET\n");
    if (family == AF_INET6)
        printf("AF_INET6\n");
    if (family == AF_UNSPEC)
        printf("AF_UNSPEC\n\n");

    dwRetVal =
        GetAdaptersAddresses(family, flags, NULL, pAddresses, &outBufLen);

    if (dwRetVal == NO_ERROR) {
        // If successful, output some information from the data we received
        pCurrAddresses = pAddresses;
        while (pCurrAddresses) {
            printf("\tLength of the IP_ADAPTER_ADDRESS struct: %ld\n",
                   pCurrAddresses->Length);
            printf("\tIfIndex (IPv4 interface): %u\n", pCurrAddresses->IfIndex);
            printf("\tAdapter name: %s\n", pCurrAddresses->AdapterName);

            pUnicast = pCurrAddresses->FirstUnicastAddress;
            if (pUnicast != NULL) {
                for (i = 0; pUnicast != NULL; i++)
                    pUnicast = pUnicast->Next;
                printf("\tNumber of Unicast Addresses: %d\n", i);
            } else
                printf("\tNo Unicast Addresses\n");

            pAnycast = pCurrAddresses->FirstAnycastAddress;
            if (pAnycast) {
                for (i = 0; pAnycast != NULL; i++)
                    pAnycast = pAnycast->Next;
                printf("\tNumber of Anycast Addresses: %d\n", i);
            } else
                printf("\tNo Anycast Addresses\n");

            pMulticast = pCurrAddresses->FirstMulticastAddress;
            if (pMulticast) {
                for (i = 0; pMulticast != NULL; i++)
                    pMulticast = pMulticast->Next;
                printf("\tNumber of Multicast Addresses: %d\n", i);
            } else
                printf("\tNo Multicast Addresses\n");

            pDnServer = pCurrAddresses->FirstDnsServerAddress;
            if (pDnServer) {
                for (i = 0; pDnServer != NULL; i++)
                    pDnServer = pDnServer->Next;
                printf("\tNumber of DNS Server Addresses: %d\n", i);
            } else
                printf("\tNo DNS Server Addresses\n");

            printf("\tDNS Suffix: %wS\n", pCurrAddresses->DnsSuffix);
            printf("\tDescription: %wS\n", pCurrAddresses->Description);
            printf("\tFriendly name: %wS\n", pCurrAddresses->FriendlyName);

            if (pCurrAddresses->PhysicalAddressLength != 0) {
                printf("\tPhysical address: ");
                for (i = 0; i < pCurrAddresses->PhysicalAddressLength;
                     i++) {
                    if (i == (pCurrAddresses->PhysicalAddressLength - 1))
                        printf("%.2X\n",
                               (int) pCurrAddresses->PhysicalAddress[i]);
                    else
                        printf("%.2X-",
                               (int) pCurrAddresses->PhysicalAddress[i]);
                }
            }
            printf("\tFlags: %ld\n", pCurrAddresses->Flags);
            printf("\tMtu: %lu\n", pCurrAddresses->Mtu);
            printf("\tIfType: %ld\n", pCurrAddresses->IfType);
            printf("\tOperStatus: %ld\n", pCurrAddresses->OperStatus);
            printf("\tIpv6IfIndex (IPv6 interface): %u\n",
                   pCurrAddresses->Ipv6IfIndex);
            printf("\tZoneIndices (hex): ");
            for (i = 0; i < 16; i++)
                printf("%lx ", pCurrAddresses->ZoneIndices[i]);
            printf("\n");

            pPrefix = pCurrAddresses->FirstPrefix;
            if (pPrefix) {
                for (i = 0; pPrefix != NULL; i++)
                    pPrefix = pPrefix->Next;
                printf("\tNumber of IP Adapter Prefix entries: %d\n", i);
            } else
                printf("\tNo IP Adapter Prefix entries\n");

            printf("\n");

            pCurrAddresses = pCurrAddresses->Next;
        }
    } else {
        printf("Call to GetAdaptersAddresses failed with error: %d\n",
               dwRetVal);
        if (dwRetVal == ERROR_NO_DATA)
            printf("\tNo addresses were found for the requested parameters\n");
        else {

            if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwRetVal, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),   // Default language
                              (LPTSTR) & lpMsgBuf, 0, NULL)) {
                printf("\tError: %s", lpMsgBuf);
                LocalFree(lpMsgBuf);
                FREE(pAddresses);
                exit(1);
            }
        }
    }
    FREE(pAddresses);
    return 0;
}

这个代码不是可移植的C++。它是专门为Visual C++编译器编写的。您应该期望在尝试交换
windows.h
winsock2.h
的顺序时提供的大多数“C++”代码都是如此
iptypes.h
仅在使用
winsock2.h
时声明受影响的类型和常量,而
windows.h
使用
winsock.h
,这会干扰
winsock2.h
。如果在
windows.h
之前包含
winsock2.h
winsock2.h
将禁用
winsock.h
@RemyLebeau-swapped。不work@heydar那么,剩下的罪魁祸首可能是您使用的是过时版本的
iphlpapi.h
/
iptypes.h
,它们没有声明您试图使用的内容,或者它们被包装在项目中没有定义的
#ifdef
中(在我的编译器的Win32 SDK中,它们包装在
#ifdef(u WINSOCK2API)
中,因此需要
winsock2.h
)。
Info: Internal Builder is used for build
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\test3.o" "..\\src\\test3.cpp" 
..\src\test3.cpp:7:0: warning: ignoring #pragma comment  [-Wunknown-pragmas]
 #pragma comment(lib, "IPHLPAPI.lib")

..\src\test3.cpp:8:0: warning: ignoring #pragma comment  [-Wunknown-pragmas]
 #pragma comment(lib, "Ws2_32.lib")

..\src\test3.cpp: In function 'int main(int, char**)':
..\src\test3.cpp:26:19: error: 'GAA_FLAG_INCLUDE_PREFIX' was not declared in this scope
     ULONG flags = GAA_FLAG_INCLUDE_PREFIX;
                   ^~~~~~~~~~~~~~~~~~~~~~~
..\src\test3.cpp:33:5: error: 'PIP_ADAPTER_ADDRESSES' was not declared in this scope
     PIP_ADAPTER_ADDRESSES pAddresses = NULL; // @suppress("Type cannot be resolved")
     ^~~~~~~~~~~~~~~~~~~~~
..\src\test3.cpp:36:27: error: expected ';' before 'pCurrAddresses'
     PIP_ADAPTER_ADDRESSES pCurrAddresses = NULL;
                           ^~~~~~~~~~~~~~
..\src\test3.cpp:37:5: error: 'PIP_ADAPTER_UNICAST_ADDRESS' was not declared in this scope
     PIP_ADAPTER_UNICAST_ADDRESS pUnicast = NULL;
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~
..\src\test3.cpp:38:5: error: 'PIP_ADAPTER_ANYCAST_ADDRESS' was not declared in this scope
     PIP_ADAPTER_ANYCAST_ADDRESS pAnycast = NULL;
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~
..\src\test3.cpp:39:5: error: 'PIP_ADAPTER_MULTICAST_ADDRESS' was not declared in this scope
     PIP_ADAPTER_MULTICAST_ADDRESS pMulticast = NULL;
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
..\src\test3.cpp:40:5: error: 'IP_ADAPTER_DNS_SERVER_ADDRESS' was not declared in this scope
     IP_ADAPTER_DNS_SERVER_ADDRESS *pDnServer = NULL;
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
..\src\test3.cpp:40:36: error: 'pDnServer' was not declared in this scope
     IP_ADAPTER_DNS_SERVER_ADDRESS *pDnServer = NULL;
                                    ^~~~~~~~~
..\src\test3.cpp:41:5: error: 'IP_ADAPTER_PREFIX' was not declared in this scope
     IP_ADAPTER_PREFIX *pPrefix = NULL;
     ^~~~~~~~~~~~~~~~~
..\src\test3.cpp:41:24: error: 'pPrefix' was not declared in this scope
     IP_ADAPTER_PREFIX *pPrefix = NULL;
                        ^~~~~~~
..\src\test3.cpp:56:25: error: 'IP_ADAPTER_ADDRESSES' was not declared in this scope
     outBufLen = sizeof (IP_ADAPTER_ADDRESSES);
                         ^~~~~~~~~~~~~~~~~~~~
..\src\test3.cpp:57:5: error: 'pAddresses' was not declared in this scope
     pAddresses = (IP_ADAPTER_ADDRESSES *) MALLOC(outBufLen);
     ^~~~~~~~~~
..\src\test3.cpp:57:41: error: expected primary-expression before ')' token
     pAddresses = (IP_ADAPTER_ADDRESSES *) MALLOC(outBufLen);
                                         ^
..\src\test3.cpp:61:73: error: 'GetAdaptersAddresses' was not declared in this scope
     if (GetAdaptersAddresses(family, flags, NULL, pAddresses, &outBufLen)
                                                                         ^
..\src\test3.cpp:64:45: error: expected primary-expression before ')' token
         pAddresses = (IP_ADAPTER_ADDRESSES *) MALLOC(outBufLen);
                                             ^
..\src\test3.cpp:73:78: warning: format '%d' expects argument of type 'int', but argument 2 has type 'ULONG {aka long unsigned int}' [-Wformat=]
     printf("Memory allocated for GetAdapterAddresses = %d bytes\n", outBufLen);
                                                                              ^
..\src\test3.cpp:83:73: error: 'GetAdaptersAddresses' was not declared in this scope
         GetAdaptersAddresses(family, flags, NULL, pAddresses, &outBufLen);
                                                                         ^
..\src\test3.cpp:87:9: error: 'pCurrAddresses' was not declared in this scope
         pCurrAddresses = pAddresses;
         ^~~~~~~~~~~~~~
..\src\test3.cpp:94:13: error: 'pUnicast' was not declared in this scope
             pUnicast = pCurrAddresses->FirstUnicastAddress;
             ^~~~~~~~
..\src\test3.cpp:102:13: error: 'pAnycast' was not declared in this scope
             pAnycast = pCurrAddresses->FirstAnycastAddress;
             ^~~~~~~~
..\src\test3.cpp:110:13: error: 'pMulticast' was not declared in this scope
             pMulticast = pCurrAddresses->FirstMulticastAddress;
             ^~~~~~~~~~
..\src\test3.cpp:167:24: warning: format '%d' expects argument of type 'int', but argument 2 has type 'DWORD {aka long unsigned int}' [-Wformat=]
                dwRetVal);
                        ^
..\src\test3.cpp:174:47: warning: format '%s' expects argument of type 'char*', but argument 2 has type 'LPVOID {aka void*}' [-Wformat=]
                 printf("\tError: %s", lpMsgBuf);
                                               ^
..\src\test3.cpp:20:11: warning: unused variable 'dwSize' [-Wunused-variable]
     DWORD dwSize = 0;
           ^~~~~~