我知道这个问题已经被问过很多次了,不知道问题到底出在哪里。我已经编写了一个代码,通过引用 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);
}