精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>编程开发>>C/C++>>网络与通讯>>如何取得MAC地址>>获取其他主机的MAC地址

主题:获取其他主机的MAC地址
发信人: iceant()
整理人: wenbobo(2003-08-26 16:58:07), 站内信件
【 在 i0519 的大作中提到:】
:请问如何通过VC获取以太网的MAC地址?
:......

从网上摘录的
============================================================

#include "stdio.h"
#include "stdlib.h"
#include "Winsock2.h"
#include "iphlpapi.h"
#include <iostream>

#pragma comment ( lib, "ws2_32.lib" )
#pragma comment ( lib, "Iphlpapi.lib" )

void main( int argc, char ** argv )
{
  //处理命令行参数
  if ( argc != 2 )
  {
    printf( "RmtHost v0.1 - Get remote HostName /MacAddress\n" );
    printf( "by ShotgunLabs ( [email protected] )\n\n" );
    printf( "Usage :\n\tRmtHost.exe [RemoteIP]\n\n");
    printf( "Example:\n\tRmtHost.exe 192.168.0.3\n\n");
    exit( 0 );
  }

  printf( "\nIpAddress : %s\n", argv[1] );
  //初始化SOCKET
  WSADATA wsaData;
  int iRet = WSAStartup(MAKEWORD(2,1), &wsaData);
  if ( iRet != 0 )
  {
    printf( "WSAStartup Error:%d\n", GetLastError() );
    exit( 0 );
  }
  //获取远程机器名
  hostent * remoteHostent = (hostent*)malloc( sizeof( hostent ));
  int nRemoteAddr = inet_addr( argv[1] );
  remoteHostent = gethostbyaddr( (char*)&nRemoteAddr,sizeof( in_addr ), AF_INET );
  if ( remoteHostent )
  printf( "HostName : %s\n",remoteHostent->h_name );
  else
  printf( "gethostbyaddr Error:%d\n", GetLastError());

  //发送ARP查询包获得远程MAC地址
  unsigned char macAddress[6];
  ULONG macAddLen = 6;
  iRet = SendARP( nRemoteAddr, NULL, (PULONG)&macAddress,&macAddLen );
  if ( iRet == NO_ERROR )
  {
    printf( "MacAddress: " );
    for( int i =0; i<6; i++ )
{
printf( "%.2x", macAddress[i] );
if ( i<5 ) printf( "-" );
}
}
else
printf( "SendARP Error:%d\n", GetLastError() );
printf( "\n" );
}



----
看得越多,想得越多,
自已就越渺小
还是好好学习吧

[关闭][返回]