首页
社区
课程
招聘
[原创]放一段获取网卡物理地址的代码(申请邀请码)
发表于: 2010-7-16 14:53 4212

[原创]放一段获取网卡物理地址的代码(申请邀请码)

2010-7-16 14:53
4212
BOOL GetPhysicMacAddrList(list<string>* xList)
{

	xList->clear();

	CString strNetCardString;
	CString strKey;
	HKEY hKey;
	ULONG iSubKey;
	LONG nResult;
    TCHAR szKeyName[MAX_PATH+1];
	ULONG nKeyNameLen;
	

	strKey = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards";
	nResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, 
		strKey, 
		0, 
		KEY_READ,
		&hKey );

	if (nResult != ERROR_SUCCESS)
	{
		return FALSE;
	}

	BOOL bFinish = FALSE;
    iSubKey = 0;
	while (!bFinish)
	{
		nKeyNameLen = sizeof(szKeyName)/sizeof(*szKeyName);
		nResult = RegEnumKeyEx(
			hKey,
			iSubKey,
			szKeyName,
			&nKeyNameLen,
			NULL,
			NULL,
			NULL,
			NULL);


		if((nResult != ERROR_SUCCESS) && (nResult != ERROR_NO_MORE_ITEMS))
		{
			break;
		}

		if (nResult == ERROR_NO_MORE_ITEMS)
			bFinish = TRUE;


		CString strKey2 = strKey;
		HKEY hKey2;
		strKey2 += "\\";
		strKey2 += CString(szKeyName);

		nResult = RegOpenKeyEx(
			HKEY_LOCAL_MACHINE, 
			strKey2, 
			0, 
			KEY_READ,
			&hKey2 );

		if (nResult != ERROR_SUCCESS)
		{
			iSubKey++;
			continue;
		}

		DWORD dwType = REG_SZ;
		TCHAR szValue[MAX_PATH+1];

		ULONG nLeng = sizeof(szValue)/sizeof(*szValue);

		nLeng = sizeof(szValue)/sizeof(*szValue);
		nResult = RegQueryValueEx(hKey2,
			_T("ServiceName"),
			NULL,
			&dwType,
			(LPBYTE)szValue,
			&nLeng);

		if (nResult != ERROR_SUCCESS)
		{
			RegCloseKey(hKey2);
			iSubKey++;
			continue;
		}

		RegCloseKey(hKey2);

		CString strDirverName;
		strDirverName.Format("\\\\.\\%s",szValue);

		HANDLE hDriver = CreateFile(strDirverName,GENERIC_READ,  
                                                  0,  
                                                  NULL,  
                                                  OPEN_EXISTING,  
                                                  0,  
                                                  NULL);  
		if (hDriver == INVALID_HANDLE_VALUE)  
		{  
			iSubKey++;
			continue;
		} 

		UCHAR ucData[8] = {0};
		ULONG oid = OID_802_3_PERMANENT_ADDRESS;
		DWORD ByteRet;


		if(DeviceIoControl(hDriver,IOCTL_NDIS_QUERY_GLOBAL_STATS,    
				 &oid,
				 4,  
				 ucData,
				 sizeof(ucData),  
				 &ByteRet,  
				 NULL))
		 {
			
			char szTemp[MAX_BUFFER];
			RtlZeroMemory(szTemp,sizeof(szTemp));
			int nResult = _snprintf(szTemp,sizeof(szTemp),"%02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X",ucData[0],ucData[1],ucData[2],ucData[3],
				ucData[4],ucData[5],ucData[6],ucData[7]);

			if (nResult > 0 && nResult < sizeof(szTemp))
			{
				xList->push_back(szTemp);
			}
		}
		 CloseHandle(hDriver);
		 iSubKey++;
	}
	RegCloseKey(hKey);

	return TRUE;
}

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 7
支持
分享
最新回复 (1)
雪    币: 215
活跃值: (11)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
你那样会重复找一个
                if((nResult != ERROR_SUCCESS) || (nResult == ERROR_NO_MORE_ITEMS))
                {
                        break;
                }
2013-4-1 11:51
0
游客
登录 | 注册 方可回帖
返回
//