# Include "stdafx. H"
# DEFINE _ win32_dcom
# Include <iostream>
Using namespace STD;
# Include <comdef. h>
# Include <wbemidl. h>
# Include <conio. h>
# Pragma comment (Lib, "wbemuuid. lib ")
Bool managewmi ();
Int _ tmain (INT argc, _ tchar * argv [])
{
If (! Managewmi () printf ("% WMI error! ");
_ Getch ();
Return 0;
}
Bool managewmi ()
{
Hresult hres;
// Step 1 :--------------------------------------------------
// Initialize com .------------------------------------------
Hres = coinitializeex (0, coinit_multithreaded );
If (failed (hres ))
{
Cout <"failed to initialize com library. Error code = 0x"
<Hex Return 1; // program has failed.
}
// Step 2 :--------------------------------------------------
// Set general com security levels --------------------------
// Note: if you are using Windows 2000, you 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 Couninitialize ();
Return 1; // program has failed.
}
// Step 3 :---------------------------------------------------
// Obtain the initial locator to WMI -------------------------
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 Couninitialize ();
Return 1; // program has failed.
}
// Step 4 :-----------------------------------------------------
// Connect to WMI through the iwbemlocator: connectserver Method
Iwbemservices * psvc = NULL;
// Connect to the root/cimv2 namespace
// The current user and obtain pointer psvc
// To make iwbemservices CILS.
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 <"cocould not connect. Error code = 0x"
<Hex Ploc-> release ();
Couninitialize ();
Return 1; // program has failed.
}
Cout <"connected to root // cimv2 WMI namespace" <Endl;
// Step 5 :--------------------------------------------------
// Set security levels on the proxy -------------------------
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 <"cocould not set proxy blanket. Error code = 0x"
<Hex Psvc-> release ();
Ploc-> release ();
Couninitialize ();
Return 1; // program has failed.
}
// Step 6 :--------------------------------------------------
// Use the iwbemservices pointer to make requests of WMI ----
// 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 '"),
Wbem_flag_forward_only | wbem_flag_return_immediately,
Null,
& Penumerator );
If (failed (hres ))
{
Cout <"query for network adapter configuration failed ."
<"Error code = 0x"
<Hex Psvc-> release ();
Ploc-> release ();
Couninitialize ();
Return 1; // program has failed.
}
// Step 7 :-------------------------------------------------
// Get the data from the query in Step 6 -------------------
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 "ipsubnet", 0, & vtprop, 0, 0 );
// Get the value of the enable property
HR = pclsobj-> get (L "ipenabled", 0, & vtprop, 0, 0 );
// Get the value of the macaddress Property
If (vtprop. boolval)
{
HR = pclsobj-> get (L "macaddress", 0, & vtprop, 0, 0 );
Wcout <"macaddress:" <vtprop. bstrval <Endl;
}
HR = pclsobj-> get (L "ipenabled", 0, & vtprop, 0, 0 );
If (vtprop. boolval)
{
Long lstart, lend;
Long idx =-1;
BSTR * pbstr;
Safearray * Sa;
HR = pclsobj-> get (L "Description", 0, & vtprop, 0, 0 );
If (! Failed (HR ))
{
Wcout <"Description:" <vtprop. bstrval <Endl;
}
HR = pclsobj-> get (L "dnshostname", 0, & vtprop, 0, 0 );
If (! Failed (HR ))
{
Cout <"DNS:" <vtprop. bstrval <Endl;
}
HR = pclsobj-> get (L "IPaddress", 0, & vtprop, 0, 0 );
If (! Failed (HR ))
{
Safearray * PSA = vtprop. parray;
Cout <"IP Address:" <PSA <Endl;
}
}
Variantclear (& vtprop );
}
// Cleanup
// ==========
Psvc-> release ();
Ploc-> release ();
Penumerator-> release ();
Pclsobj-> release ();
Couninitialize ();
Return 0; // program successfully completed.
}