windows获取IP和MAC地址【Qt】
1、根据hostname获取IP
main.cpp:
#include <QCoreApplication>
#include"noarp.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
char iname[128];
hostent *host;
//-----------初始化winsock---------------
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
//--------------------------------------
if(gethostname(iname,sizeof(iname))!=0){
printf("hostname_Error:%d\n",WSAGetLastError());
exit(0);
}
host=gethostbyname(iname);
printf("hostname:%s\n",host->h_name);
in_addr addr;
for(int i=0;;i++){
char * p=host->h_addr_list[i];
if(p==NULL)
break;
memcpy(&addr.s_addr,p,host->h_length);
}
char *dip=inet_ntoa(addr);
printf("IP:%s\n\n\n",dip);
return a.exec();
}
noarp.h:
#ifndef NOARP_H
#define NOARP_H
#include<winsock2.h>
#include<iphlpapi.h>
#include<stdlib.h>
#include<ws2tcpip.h>
#endif // NOARP_H
noarp.pro:
#-------------------------------------------------
#
# Project created by QtCreator 2017-11-22T18:39:28
#
#-------------------------------------------------
QT += core
QT -= gui
LIBS += -lpthread libwsock32 libws2_32
LIBS += -lIphlpapi
TARGET = noarp
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
HEADERS += \
noarp.h
注:
1、Winsock是Windows下的网络编程接口,它是由Unix下的BSD Socket
发展而来,是一个与网络协议无关的编程接口。
2、hostent结构体
struct hostent
{
char *h_name; //正式主机名
char **h_aliases; //主机别名
int h_addrtype; //主机IP地址类型:IPV4-AF_INET
int h_length; //主机IP地址字节长度,对于IPv4是四字
节,即32位
char **h_addr_list; //主机的IP地址列表
};
#define h_addr h_addr_list[0] //保存的是IP地址
3、动态链接库
LIBS += -lpthread libwsock32 libws2_32
LIBS += -lIphlpapi
2、根据网卡获取IP和MAC
main.cpp:
#include <QCoreApplication>
#include"noarp.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//-----------初始化winsock---------------
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
//--------------------------------------
PIP_ADAPTER_ADDRESSES pAddress=NULL;
//PIP_ADAPTER_ADDRESSES是一个指针,所以声明时没有得到
实际的存储空间,所以在下面要给其动态分配内存空间
ULONG buflen=0;
DWORD reCode=0;
GetAdaptersAddresses(AF_UNSPEC,0,NULL,pAddress,&buflen);
//GetAdaptersAddresses执行出错,作用是获取buflen的长度
//printf("%d\n",buflen);
pAddress = (IP_ADAPTER_ADDRESSES*) malloc(buflen);
if((reCode=GetAdaptersAddresses
(AF_INET,0,NULL,pAddress,&buflen))!=NO_ERROR){
printf("2no error\n");exit(0);
}
while(pAddress!=NULL){
PIP_ADAPTER_UNICAST_ADDRESS pUnicast=pAddress-
>FirstUnicastAddress;
for(int i=0;pUnicast!=NULL;i++){
if(pUnicast->Address.lpSockaddr-
>sa_family==AF_INET){
sockaddr_in *sa_in=(sockaddr_in *)pUnicast-
>Address.lpSockaddr;
printf("IP:%s\t",inet_ntoa(sa_in->sin_addr));
}
pUnicast=pUnicast->Next;
}
printf("MAC:%.2x-%.2x-%.2x-%.2x-%.2x-%.2x\n",
pAddress->PhysicalAddress[0],
pAddress->PhysicalAddress[1],
pAddress->PhysicalAddress[2],
pAddress->PhysicalAddress[3],
pAddress->PhysicalAddress[4],
pAddress->PhysicalAddress[5]);
pAddress=pAddress->Next;
}
return a.exec();
}
1)PIP_ADAPTER_ADDRESSES结构体
typedef struct _IP_ADAPTER_ADDRESSES {
union {
ULONGLONG Alignment;
struct {
ULONG Length;
DWORD IfIndex;
};
};
struct _IP_ADAPTER_ADDRESSES *Next;
PCHAR AdapterName;
PIP_ADAPTER_UNICAST_ADDRESS FirstUnicastAddress;
PIP_ADAPTER_ANYCAST_ADDRESS FirstAnycastAddress;
PIP_ADAPTER_MULTICAST_ADDRESS FirstMulticastAddress;
PIP_ADAPTER_DNS_SERVER_ADDRESS FirstDnsServerAddress;
PWCHAR DnsSuffix;
PWCHAR Description;
PWCHAR FriendlyName;
BYTE PhysicalAddress
[MAX_ADAPTER_ADDRESS_LENGTH];
DWORD PhysicalAddressLength;
DWORD Flags;
DWORD Mtu;
DWORD IfType;
IF_OPER_STATUS OperStatus;
DWORD Ipv6IfIndex;
DWORD ZoneIndices[16];
PIP_ADAPTER_PREFIX FirstPrefix;
ULONG64 TransmitLinkSpeed;
ULONG64 ReceiveLinkSpeed;
PIP_ADAPTER_WINS_SERVER_ADDRESS_LH FirstWinsServerAddress;
PIP_ADAPTER_GATEWAY_ADDRESS_LH FirstGatewayAddress;
ULONG Ipv4Metric;
ULONG Ipv6Metric;
IF_LUID Luid;
SOCKET_ADDRESS Dhcpv4Server;
NET_IF_COMPARTMENT_ID CompartmentId;
NET_IF_NETWORK_GUID NetworkGuid;
NET_IF_CONNECTION_TYPE ConnectionType;
TUNNEL_TYPE TunnelType;
SOCKET_ADDRESS Dhcpv6Server;
BYTE Dhcpv6ClientDuid
[MAX_DHCPV6_DUID_LENGTH];
ULONG Dhcpv6ClientDuidLength;
ULONG Dhcpv6Iaid;
PIP_ADAPTER_DNS_SUFFIX FirstDnsSuffix;
} IP_ADAPTER_ADDRESSES, *PIP_ADAPTER_ADDRESSES;
2)*PIP_ADAPTER_UNICAST_ADDRESS结构体
typedef struct _IP_ADAPTER_UNICAST_ADDRESS {
union {
struct {
ULONG Length;
DWORD Flags;
};
};
struct _IP_ADAPTER_UNICAST_ADDRESS *Next;
SOCKET_ADDRESS Address;
IP_PREFIX_ORIGIN PrefixOrigin;
IP_SUFFIX_ORIGIN SuffixOrigin;
IP_DAD_STATE DadState;
ULONG ValidLifetime;
ULONG PreferredLifetime;
ULONG LeaseLifetime;
UINT8 OnLinkPrefixLength;
} IP_ADAPTER_UNICAST_ADDRESS, *PIP_ADAPTER_UNICAST_ADDRESS;
3)SOCKET_ADDRESS结构体
typedef struct _SOCKET_ADDRESS {
LPSOCKADDR lpSockaddr;
INT iSockaddrLength;
} SOCKET_ADDRESS, *PSOCKET_ADDRESS;
4)sockaddr_in结构体
struct sockaddr_in {
short sin_family;
u_short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};