您的位置:首页 > 理论基础 > 计算机网络

获取网络配置信息

2009-06-12 11:01 330 查看
void GetMac2()
{
// It is possible for an adapter to have multiple
// IPv4 addresses, gateways, and secondary WINS servers
// assigned to the adapter.
// Note that this sample code only prints out the
// first entry for the IP address/mask, gateway,
// and secondary WINS server for each adapter.

PIP_ADAPTER_INFO pAdapterInfo;
PIP_ADAPTER_INFO pAdapter = NULL;
DWORD dwRetVal = 0;
unsigned long ulOutBufLen = 0;

pAdapterInfo = (IP_ADAPTER_INFO *) malloc( sizeof(IP_ADAPTER_INFO) );
ulOutBufLen = sizeof(IP_ADAPTER_INFO);


// Make an initial call to GetAdaptersInfo to get
// the necessary size into the ulOutBufLen variable
if (GetAdaptersInfo( pAdapterInfo, &ulOutBufLen) != ERROR_SUCCESS) {
GlobalFree (pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen);
}

if ((dwRetVal = GetAdaptersInfo( pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
pAdapter = pAdapterInfo;
while (pAdapter) {
printf("/tAdapter Name: /t%s/n", pAdapter->AdapterName);
printf("/tAdapter Desc: /t%s/n", pAdapter->Description);
printf("/tAdapter Addr: /t");
for (UINT i = 0; i < pAdapter->AddressLength; i++)
printf("%.2x%c", pAdapter->Address[i], i == pAdapter->AddressLength - 1 ? '/n' : '-');
printf("/tIP Address: /t%s/n", pAdapter->IpAddressList.IpAddress.String);
printf("/tIP Mask: /t%s/n", pAdapter->IpAddressList.IpMask.String);

printf("/tGateway: /t%s/n", pAdapter->GatewayList.IpAddress.String);
printf("/t***/n");
if (pAdapter->DhcpEnabled) {
printf("/tDHCP Enabled: Yes/n");
printf("/t/tDHCP Server: /t%s/n", pAdapter->DhcpServer.IpAddress.String);
printf("/tLease Obtained: %ld/n", pAdapter->LeaseObtained);
}
else
printf("/tDHCP Enabled: No/n");

if (pAdapter->HaveWins) {
printf("/tHave Wins: Yes/n");
printf("/t/tPrimary Wins Server: /t%s/n", pAdapter->PrimaryWinsServer.IpAddress.String);
printf("/t/tSecondary Wins Server: /t%s/n", pAdapter->SecondaryWinsServer.IpAddress.String);
}
else
printf("/tHave Wins: No/n");

pAdapter = pAdapter->Next;
}
}
else {
printf("Call to GetAdaptersInfo failed./n");
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: