Get computer information using WMI

xiaoxiao2021-04-02  227

// WMI.cpp: Defines the entry point of the console application. //

/ * EXAMPLE: Getting WMI Data from The Local Computer

Http://www.dx21.com/scripting/wmi/classes.asp You can view all classes and available properties

// Reference http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/example__getting_wmi_data_from_the_local_computer.asp*/

#include "stdafx.h"

#define _win32_dcom # include #include #include

Using namespace std;

# Pragma Comment (Lib, "WbemuuiD.lib")

INT _TMAIN (int Argc, _tchar * argv []) {hResult hres; // step 1: --------------------------- -------------------- // Initialize COM. ------------------------ ------------------

HRES = CoinitializeEx (0, Coinit_Multithreaded); if (failed (hres)) {cout << "failed to initialize CORARY. Error code = 0x" << HEX << hres << endl; return 1; // Program Has Failed }

// STEP 2: --------------------------------------------- ----- // Set General Com Security Levels -------------------------- // Note: if you are useless 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 coinitializeigurecurity ------------------------------------------------------------------------------------ --------

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); failed (hres)) {cout << "failed to initialize security. Error code = 0x" << Hex << hres << endl; counitialize (); returnitialize (); 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 <

// STEP 4: ----------------------------------------------- -------- // Connect to WMI THROUGH THE IWBEMLOCATOR :: ConnectServer Method

IWbemServices * pSvc = NULL; // 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"),. // Object Path of WMI Namespace Null, // user name. Null = current user null, // user password. Null = current 0, // locale. Null indeicates current null, // security flags. 0, // Authority EG 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 (); returnit 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 << "Could Not set proxy blanket. error code = 0x" << Hex << hres << Endl; psvc-> release (); Ploc-> Release (); counitialize () Return 1; // Program Has Failed.} // STEP 6: ---------------------------------- ---------------- // Use the wbemservices 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_OperatingSystem"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, & pEnumerator) IF (failed (hres)) {cout << "Query for Operating system name failed." << "= 0x" << Hex << hres << endl; psvc-> release (); ploc-> release (); Counitialize (); return 1; // program HAS failed.

// STEP 7: ---------------------------------------------- ---- // Get The Data from The Query in Step 6 ------------------- i iMClassObject * PClsobj; ulong ureturn = 0; While (penumerator) {HRESULT HR = Penumerator-> Next (WBEM_INFINITE, 1, & PCLSOBJ, & URETURN);

IF (0 == URETURN) {Break;}

Variant vtprop;

// Get the value of the name protection hr = pclsobj-> get (l "name", 0, & vtprop, 0, 0); wcout << os Name: << vtprop.bstrval << Endl; variantclear (& VtProp) }

// cleanup / / ======== psvc-> release (); Ploc-> Release (); penumerator-> release (); pclsobj-> release (); counitialize (); return 0; // PROGRAM SUCCESSFULLETED.}

转载请注明原文地址:https://www.9cbs.com/read-131403.html

New Post(0)