本文主要是介绍通过WMI技术获取操作系统的信息类Win32_OperatingSystem,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
项目比较时间比较紧张,WMI匆忙找了一下,不理解其中的大部分内容
从msdn上找了个例子一旦运行起来,心里也就有了着落了。
于是有了一些测试,也有了一些加深的理解
#include <stdio.h>
#define _WIN32_DCOM
#include <wbemidl.h>
# pragma comment(lib, "wbemuuid.lib")
#include <iostream>
using namespace std;
#include <comdef.h>void main(void)
{HRESULT hr;_bstr_t bstrNamespace;IWbemLocator *pWbemLocator = NULL;IWbemServices *pServices = NULL;IWbemClassObject *pDrive = NULL;hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);hr = CoInitializeSecurity(NULL, -1, NULL, NULL,RPC_C_AUTHN_LEVEL_CONNECT,RPC_C_IMP_LEVEL_IMPERSONATE,NULL, EOAC_NONE, 0);if (FAILED(hr)){CoUninitialize();cout << "Failed to initialize security. Error code = 0x" << hex << hr << endl;return;}hr = CoCreateInstance(CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (void**) &pWbemLocator);if( FAILED(hr) ) {CoUninitialize();printf("failed CoCreateInstance\n");return;}bstrNamespace = L"root\\cimv2";hr = pWbemLocator->ConnectServer(bstrNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pServices);if( FAILED(hr) ) {pWbemLocator->Release();CoUninitialize();printf("failed ConnectServer\n");return;}pWbemLocator->Release();printf("Successfully connected to namespace.\n");BSTR bstrPath = SysAllocString(L"Win32_LogicalDisk.DeviceID=\"C:\"");// *******************************************************//// Perform a full-instance retrieval. // *******************************************************//hr = pServices->GetObject(bstrPath,0,0, &pDrive, 0);if( FAILED(hr) ){ pServices->Release();CoUninitialize();printf("failed GetObject\n");return;} // Display the objectBSTR bstrDriveObj;hr = pDrive->GetObjectText(0, &bstrDriveObj);printf("%S\n\n", bstrDriveObj);pDrive->Release();pDrive = NULL;// *****************************************************//// Perform a partial-instance retrieval. // *****************************************************//IWbemContext *pctxDrive; // Create context objecthr = CoCreateInstance(CLSID_WbemContext, NULL, CLSCTX_INPROC_SERVER, IID_IWbemContext, (void**) &pctxDrive);if (FAILED(hr)){pServices->Release();pDrive->Release(); CoUninitialize();printf("create instance failed for context object.\n");return;}VARIANT vExtensions;VARIANT vClient;VARIANT vPropertyList;VARIANT vProperty;SAFEARRAY *psaProperties;SAFEARRAYBOUND saBounds;LONG lArrayIndex = 0;// Add named values to the context object. VariantInit(&vExtensions);V_VT(&vExtensions) = VT_BOOL;V_BOOL(&vExtensions) = VARIANT_TRUE;hr = pctxDrive->SetValue(_bstr_t(L"__GET_EXTENSIONS"), 0, &vExtensions);VariantClear(&vExtensions);VariantInit(&vClient);V_VT(&vClient) = VT_BOOL;V_BOOL(&vClient) = VARIANT_TRUE;hr = pctxDrive->SetValue(_bstr_t(L"__GET_EXT_CLIENT_REQUEST"), 0, &vClient);VariantClear(&vClient);// Create an array of properties to return.saBounds.cElements = 1;saBounds.lLbound = 0;psaProperties = SafeArrayCreate(VT_BSTR, 1, &saBounds);// Add the Caption property to the array.VariantInit(&vProperty);V_VT(&vProperty) = VT_BSTR;V_BSTR(&vProperty) = _bstr_t(L"Caption");SafeArrayPutElement(psaProperties, &lArrayIndex, V_BSTR(&vProperty));VariantClear(&vProperty);VariantInit(&vPropertyList);V_VT(&vPropertyList) = VT_ARRAY | VT_BSTR;V_ARRAY(&vPropertyList) = psaProperties;// Put the array in the named value __GET_EXT_PROPERTIES.hr = pctxDrive->SetValue(_bstr_t(L"__GET_EXT_PROPERTIES"), 0, &vPropertyList);VariantClear(&vPropertyList);SafeArrayDestroy(psaProperties);// Pass the context object as the pCtx parameter of GetObject.hr = pServices->GetObject(bstrPath, 0, pctxDrive, &pDrive, 0);if( FAILED(hr) ){ pServices->Release();CoUninitialize();printf("failed property GetObject\n");return;} // Display the objecthr = pDrive->GetObjectText(0, &bstrDriveObj);printf("%S\n\n", bstrDriveObj);SysFreeString(bstrPath);VARIANT vFileSystem;// Attempt to get a property that was not requested.// The following should return a NULL property if// the partial-instance retrieval succeeded.hr = pDrive->Get(_bstr_t(L"FileSystem"), 0,&vFileSystem, NULL, NULL);if( FAILED(hr) ){ pServices->Release();pDrive->Release(); CoUninitialize();printf("failed get for file system\n");return;} if (V_VT(&vFileSystem) == VT_NULL)printf("file system variable is null - expected\n");elseprintf("FileSystem = %S\n", V_BSTR(&vFileSystem));VariantClear(&vFileSystem);pDrive->Release(); pctxDrive->Release();pServices->Release();pServices = NULL; // MUST be set to NULLCoUninitialize();return; // -- program successfully completed
};
原来这里的sql语句的参数皆可以从下面的这些类里面出来
https://msdn.microsoft.com/en-us/library/windows/desktop/aa394239(v=vs.85).aspx
Win32_OperatingSystem例如现在要获取系统的信息,从类的属性BootDevice就可以知道启动硬盘的信息
这篇关于通过WMI技术获取操作系统的信息类Win32_OperatingSystem的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!