Using WMI to obtain system information procedures
As an example of obtaining a native system, the process of obtaining system information is as follows:
1 initialization of the COM library
Since the WMI application was written in C + + based on COM technology, the COM library must be initialized, when the call function CoInitializeEx is used to initialize the COM library.
HRESULT hres;
hres = CoInitializeEx (0, coinit_multithreaded);
if (FAILED (hres))
... {
cout << "Failed to initialize COM library. Error code = 0x "
<< hex << hres << Endl;
return 1; Program has failed.
} 2 initialize COM library security level
The call function CoInitializeSecurity is used to initialize the COM security level.
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 1; Program has failed.
} 3) connecting to the WMI namespace
Initializes the WMI locator (an instance of the IWbemLocator type) by calling CoCreateInstance.
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 1; Program has failed.
{4) Creating the WMI service
Calls the IWbemLocator::ConnectServer method, which connects to the WMI namespace through this locator, and creates the service by passing an instance of IWbemServices to the ConnectServer method as a parameter. If we need some BIOS information, the WMI provider you need to use is Win32_BIOS, and you need to connect to the Root//cimv2 namespace.
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
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.