取本机IP地址和网卡号
界面如下,主要是使用Winsock的一些东东取机器名和IP地址,取网卡就麻烦些,需要发送Netbios命令,需要注意的是需要包含winsock2.h和nb30.h,并且要引入两个lib:
包含头和lib如下:
根据 shootingstars 的指点,编写了一下用 Iphlpapi 取IP地址的代码,可以取本机IP,网关IP,子网掩码,网卡名字等,需要在VC7上编译:
包含头和lib如下:
#include<winsock2.h>
#include <nb30.h>
#pragma comment(lib, "netapi32.lib")
#pragma comment(lib, "Wsock32.lib")
#include <nb30.h>
#pragma comment(lib, "netapi32.lib")
#pragma comment(lib, "Wsock32.lib")
具体的实现参考源程序。
源程序下载: http://www.cppblog.com/Files/edog/GetIPName.rar根据 shootingstars 的指点,编写了一下用 Iphlpapi 取IP地址的代码,可以取本机IP,网关IP,子网掩码,网卡名字等,需要在VC7上编译:
#include "stdio.h"
#include "windows.h"
#include "Iphlpapi.h"
#pragma comment(lib, "Iphlpapi.lib")
void main()
{
IP_ADAPTER_INFO pAdapterInfo;
ULONG len = sizeof(pAdapterInfo);
if(GetAdaptersInfo(&pAdapterInfo, &len) != ERROR_SUCCESS)
{
printf("GetAdaptersInfo ERROR!\n");
}
else
{
printf("AdapterName: %s\n", pAdapterInfo.AdapterName);
printf("Description: %s\n", pAdapterInfo.Description);
printf("IP Address : %s\n", pAdapterInfo.IpAddressList.IpAddress.String);
printf("IP Mask : %s\n", pAdapterInfo.IpAddressList.IpMask.String);
printf("Gateway IP : %s\n", pAdapterInfo.GatewayList.IpAddress.String);
}
}
输出结果如下:
AdapterName: {475CB3CA-3478-415A-86D8-7E5982306F58}
Description: D-Link DFE-530TX PCI Fast Ethernet Adapter (rev.C)
IP Address : 192.168.168.23
IP Mask : 255.255.255.0
Gateway IP : 192.168.168.10
#include "windows.h"
#include "Iphlpapi.h"
#pragma comment(lib, "Iphlpapi.lib")
void main()
{
IP_ADAPTER_INFO pAdapterInfo;
ULONG len = sizeof(pAdapterInfo);
if(GetAdaptersInfo(&pAdapterInfo, &len) != ERROR_SUCCESS)
{
printf("GetAdaptersInfo ERROR!\n");
}
else
{
printf("AdapterName: %s\n", pAdapterInfo.AdapterName);
printf("Description: %s\n", pAdapterInfo.Description);
printf("IP Address : %s\n", pAdapterInfo.IpAddressList.IpAddress.String);
printf("IP Mask : %s\n", pAdapterInfo.IpAddressList.IpMask.String);
printf("Gateway IP : %s\n", pAdapterInfo.GatewayList.IpAddress.String);
}
}
输出结果如下:
AdapterName: {475CB3CA-3478-415A-86D8-7E5982306F58}
Description: D-Link DFE-530TX PCI Fast Ethernet Adapter (rev.C)
IP Address : 192.168.168.23
IP Mask : 255.255.255.0
Gateway IP : 192.168.168.10