|
楼主 |
发表于 2007-12-12 10:37:04
|
显示全部楼层
获得正确本地MAC地址,区分虚拟网卡
- void GetLocalMAC(char *buf)
- {
- IP_ADAPTER_INFO *IpAdaptersInfo =NULL;
- IP_ADAPTER_INFO *IpAdaptersInfoHead =NULL;
- IpAdaptersInfo = (IP_ADAPTER_INFO *) GlobalAlloc(GPTR, sizeof(IP_ADAPTER_INFO ));
- if (IpAdaptersInfo == NULL)
- {
- return;
- }
- DWORD dwDataSize = sizeof( IP_ADAPTER_INFO );
- DWORD dwRetVal = GetAdaptersInfo(IpAdaptersInfo,&dwDataSize);
- if ( ERROR_SUCCESS != dwRetVal)
- {
- GlobalFree( IpAdaptersInfo );
- IpAdaptersInfo = NULL;
- if( ERROR_BUFFER_OVERFLOW == dwRetVal)
- {
- IpAdaptersInfo =(IP_ADAPTER_INFO *) GlobalAlloc( GPTR, dwDataSize );
- if (IpAdaptersInfo == NULL)
- {
- return;
- }
- if ( ERROR_SUCCESS != GetAdaptersInfo( IpAdaptersInfo, &dwDataSize ))
- {
- GlobalFree( IpAdaptersInfo );
- return;
- }
- }
- else
- {
- return;
- }
- }
- //Save the head pointer of IP_ADAPTER_INFO structures list.
- IpAdaptersInfoHead = IpAdaptersInfo;
- do{
- if (IsLocalAdapter(IpAdaptersInfo->AdapterName))
- {
- sprintf(buf,"%02x-%02x-%02x-%02x-%02x-%02x",
- IpAdaptersInfo->Address[0],
- IpAdaptersInfo->Address[1],
- IpAdaptersInfo->Address[2],
- IpAdaptersInfo->Address[3],
- IpAdaptersInfo->Address[4],
- IpAdaptersInfo->Address[5]);
- //
- break;
- }
- IpAdaptersInfo = IpAdaptersInfo->Next;
- }while (IpAdaptersInfo);
- if (IpAdaptersInfoHead != NULL)
- {
- GlobalFree( IpAdaptersInfoHead );
- }
- }
- BOOL IsLocalAdapter(char *pAdapterName)
- {
- BOOL ret_value = FALSE;
- #define NET_CARD_KEY _T("System\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}")
- char szDataBuf[MAX_PATH+1];
- DWORD dwDataLen = MAX_PATH;
- DWORD dwType = REG_SZ;
- HKEY hNetKey = NULL;
- HKEY hLocalNet = NULL;
- if(ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE, NET_CARD_KEY, 0, KEY_READ, &hNetKey))
- return FALSE;
- wsprintf(szDataBuf, "%s\\Connection", pAdapterName);
- if(ERROR_SUCCESS != RegOpenKeyEx(hNetKey ,szDataBuf ,0 ,KEY_READ, &hLocalNet))
- {
- RegCloseKey(hNetKey);
- return FALSE;
- }
- if (ERROR_SUCCESS != RegQueryValueEx(hLocalNet, "MediaSubType", 0, &dwType, (BYTE *)szDataBuf, &dwDataLen))
- {
- goto ret;
- }
- if (*((DWORD *)szDataBuf)!=0x01)
- goto ret;
- dwDataLen = MAX_PATH;
- if (ERROR_SUCCESS != RegQueryValueEx(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;
- }
复制代码
以前的那个无法区分虚拟网卡,所以在得到MAC地址时会出现大量重复的现象,非常麻烦,这个就没有问题了。
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\{ACA306D0-1D69-4116-BC2B-919B428AD084}\Connection
网卡在注册表里的位置
区分PnpInstanceID,如果前面有PCI就是本机的真实网卡,MediaSubType为01则是常见网卡,02为无线网卡。 |
|