VC中如何获取本地计算机MAC地址
6个回答
展开全部
#include "stdafx.h"
#include <Windows.h>
#include <iphlpapi.h>
#pragma comment(lib,"iphlpapi.lib")
int _tmain(int argc, _TCHAR* argv[])
{
IP_ADAPTER_INFO adapter[5]; //Maximum 5 adapters
DWORD buflen=sizeof(adapter);
DWORD status=GetAdaptersInfo(adapter,&buflen);
BYTE s[8];
if(status==ERROR_SUCCESS)
{
PIP_ADAPTER_INFO painfo=adapter;
memcpy(s,painfo->Address,6);
printf("Physical Address:%02X-%02X-%02X-%02X-%02X-%02X\n",s[0],s[1],s[2],s[3],s[4],s[5]);
return TRUE;
}
#include <Windows.h>
#include <iphlpapi.h>
#pragma comment(lib,"iphlpapi.lib")
int _tmain(int argc, _TCHAR* argv[])
{
IP_ADAPTER_INFO adapter[5]; //Maximum 5 adapters
DWORD buflen=sizeof(adapter);
DWORD status=GetAdaptersInfo(adapter,&buflen);
BYTE s[8];
if(status==ERROR_SUCCESS)
{
PIP_ADAPTER_INFO painfo=adapter;
memcpy(s,painfo->Address,6);
printf("Physical Address:%02X-%02X-%02X-%02X-%02X-%02X\n",s[0],s[1],s[2],s[3],s[4],s[5]);
return TRUE;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-07-27
展开全部
大概的步骤如下 1.获得子网掩码和gateway IP地址 2.AND操作获得局域网IP启示段,OR操作获得最大范围 3.向这个区域的每个IP,发送ARP(地址解析协议),获得的计算机会返回给你消息 4.至于如何发送ARP,window下面提供了SendARP interface,也可以使用第3方 driver 根据这个流程,对应google搜集资料,可以实现 --------------------------------------------------------------- 先用 DhcpEnumSubnets 枚举子网, 会有很多。 wchar_t wcIP[255]; //把dhcp主机的IP放在wcIP中 DHCP_RESUME_HANDLE hResumeHandle = 0; DWORD PrefMax = 255;//预计的最大租约个数 DWORD dwRead; DWORD dwTotal; LPDHCP_IP_ARRAY IPArray = NULL; DWORD dwRet ; dwRet = DhcpEnumSubnets(wcIP, &hResumeHandle, PrefMax, &IPArray , &dwRead, &dwTotal); 然后在子网的基础上, 用DhcpEnumSubnetClients枚举租约 for (DWORD i = 0; i < dwRead; i ++) { DWORD ip = IPArray->Elements[i];//子网地址 //str.Format("(%d) IP = %s\n", i, IP2Str(htonl(ip))); //AfxMessageBox(str); DHCP_RESUME_HANDLE hRH = 0; LPDHCP_CLIENT_INFO_ARRAY clientArray = NULL; DWORD PrefMaxClient = 65535; DWORD ClientsRead, ClientsTotal; DhcpEnumSubnetClients(wcIP, ip, &hRH, PrefMaxClient, &clientArray,&ClientsRead, &ClientsTotal); for (DWORD j = 0; j < ClientsRead; j ++) { //输出客户地址 DHCP_IP_ADDRESS clientIP = clientArray->Clients[j]->ClientIpAddress; //这个是主机机名, 宽字串的:clientArray->Clients[j]->ClientName //MAC地址:一般是6个 clientArray->Clients[j]->ClientHardwareAddress->Data } } } wangnan3 http://zhidao.baidu.com/question/43836243.html 2 怎样取消预编译头文件?~~~~~ 答案: 这里没装VC6``忘了那几个名字 你去project-setting,然后在第一个(或第二个)tab页,会看到左边有一个树,你选择对应要取消预编译头的文件,然后在右边,会看到一个combo box,选择process.. header``然后会有4个单选框,你选择第一个no...什么的就可以了 --------------------------------------------------------------- vc++IDE--->Project--->Setting--->c/c++--->Category--->PreCompiled Headers--->Not Use Precompiled Headers专家提供:回答者: 张琎 - 软件专家 1-16 10:02我来评论>>评价已经被关闭 目前有 0 个人评价 好50% (0) 不好50% (0) 相关内容 路由器安装设置向导 局域网某几台机子上不了网,从DHCP获取IP失败 明明可以从DHCP获取IP,为什么显示"DHCP Enabled........ 本地连接通过DHCP获取IP(192.168.200.61),无法登... 上不了网,本地连接提示DHCP获取IP地址失败。是什么... 查看同主题问题:分配 ip dhcp 其他回答 共 1 条//将CString 型IP地址在IPAddressCtrl中显示 CString strIP; char *name = strIP.GetBuffer(100); char *ip; PHOSTENT hostinfo; TRACE1("%d\n", sizeof(name)); if( gethostname (name , 100) == 0) { if((hostinfo = gethostbyname(name)) != NULL) { //这些就是获得IP的函数 ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list); strIP.ReleaseBuffer(); strIP = ip; } } DWORD dwIP; dwIP = inet_addr(strIP); unsigned char *pIP = (unsigned char*)&dwIP;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-07-27
展开全部
获取网卡的MAC地址的方法很多,如:Netbios,SNMP,GetAdaptersInfo等。经过测试发现 Netbios 方法在网线拔出的情况下获取不到MAC,而 SNMP 方法有时会获取多个重复的网卡的MAC,试来试去还是 GetAdaptersInfo 方法比较好,网线拔出的情况下可以获取MAC,而且很准确,不会重复获取网卡。
GetAdaptersInfo 方法也不是十全十美,也存在些问题:
GetAdaptersInfo 方法也不是十全十美,也存在些问题:
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-07-27
展开全部
#include <afxwin.h>
#include "global.h"
#include"include/pcap.h"
#pragma pack(push)
#pragma pack(1)
typedef struct tagARPFrame
{
u_char8 DestMAC[6];
u_char8 SourMAC[6];
u_char8 EthType[2];
unsigned short HW_Type; /* hardware address */
unsigned short Prot_Type; /* protocol address */
unsigned char HW_Addr_Len; /* length of hardware address */
unsigned char Prot_Addr_Len; /* length of protocol address */
unsigned short Opcode; /* ARP/RARP */
unsigned char Send_HW_Addr[6]; /* sender hardware address */
unsigned char Send_Prot_Addr[4]; /* sender protocol address */
unsigned char Targ_HW_Addr[6]; /* target hardware address */
unsigned char Targ_Prot_Addr[4]; /* target protocol address */
unsigned char padding[18];
} ARPPACKET, *PARPPACKET;
#pragma pack(pop)
class CSendARP
{
public:
CSendARP(const HWND hDlg);
~CSendARP(void);
BOOL Start();
void Init(const USERDATA* pUserData);
void Close();
private:
const USERDATA* m_pData;
pcap_t* m_fp;
pcap_t* m_fp2;
bool SendARP_Multiple(ARPPACKET & packet,int ip3);
bool OpenAdapter();
friend UINT WorkProc_Send(LPVOID lpParam);
friend UINT WorkProc_Recv(LPVOID lpParam);
protected:
HWND m_hDlg;
CWinThread* Thread_Send;
CWinThread* Thread_Recv;
int Continue;
int nCount;
UCHAR mac[6];
int d_ip3;
int __i;
};
#include "global.h"
#include"include/pcap.h"
#pragma pack(push)
#pragma pack(1)
typedef struct tagARPFrame
{
u_char8 DestMAC[6];
u_char8 SourMAC[6];
u_char8 EthType[2];
unsigned short HW_Type; /* hardware address */
unsigned short Prot_Type; /* protocol address */
unsigned char HW_Addr_Len; /* length of hardware address */
unsigned char Prot_Addr_Len; /* length of protocol address */
unsigned short Opcode; /* ARP/RARP */
unsigned char Send_HW_Addr[6]; /* sender hardware address */
unsigned char Send_Prot_Addr[4]; /* sender protocol address */
unsigned char Targ_HW_Addr[6]; /* target hardware address */
unsigned char Targ_Prot_Addr[4]; /* target protocol address */
unsigned char padding[18];
} ARPPACKET, *PARPPACKET;
#pragma pack(pop)
class CSendARP
{
public:
CSendARP(const HWND hDlg);
~CSendARP(void);
BOOL Start();
void Init(const USERDATA* pUserData);
void Close();
private:
const USERDATA* m_pData;
pcap_t* m_fp;
pcap_t* m_fp2;
bool SendARP_Multiple(ARPPACKET & packet,int ip3);
bool OpenAdapter();
friend UINT WorkProc_Send(LPVOID lpParam);
friend UINT WorkProc_Recv(LPVOID lpParam);
protected:
HWND m_hDlg;
CWinThread* Thread_Send;
CWinThread* Thread_Recv;
int Continue;
int nCount;
UCHAR mac[6];
int d_ip3;
int __i;
};
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询