百科 教育 动画 游戏 博览 网址 金融 搜搜 资料
触屏版

在线: 4929   » [用户登录]   QQ   » [注册]
close ◇ 读取数据,请稍候 Loading...

.: Welcome to flymote.com [flymot.com] :.



  »

◆ 其它资料:.

VC 获远程机器名与网卡MAC地址(或本地) 物理地址

2005-10-05 17:30:00   李茂祥      阅读: 2386   【本类别其它资料】 【资料库首页
上一篇 收藏 打印 收藏夹 下一篇

〖VC 获远程机器名与网卡MAC地址(或本地) 物理地址〗
//必须接好网线,使用Winsock 及 iphlpapi
#include <stdio.h>
#include <stdlib.h>
#include <Winsock2.h>
#include <iphlpapi.h>
#include <windows.h>
#include <iostream>


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

void main( int argc, char ** argv )
{

typedef struct _MIB_IPNETROW
{ DWORD dwIndex;
DWORD dwPhysAddrLen;
BYTE bPhysAddr[MAXLEN_PHYSADDR];
DWORD dwAddr;
DWORD dwType;
} MIB_IPNETROW, *PMIB_IPNETROW;

typedef struct _ASTAT_
{
ADAPTER_STATUS adapt;
NAME_BUFFER NameBuff [30];
}ASTAT, * PASTAT;

int iRet;
struct in_addr sa;
unsigned char macAddress[6];
HOSTENT* remoteHostent;
IPAddr nRemoteAddr;
ULONG macAddLen = 6;
WSADATA wsaData;
// ASTAT Adapter;
//MIB_IPNETROW dwIndex;
// MIB_IPNETROW dwaddr;

typedef DWORD(CALLBACK * PFLUSHIPNETTABLE)(DWORD);

HINSTANCE hInst;
hInst=LoadLibrary("iphlpapi.dll");
nRemoteAddr = inet_addr( "192.168.1.2" );
//处理命令行参数
if ( argc == 2)
{
nRemoteAddr = inet_addr(argv[1]);
}else
{
printf( "RmtHost v0.4 - Get remote HostName and MacAddress\\n" );
printf( "Usage :\\n\tRmtHost.exe [RemoteIP] \\n\\n" );
printf( "Example:\\n\\tRmtHost.exe 192.168.1.2\\n" );
}

//初始化SOCKET
iRet = WSAStartup(MAKEWORD(2,1), &wsaData);
if ( iRet != 0 )
{
printf( "WSAStartup Error:%d\\n", GetLastError() );
exit( 0 );
}

// remoteHostent= (struct hostent*)malloc( sizeof(struct hostent )); //这一句多余,反而可能出错
//获取远程机器名
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 );
}
else
{
printf( "gethostbyaddr Error:%d\\n",GetLastError() );
}
//发送ARP查询包获得远程MAC地址

iRet=SendARP(nRemoteAddr, (unsigned long)NULL,(PULONG)&macAddress, &macAddLen);
if ( iRet == NO_ERROR )
{
printf("MacAddress is: %02X-%02X-%02X-%02X-%02X-%02X\n", macAddress[0], macAddress[1], macAddress[2], macAddress[3], macAddress[4], macAddress[5]);
}
else
{
printf( "SendARP Error:%d\\n", GetLastError());
}
}
责任编辑:

【最近的搜索】:

上一篇 收藏 打印 收藏夹 下一篇
CopyRight(c) 2007 - 2020 All Rights Reserved  【赣ICP备12001042号】
触屏版 | Archiver 20200402 18:51 | 简介 | 帮助 | 留言 | 关于 | 360网站安全检测平台