獲取有線網卡的物理地址


BOOL GetMacAddress(UCHAR MacAddress[8])
{
	BOOL bRet = FALSE;
	PIP_ADAPTER_INFO lpAdapterInfo = NULL;
	ULONG nSize = 0;

	if (GetAdaptersInfo(lpAdapterInfo, &nSize) == ERROR_BUFFER_OVERFLOW)
	{
		if (lpAdapterInfo = (PIP_ADAPTER_INFO)GlobalAlloc(GPTR, nSize))
		{
			if (GetAdaptersInfo(lpAdapterInfo, &nSize) == ERROR_SUCCESS)
			{
				for (PIP_ADAPTER_INFO pos = lpAdapterInfo; pos; pos = pos->Next)
				{
					if (pos->Type == MIB_IF_TYPE_ETHERNET)
					{
						HKEY hKey = NULL;
						TCHAR RegPath[MAX_PATH];

						wsprintf(RegPath, TEXT("SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\")
#ifdef UNICODE
							TEXT("%S")
#else
							TEXT("%s")
#endif
							TEXT("\\Connection"), pos->AdapterName
							);
						if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, RegPath, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
						{
							TCHAR szText[MAX_PATH];
							DWORD cbSize = sizeof(szText);

							if (RegQueryValueEx(hKey, TEXT("PnPInstanceId"), NULL, NULL, (PBYTE)szText, &cbSize) == ERROR_SUCCESS)
							{
								if (_tcsnicmp(szText, TEXT("pci\\"), 4) == 0)
								{
									bRet = TRUE;
								}
							}
							RegCloseKey(hKey);
						}
					}
					if (bRet)
					{
						for (UINT i = 0; i < 8; i++)
						{
							MacAddress[i] = pos->Address[i];
						}
						break;//for
					}
				}
			}
			GlobalFree(lpAdapterInfo);
		}
	}
	return bRet;
}


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
  © 2014-2022 ITdaan.com 联系我们: