C++编写的 获取机器的mac地址的方法:
头文件:MyFuntions.h
// Base64.h: interface for the CBase64 class.
//
//
#if !defined(AFX_BASE64_H__CF8A6052_4D19_4BEB_807E_B8E47E65E64F__INCLUDED_)
#define AFX_BASE64_H__CF8A6052_4D19_4BEB_807E_B8E47E65E64F__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <windows.h>
#include <iphlpapi.h>
#include <stdio.h>
#pragma comment(lib, "IpHlpApi.lib ")
static char base64_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
enum Base64Option{BASE64_NeedCRLF=1};
class CAlgorithm
{
public:
/* static int hex2bin (char* indata, int inlen, unsigned char* outdata, int *outlen);
static int bin2hex (unsigned char* indata, int inlen, char* outdata, int *outlen);
static int base64_decode(const char *base64_data,long base64_size,unsigned char *bin_data,long *bin_size);
static int base64_encode(unsigned char * bin_data, long bin_size, char * base64_data,long * base64_size,int Mode = 0);*/
CAlgorithm();
virtual ~CAlgorithm();
static char* GetLocalAdapterAddress();
static BOOL IsLocalAdapter(char *pAdapterName);
};
#endif // !defined(AFX_BASE64_H__CF8A6052_4D19_4BEB_807E_B8E47E65E64F__INCLUDED_)
源文件:MyFuntions.cpp
// Base64.cpp: implementation of the CBase64 class.
//
//
#include "MyFuntions.h"
#include <windows.h>
//
// Construction/Destruction
//
#define NET_CARD_KEY "System\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}"
CAlgorithm::CAlgorithm()
{}
CAlgorithm::~CAlgorithm()
{}
/************************************************************************
用途说明:返回所有真实网卡地址列表,多个地址之间用|分隔
输入参数:无
返回结果:以"|"分隔的多个网卡地址列表
************************************************************************/
char* CAlgorithm::GetLocalAdapterAddress()
{
char strRet[MAX_PATH];
memset(strRet,0,MAX_PATH);
PIP_ADAPTER_INFO pAdapterInfo=NULL;
PIP_ADAPTER_INFO pAdapter=NULL;
ULONG ulOutBufLen=0;
char tmpBuffer[MAX_PATH];
//获得存放网卡数据区的长度
if(GetAdaptersInfo(NULL,&ulOutBufLen)!=ERROR_SUCCESS)
{
pAdapterInfo=(IP_ADAPTER_INFO*)malloc(ulOutBufLen);
}
//获取网卡数据
if((GetAdaptersInfo(pAdapterInfo,&ulOutBufLen))==NO_ERROR)
{
pAdapter=pAdapterInfo;
while(pAdapter)
{
{
if(IsLocalAdapter(pAdapter->AdapterName))
{
memset(tmpBuffer,0,MAX_PATH);
int len=strlen(strRet);
if(len!=0)
{
strRet[len]= '|';
}
for(UINT i=0;i<pAdapter->AddressLength;i++)
{
sprintf(tmpBuffer,"%02X",pAdapter->Address[i]);
// strRet += tmpBuffer;
strcat(strRet,tmpBuffer);
if(i < pAdapter->AddressLength-1)
{
int len2=strlen(strRet);
strRet[len2]= '-';
}
}
}
}
pAdapter=pAdapter->Next;
}
}
if(pAdapterInfo !=NULL)
{
free(pAdapterInfo);
}
return strRet;
}
/************************************************************************
用途说明:根据网卡适配器名称,判断该网卡是否是真实网卡
输入参数:pAdapterName 网卡适配器名称
返回结果:TRUE 该网卡是真实网卡 FALSE 不是真实网卡
************************************************************************/
BOOL CAlgorithm::IsLocalAdapter(char *pAdapterName)
{
BOOL ret_value = FALSE;
char szDataBuf[MAX_PATH+1];
DWORD dwDataLen = MAX_PATH;
DWORD dwType = REG_SZ;
HKEY hNetKey = NULL;
HKEY hLocalNet = NULL;
if(ERROR_SUCCESS != RegOpenKeyExA(HKEY_LOCAL_MACHINE, NET_CARD_KEY, 0, KEY_READ, &hNetKey))
return FALSE;
wsprintfA(szDataBuf, "%s\\Connection", pAdapterName);
if(ERROR_SUCCESS != RegOpenKeyExA(hNetKey ,szDataBuf ,0 ,KEY_READ, &hLocalNet))
{
RegCloseKey(hNetKey);
return FALSE;
}
if (ERROR_SUCCESS != RegQueryValueExA(hLocalNet, "MediaSubType", 0, &dwType, (BYTE *)szDataBuf, &dwDataLen))
{
goto ret;
}
if (*((DWORD *)szDataBuf)!=0x01 && *((DWORD *)szDataBuf)!=0x02)
goto ret;
dwDataLen = MAX_PATH;
if (ERROR_SUCCESS != RegQueryValueExA(hLocalNet, "PnpInstanceID", 0, &dwType, (BYTE *)szDataBuf, &dwDataLen))
{
goto ret;
}
if (strncmp(szDataBuf, "PCI", strlen("PCI")))
goto ret;
ret_value = TRUE;
ret:
RegCloseKey(hLocalNet);
RegCloseKey(hNetKey);
return ret_value;
}
-----------------------------------------------------------------------
//获取字符串长度
int getlen(char *result)
{
int i=0;
while(result[i]!='\0'){
i++;
}
return i;
}
-------------------------------------------------------------------------
//写入日志
void writeLogs(char* str)
{
time_t t = time(0);
char tmp[164];
strftime(tmp, sizeof(tmp), "%Y/%m/%d %X:",localtime(&t)); //获取当前时间
puts(tmp);
strcat(tmp,str);//时间+日志内容
strcat(tmp,"\r\n");//加入换行符
FILE *fp;
char fname[32] = "c:\\log.txt";/*文件名*/
fp = fopen(fname,"a+");//追加写入文本
fprintf(fp,tmp);
fclose(fp);
}