Get the NIC MAC address, hard drive serial number, motherboard serial number, CPU ID, BIOS serial number via WMI

Source: Internet
Author: User

Recently, due to the needs of the project, the need to get the machine's hard drive serial number and MAC address information, under C #, it is easy to obtain this information, but in the C + + program feel more trouble. After Baidu, found that a lot of prawns are through the WMI to obtain these hardware information, the network also has relevant code, through the actual debugging, but also found that it is indeed possible to obtain this information through WMI. Two days ago, on the internet suddenly found a more complete program, for later use, reproduced records. At the same time, in Daniel's code to add some of their own comments, are the actual use of their own problems encountered in the process.

#define _win32_dcom

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include "Direct.h"
#include <tchar.h>
#include <time.h>
#include <comdef.h>
#include <Wbemidl.h>
#include <conio.h>
#include "Atlstr.h"
#include "atlbase.h"
#include "TcpCtl.h"
#include "Winsock2.h"
#include "InitDll.h"
using namespace Std;

# pragma comment (lib, "Wbemuuid.lib")
# pragma comment (lib, "Ws2_32.lib")

Get Board number via WMI
BOOL Managewmibord (char bord[])
{
HRESULT hres;
Step 1: initialize COM
hres = CoInitializeEx (0, coinit_multithreaded); The code on the Web is initialized with this line of statements, but in practice, I find it possible to initialize with the following statement.
hres = CoInitialize (0);

The online code is not commented on the following judgment, but the actual use found that if it has been initialized successfully, at the time of the second initialization, the following code will cause the return false, so, in actual use I commented out
if (FAILED (hres))
//{
cout << "Failed to initialize COM library. Error code = 0x "
<< hex << hres << Endl;
return false; Program has failed.
//}


Step 2: Set the security authentication level for COM

In the actual use of the process, I found that if this step is not commented out, the program always return false, comment out after the program can run normally, for unknown reason

Note:if you is using Windows need to specify-
The default authentication credentials for a user by using
A sole_authentication_list structure in the pauthlist----
parameter of CoInitializeSecurity------------------------
hres = CoInitializeSecurity (
Null
-1,//COM Authentication
NULL,//authentication Services
NULL,//Reserved
Rpc_c_authn_level_default,//DEFAULT Authentication
Rpc_c_imp_level_impersonate,//Default 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 false; Program has failed.
////}

Step 3:get the WMI connection COM interface
IWbemLocator *ploc = NULL;
hres = CoCreateInstance (
Clsid_wbemlocator,
0,
Clsctx_inproc_server,
Iid_iwbemlocator, (LPVOID *) &ploc);

if (FAILED (hres))
{
cout << "Failed to create IWbemLocator object."
<< "ERR code = 0x"
<< hex << hres << Endl;
CoUninitialize ();
return false; Program has failed.
}


Step 4: Connect WMI's Kernel object name "root//cimv2" through the Connection interface
IWbemServices *psvc = NULL;

hres = Ploc->connectserver (

_bstr_t (L "root\\cimv2"),//Object Path of WMI namespace
NULL,//User name. NULL = Current User
NULL,//User password. NULL = Current
0,//Locale. NULL indicates current
NULL,//Security flags.
0,//authority (e.g. Kerberos)
0,//Context Object
&PSVC//Pointer to IWbemServices Proxy
);

if (FAILED (hres))
{
cout << "Could not connect. Error code = 0x "
<< hex << hres << Endl;
Ploc->release ();
CoUninitialize ();
return false; Program has failed.
}
cout << "Connected to root\\cimv2 WMI namespace" << Endl;

Step 5:set the security level of the request agent
hres = CoSetProxyBlanket (
PSVC,//Indicates the proxy to set
Rpc_c_authn_winnt,//Rpc_c_authn_xxx
Rpc_c_authz_none,//Rpc_c_authz_xxx
NULL,//Server principal Name
Rpc_c_authn_level_call,//Rpc_c_authn_level_xxx
Rpc_c_imp_level_impersonate,//Rpc_c_imp_level_xxx
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 false; Program has failed.
}
Step 6: Send requests to WMI by requesting a proxy----
For example, get the name of the operating system
ienumwbemclassobject* penumerator = NULL;
hres = Psvc->execquery (
bstr_t ("WQL"),
bstr_t ("select * from Win32_NetworkAdapterConfiguration WHERE ipenabled = ' TRUE '),
bstr_t ("SELECT * from Win32_baseboard"),//Only need to modify the query statement here, you can implement the MAC address and other information such as query
wbem_flag_forward_only | Wbem_flag_return_immediately,
Null
&penumerator);

if (FAILED (hres))
{
cout << "Query for Network Adapter Configuration failed."
<< "Error code = 0x"
<< hex << hres << Endl;
Psvc->release ();
Ploc->release ();
CoUninitialize ();
return false; Program has failed.
}


Step 7: Iterate through all the result objects

IWbemClassObject *pclsobj;
ULONG Ureturn = 0;

while (Penumerator)
{
HRESULT hr = Penumerator->next (wbem_infinite, 1,
&pclsobj, &ureturn);
if (0 = = Ureturn)
{
Break
}
VARIANT Vtprop;
VariantInit (&vtprop);

hr = Pclsobj->get (L "SerialNumber", 0, &vtprop, 0, 0);//query different hardware information, in addition to modify the above query statement, the field here also to modify

if (! FAILED (HR))
{
CW2A tmpstr1 (Vtprop.bstrval);
strcpy_s (BORD,200,TMPSTR1);//The 200 here is adjustable, set according to the actual situation, but certainly not greater than the length of the Bord
cout << "BORDSN:" << sn << endl;

}

VariantClear (&vtprop);
}//end while

Freeing Resources
Psvc->release ();
Ploc->release ();
Penumerator->release ();
Pclsobj->release ();
CoUninitialize ();
return true; Program successfully completed.

}

Report:

WQL query Statement
Const T_wql_query szwqlquery[] = {
Nic Native MAC Address
"SELECT * from Win32_NetworkAdapter WHERE (MACAddress was not NULL) and (not (Pnpdeviceid like ' root% ')"),
L "Pnpdeviceid",

Hard Drive serial Number
"SELECT * from Win32_DiskDrive WHERE (SerialNumber was not NULL) and (mediatype like ' Fixed hard disk% ')",
L "SerialNumber",

Motherboard serial Number
"SELECT * from Win32_baseboard where (SerialNumber are not NULL)",
L "SerialNumber",

Processor ID
"SELECT * from Win32_Processor where (Processorid are not NULL)",
L "Processorid",

BIOS Serial Number
"SELECT * from Win32_BIOS where (SerialNumber are not NULL)",
L "SerialNumber",

Motherboard model
"SELECT * from Win32_baseboard where (Product was not NULL)",
L "Product",

Network card current MAC address
"SELECT * from Win32_NetworkAdapter WHERE (MACAddress was not NULL) and (not (Pnpdeviceid like ' root% ')"),
L "MACAddress",

Model and manufacturer of the current machine

"SELECT * from Win32_ComputerSystem",

L "Manufacturer", L "Model"


}

(GO) Get the NIC MAC address, hard drive serial number, motherboard serial number, CPU ID, BIOS serial number via WMI

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.