2

我知道这个问题已经被问过很多次了,不知道问题到底出在哪里。我已经编写了一个代码,通过引用 msdn库中的GetAdaptersAddresses函数来获取本地计算机的IP地址。else部分中的printf函数正在获取打印在控制台屏幕上。

代码如下。

  #include<WinSock2.h>
  #include<iphlpapi.h>
  #include<stdio.h>
  #include<stdlib.h>
  #include<Windows.h>
  #pragma comment(lib, "IPHLPAPI.lib")


   int main(void )
   {
          PIP_ADAPTER_ADDRESSES pAddresses = NULL;

          DWORD retval=0;
          ULONG outBufLen=sizeof(IP_ADAPTER_ADDRESSES);


          retval=GetAdaptersAddresses(AF_UNSPEC,GAA_FLAG_INCLUDE_PREFIX, NULL,pAddresses, &outBufLen);

    if(retval==NO_ERROR)
     {
        while (pAddresses) {

          printf("\tAdapter name: %s\n", pAddresses->AdapterName);

          printf("Description: %wS\n", pAddresses->Description);
          printf("\tTransmit link speed: %I64u\n", pAddresses->TransmitLinkSpeed);
          printf("\tReceive link speed: %I64u\n", pAddresses->ReceiveLinkSpeed);

          pAddresses = pAddresses->Next;
      }
   }
  else
  {
   printf("\n There has been an error ");
  }

  free(pAddresses);
 }
4

1 回答 1

1

问题是pAddresses没有分配。至少不在您向我们展示的代码中。看看这个底部的例子:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa365915%28v=vs.85%29.aspx

专供线路pAddresses = (IP_ADAPTER_ADDRESSES *) MALLOC(outBufLen);

于 2013-06-28T14:39:46.557 回答