📄 getmac.~h
字号:
#ifndef _GET_NETINFO_H
#define _GET_NETINFO_H
#include "iphlpapi.h"
#include "winsock2.h"
#pragma comment ( lib, "ws2_32.lib" )
#pragma comment ( lib, "Iphlpapi.lib" )
#include "stdio.h"
char *getmac(char * dest_mac, char * input_ip)
{
WSADATA wsaData;
int numberOfHost = 1;
dest_mac[0]=0;
int iRet = WSAStartup(MAKEWORD(2,1), &wsaData);
if ( iRet != 0 )
{
//printf( "WSAStartup Error:%d\n", GetLastError() );
strcpy(dest_mac,"init_error");
exit( 0 );
}
int nRemoteAddr = inet_addr( input_ip );
struct hostent* remoteHostent;
//remoteHostent= (struct hostent*)malloc( sizeof(struct hostent ));
struct in_addr sa;
for ( int i = 0; i < numberOfHost; i ++ )
{
//获取远程机器名
sa.s_addr = nRemoteAddr;
//printf( "\nIpAddress : %s\n", inet_ntoa( sa ) );
//remoteHostent = gethostbyaddr( (char*)&nRemoteAddr,4, AF_INET );
//if ( remoteHostent )
//printf( "HostName : %s\n",remoteHostent->h_name );
//Form1->Label1->Caption = remoteHostent->h_name ;
//else
//printf( "gethostbyaddr Error:%d\n",GetLastError() );
//发送ARP查询包获得远程MAC地址
//Form1->Label1->Caption = GetLastError();
unsigned char macAddress[6];
ULONG macAddLen = 6;
iRet=SendARP(nRemoteAddr, (unsigned long)NULL,(PULONG)&macAddress, &macAddLen);
char tmp[3];
if ( iRet == NO_ERROR )
{
char *p;
p = dest_mac;
for( int i =0; i<6; i++ )
{
/*if(macAddress[i]<10)strcat(dest_mac,"0");
sprintf(tmp, "%2x", macAddress[i] );
strncat(dest_mac,tmp,2);*/
p += sprintf(p, i ? "%02X:" : "%02X:", macAddress[i]);
}
}
else
//printf( "SendARP Error:%d\n", GetLastError());
strcpy(dest_mac,"null");
nRemoteAddr = htonl( ntohl( nRemoteAddr ) + 1 );
}
dest_mac[17]= 0;
return dest_mac;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -