您的位置:首页 > 其它

Windows客户端开发--获取系统mac地址(使用WMI)

2016-11-21 14:56 323 查看
之前写过两篇博客,介绍了windows的WMI技术,以及如果通过WMI获取显卡详细信息:

Windows客户端开发–WMI技术介绍

Windows客户端开发–使用WMI获取显卡详细信息(win32控制台程序)

关于获取电脑的mac地址,之前也有相关博客进行了介绍,使用的是QT:

qt中查看本机mac/ip地址

什么是mac地址?

MAC(Media Access Control或者Medium Access Control)地址,意译为媒体访问控制,或称为物理地址、硬件地址,用来定义网络设备的位置。在OSI模型中,第三层网络层负责 IP地址,第二层数据链路层则负责 MAC地址。因此一个主机会有一个MAC地址,而每个网络位置会有一个专属于它的IP地址。

WIKI上是这样描述的:

This article is about the network addressing term. For the series of personal computers by Apple Inc., see Macintosh. For other similar terms, see Mac.

Label of an UMTS router with MAC addresses for LAN and WLAN modules

A media access control address (MAC address) of a computer is a unique identifier assigned to network interfaces for communications at the data link layer of a network segment. MAC addresses are used as a network address for most IEEE 802 network technologies, including Ethernet and WiFi. Logically, MAC addresses are used in the media access control protocol sublayer of the OSI reference model.

MAC addresses are most often assigned by the manufacturer of a network interface controller (NIC) and are stored in its hardware, such as the card’s read-only memory or some other firmware mechanism. If assigned by the manufacturer, a MAC address usually encodes the manufacturer’s registered identification number and may be referred to as the burned-in address (BIA). It may also be known as an Ethernet hardware address (EHA), hardware address or physical address (not to be confused with a memory physical address). This can be contrasted to a programmed address, where the host device issues commands to the NIC to use an arbitrary address.

通过WMI获取本机的mac地址:

因为之前已经写过了,所以直接就上代码了:

#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <comdef.h>
#include <Wbemidl.h>
#include <string>

#pragma comment(lib, "wbemuuid.lib")

int main(int argc, char **argv)
{
HRESULT hres;

// Initialize COM.
hres = CoInitializeEx(0, COINIT_APARTMENTTHREADED);
if (FAILED(hres))
{
cout << "Failed to initialize COM library. "
<< "Error code = 0x"
<< hex << hres << endl;
return 1;              // Program has failed.
}

// Initialize
hres = CoInitializeSecurity(
NULL,
-1,      // COM negotiates service
NULL,    // Authentication services
NULL,    // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT,    // authentication
RPC_C_IMP_LEVEL_IMPERSONATE,  // Impersonation
NULL,             // Authentication info
EOAC_NONE,        // Additional capabilities
NULL              // Reserved
);

if (FAILED(hres))
{
cout << "Failed to initialize security. "
<< "Error code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1;          // Program has failed.
}

// Obtain the initial locator to Windows Management
// on a particular host computer.
IWbemLocator *pLoc = 0;

hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *)&pLoc);

if (FAILED(hres))
{
cout << "Failed to create IWbemLocator object. "
<< "Error code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1;       // Program has failed.
}

IWbemServices *pSvc = 0;

// Connect to the root\cimv2 namespace with the
// current user and obtain pointer pSvc
// to make IWbemServices calls.

hres = pLoc->ConnectServer(

_bstr_t(L"ROOT\\CIMV2"), // WMI namespace
NULL,                    // User name
NULL,                    // User password
0,                       // Locale
NULL,                    // Security flags
0,                       // Authority
0,                       // Context object
&pSvc                    // IWbemServices proxy
);

if (FAILED(hres))
{
cout << "Could not connect. Error code = 0x"
<< hex << hres << endl;
pLoc->Release();
CoUninitialize();
return 1;                // Program has failed.
}

cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;

// Set the IWbemServices proxy so that impersonation
// of the user (client) occurs.
hres = CoSetProxyBlanket(

pSvc,                         // the proxy to set
RPC_C_AUTHN_WINNT,            // authentication service
RPC_C_AUTHZ_NONE,             // authorization service
NULL,                         // Server principal name
RPC_C_AUTHN_LEVEL_CALL,       // authentication level
RPC_C_IMP_LEVEL_IMPERSONATE,  // impersonation level
NULL,                         // client identity
EOAC_NONE                     // proxy capabilities
);

if (FAILED(hres))
{
cout << "Could not set proxy blanket. Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;               // Program has failed.
}

// Use the IWbemServices pointer to make requests of WMI.
// Make requests here:

// For example, query for all the running processes
IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_NetworkAdapter"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);

if (FAILED(hres))
{
cout << "Query for processes failed. "
<< "Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;               // Program has failed.
}
else
{
IWbemClassObject *pclsObj;
ULONG uReturn = 0;

while (pEnumerator)
{
hres = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);
if (0 == uReturn)
{
break;
}

VARIANT vtProp;
VARIANT vtProp2;

// Get the value of the Name property
hres = pclsObj->Get(L"Description", 0, &vtProp, 0, 0);
WCHAR* des;
des = vtProp.bstrVal;

hres = pclsObj->Get(L"MACAddress", 0, &vtProp2, 0, 0);
WCHAR* mac;
mac = vtProp2.bstrVal;

if (mac != NULL && des != NULL)
{
wcout << "Description: " << des << endl;
wcout << "Description: " << mac << endl;
}

VariantClear(&vtProp);
VariantClear(&vtProp2);
}

}

// Cleanup
// ========

pSvc->Release();
pLoc->Release();
CoUninitialize();

return 0;
}


输出结果:

Connected to ROOT\CIMV2 WMI namespace
Description: Intel(R) Dual Band Wireless-AC 3160
Description: E4:F8:9C:D9:84:B0

Description: VMware Virtual Ethernet Adapter for VMnet1
Description: 00:50:56:C0:00:01

Description: VMware Virtual Ethernet Adapter for VMnet8
Description: 00:50:56:C0:00:08

Description: Realtek PCIe GBE Family Controller
Description: F0:76:1C:F3:2B:1B

Description: Microsoft Wi-Fi Direct Virtual Adapter
Description: E4:F8:9C:D9:84:B1

Description: WAN Miniport (IP)
Description: 80:62:20:52:41:53

Description: WAN Miniport (IPv6)
Description: 5E:13:20:52:41:53

Description: WAN Miniport (Network Monitor)
Description: 68:EE:20:52:41:53
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: