获取本地网卡适配器信息具体代码
本文导语: 效果如下: 具体代码如下: 代码如下:#include #include #include #pragma comment(lib, "IPHlpApi")#pragma comment(lib, "ws2_32") int main(int argc, char **argv){ PIP_ADAPTER_INFO pAdapterInfo = NULL; ULONG ulLen = sizeof(IP_ADAPTER_INFO); struct tm newtime; ...
效果如下:
具体代码如下:
#include
#include
#include
#pragma comment(lib, "IPHlpApi")
#pragma comment(lib, "ws2_32")
int main(int argc, char **argv)
{
PIP_ADAPTER_INFO pAdapterInfo = NULL;
ULONG ulLen = sizeof(IP_ADAPTER_INFO);
struct tm newtime;
char szBuffer[32];
errno_t error;
//为适配器结构申请内存
//pAdapterInfo = (PIP_ADAPTER_INFO)GlobalAlloc(GPTR, ulLen);
pAdapterInfo = (PIP_ADAPTER_INFO)HeapAlloc(GetProcessHeap(), 0, sizeof(IP_ADAPTER_INFO));
if (NULL == pAdapterInfo)
{
printf("Error allocating memory needed to call GetAdaptersInfo.n");
return 1;
}
if (ERROR_BUFFER_OVERFLOW == GetAdaptersInfo(pAdapterInfo, &ulLen))
{
HeapFree(GetProcessHeap(), 0, pAdapterInfo);
pAdapterInfo = (PIP_ADAPTER_INFO)HeapAlloc(GetProcessHeap(), 0, ulLen);
if (NULL == pAdapterInfo)
{
printf("Error allocating memory needed to call GetAdaptersInfo.n");
return 1;
}
}
//取得本地适配器结构信息
if (ERROR_SUCCESS != GetAdaptersInfo(pAdapterInfo, &ulLen))
{
printf("GetAdaptersInfo error!n");
return 0;
}
if (NULL == pAdapterInfo)
{
printf("There is no adapters!n");
return 0;
}
SetConsoleTitle(TEXT("本地网卡适配器信息"));
do
{
printf("ComboIndex:%dn", pAdapterInfo->ComboIndex);
printf("Adapter Name:%sn", pAdapterInfo->AdapterName);
printf("Adapter Desc:%sn", pAdapterInfo->Description);
printf("Adapter Addr:");
for (size_t i = 0; i < pAdapterInfo->AddressLength; i++)
{
if (i == (pAdapterInfo->AddressLength - 1))
{
printf("%02X", (int)pAdapterInfo->Address[i]);
}
else
{
printf("%02X-", (int)pAdapterInfo->Address[i]);
}
}
printf("n");
printf("Index:%dn", pAdapterInfo->Index);
printf("Type:");
switch (pAdapterInfo->Type)
{
case MIB_IF_TYPE_OTHER:printf("Othern"); break;
case MIB_IF_TYPE_ETHERNET:printf("Ethernetn"); break;
case MIB_IF_TYPE_TOKENRING:printf("Token Ringn"); break;
case MIB_IF_TYPE_FDDI:printf("FDDIn"); break;
case MIB_IF_TYPE_PPP:printf("PPPn"); break;
case MIB_IF_TYPE_LOOPBACK:printf("Lookbackn"); break;
case MIB_IF_TYPE_SLIP:printf("Slipn"); break;
default:printf("Unknow type %ldn", pAdapterInfo->Type); break;
}
printf("IP Address:%sn", pAdapterInfo->IpAddressList.IpAddress.String);
printf("IP Mask:%sn", pAdapterInfo->IpAddressList.IpMask.String);
printf("Gateway:%sn", pAdapterInfo->GatewayList.IpAddress.String);
if (pAdapterInfo->DhcpEnabled)
{
printf("DHCP Enabled:Yesn");
printf("DHCP Server:%sn", pAdapterInfo->DhcpServer.IpAddress.String);
printf("Lease Obtained:");
error = _localtime32_s(&newtime, (__time32_t*)&pAdapterInfo->LeaseObtained);
if (error)
{
printf("Invalid Argument to _localtime32_s.n");
}
else
{
error = asctime_s(szBuffer, 32, &newtime);
if (error)
{
printf("Invalid Argument to asctime_s.n");
}
else
{
printf("%s", szBuffer);
}
}
printf("Lease Expires:");
error = _localtime32_s(&newtime, (__time32_t*)&pAdapterInfo->LeaseExpires);
if (error)
{
printf("Invalid Argument to _localtime32_s.n");
}
else
{
error = asctime_s(szBuffer, 32, &newtime);
if (error)
{
printf("Invalid Argument to asctime_s.n");
}
else
{
printf("%s", szBuffer);
}
}
}
else
{
printf("DHCP Enabled:Non");
}
if (pAdapterInfo->HaveWins)
{
printf("Have Wins:Yesn");
printf("Primary Wins Server:%sn", pAdapterInfo->PrimaryWinsServer.IpAddress.String);
printf("Secondary Wins Server:%sn", pAdapterInfo->SecondaryWinsServer.IpAddress.String);
}
else
{
printf("Have Wins:Non");
}
printf("=================================================================n");
pAdapterInfo = pAdapterInfo->Next;
} while (pAdapterInfo);
if (pAdapterInfo)
{
HeapFree(GetProcessHeap(), 0, pAdapterInfo);
}
return 0;
}
您可能感兴趣的文章:
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。