C语言实现Windows下获取IP和MAC地址

C语言实现Windows下获取IP和MAC地址

C语言实现Windows下获取IP和MAC地址。

 #include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <httpext.h>
#include <windef.h>
#include <Nb30.h>
#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib,"netapi32.lib")
void CheckIP(void)
{
WSADATA wsaData;
char name[];
char *ip;
PHOSTENT hostinfo;
if (WSAStartup(MAKEWORD(, ), &wsaData) == )
{
if (gethostname(name, sizeof(name)) == )
{
if ((hostinfo = gethostbyname(name)) != NULL)
{
ip = inet_ntoa(*(struct in_addr *)*hostinfo->h_addr_list);
printf(" IP地址: %s ", ip);
}
}
WSACleanup();
}
}
int getMAC(char * mac)
{
NCB ncb;
typedef struct _ASTAT_
{
ADAPTER_STATUS adapt;
NAME_BUFFER NameBuff[];
}ASTAT, *PASTAT; ASTAT Adapter;
typedef struct _LANA_ENUM
{
UCHAR length;
UCHAR lana[MAX_LANA];
}LANA_ENUM;
LANA_ENUM lana_enum;
UCHAR uRetCode;
memset(&ncb, , sizeof(ncb));
memset(&lana_enum, , sizeof(lana_enum)); ncb.ncb_command = NCBENUM;
ncb.ncb_buffer = (unsigned char *)&lana_enum;
ncb.ncb_length = sizeof(LANA_ENUM);
uRetCode = Netbios(&ncb);
if (uRetCode != NRC_GOODRET)
return uRetCode; for (int lana = ; lana<lana_enum.length; lana++)
{
ncb.ncb_command = NCBRESET;
ncb.ncb_lana_num = lana_enum.lana[lana];
uRetCode = Netbios(&ncb);
if (uRetCode == NRC_GOODRET)
break;
}
if (uRetCode != NRC_GOODRET)
return uRetCode; memset(&ncb, , sizeof(ncb));
ncb.ncb_command = NCBASTAT;
ncb.ncb_lana_num = lana_enum.lana[];
strcpy_s((char*)ncb.ncb_callname,, "*");
ncb.ncb_buffer = (unsigned char *)&Adapter;
ncb.ncb_length = sizeof(Adapter);
uRetCode = Netbios(&ncb);
if (uRetCode != NRC_GOODRET)
return uRetCode;
sprintf_s(mac,, "%02X-%02X-%02X-%02X-%02X-%02X",
Adapter.adapt.adapter_address[],
Adapter.adapt.adapter_address[],
Adapter.adapt.adapter_address[],
Adapter.adapt.adapter_address[],
Adapter.adapt.adapter_address[],
Adapter.adapt.adapter_address[]
);
return ;
}
int main(void)
{
CheckIP();
char mac[];
getMAC(mac);
printf(" mac地址 : %s ", mac);
getchar();
return ;
}

C语言实现Windows下获取IP和MAC地址。-LMLPHP

05-06 13:06