📄 wmicode.cpp
字号:
#include "testwmi.h"
//
// The function connects to the namespace.
//
IWbemServices *ConnectToNamespace (VOID)
{
IWbemServices *pIWbemServices = NULL;
IWbemLocator *pIWbemLocator = NULL;
HRESULT hr;
//
// Create an instance of WbemLocator interface.
//
hr = CoCreateInstance( CLSID_WbemLocator,
NULL,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator,
(LPVOID *)&pIWbemLocator );
if ( hr == S_OK ) {
//
// Using the locator, connect to COM in the given namespace.
//
hr = pIWbemLocator->ConnectServer( (BSTR)((PVOID)DEFAULT_NAMESPACE),
NULL, // current account.
NULL, // current password.
0L, // locale
0L, // securityFlags
NULL, // domain for NTLM
NULL, // context
&pIWbemServices );
if ( hr == WBEM_S_NO_ERROR) {
//
// Switch the security level to IMPERSONATE so that provider(s)
// will grant access to system-level objects, and so that
// CALL authorization will be used.
//
hr = CoSetProxyBlanket( (IUnknown *)pIWbemServices, // proxy
RPC_C_AUTHN_WINNT, // authentication service
RPC_C_AUTHZ_NONE, // authorization service
NULL, // server principle name
RPC_C_AUTHN_LEVEL_CALL, // authentication level
RPC_C_IMP_LEVEL_IMPERSONATE, // impersonation
NULL, // identity of the client
EOAC_NONE ); // capability flags
if ( hr != S_OK ) {
pIWbemServices->Release();
pIWbemServices = NULL;
PrintError( hr,
__LINE__,
TEXT(__FILE__),
TEXT("Couldn't impersonate, program exiting...") );
}
}
else {
PrintError( hr,
__LINE__,
TEXT(__FILE__),
TEXT("Couldn't connect to root\\wmi, program exiting...") );
}
//
// Done with IWbemLocator.
//
pIWbemLocator->Release();
}
else {
PrintError( hr,
__LINE__,
TEXT(__FILE__),
TEXT("Couldn't create an instance of ")
TEXT("IWbemLocator interface, programm exiting...") );
}
return pIWbemServices;
}
//
// Given a class name, the function populates the combo box with all
// the instances of the class.
//
VOID EnumInstances (IWbemServices *pIWbemServices,
LPTSTR lpszClass,
HWND hwndInstTree)
{
IEnumWbemClassObject *pEnumInst;
IWbemClassObject *pInst;
VARIANT varInstanceName;
BSTR bstrClass;
LPTSTR lpszInstance;
ULONG ulFound;
HRESULT hr;
bstrClass = StringToBstr( lpszClass,
-1 );
if ( !bstrClass ) {
PrintError( HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY),
__LINE__,
TEXT(__FILE__),
TEXT("Not enough memory to enumerate instances of %s"),
lpszClass );
return;
}
hr = pIWbemServices->CreateInstanceEnum(
bstrClass, // Name of the root class.
WBEM_FLAG_SHALLOW | // Enumerate at current root only.
WBEM_FLAG_FORWARD_ONLY, // Forward-only enumeration.
NULL, // Context.
&pEnumInst ); // pointer to class enumerator
if ( hr == WBEM_S_NO_ERROR ) {
//
// Begin enumerating instances.
//
ulFound = 0;
hr = pEnumInst->Next( 2000, // two seconds timeout
1, // return just one instance.
&pInst, // pointer to instance.
&ulFound); // Number of instances returned.
while ( (hr == WBEM_S_NO_ERROR) && (ulFound == 1) ) {
VariantInit( &varInstanceName );
//
// Get the instance name stored in __RELPATH property.
//
hr = pInst->Get( L"__RELPATH", // property name
0L, // Reserved, must be zero.
&varInstanceName, // property value returned.
NULL, // CIM type not needed.
NULL ); // Flavor not needed.
if ( hr == WBEM_S_NO_ERROR ) {
lpszInstance = BstrToString( V_BSTR(&varInstanceName),
-1 );
if ( lpszInstance ) {
InsertItem( hwndInstTree,
lpszInstance );
SysFreeString( (BSTR)((PVOID)lpszInstance) );
}
else {
hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
PrintError( hr,
__LINE__,
TEXT(__FILE__),
TEXT("Out of memory while enumerating instaces of")
TEXT(" %s, no more instances will")
TEXT(" be listed."),
lpszClass );
}
VariantClear( &varInstanceName );
}
else {
PrintError( hr,
__LINE__,
TEXT(__FILE__),
TEXT("Couldn't retrieve __RELPATH of an instance")
TEXT(" of %s, no more instances will be listed."),
lpszClass );
}
//
// Done with this instance.
//
pInst->Release();
if ( hr == WBEM_S_NO_ERROR ) {
hr = pEnumInst->Next( 2000, // two seconds timeout.
1, // return just one class.
&pInst, // pointer to returned class.
&ulFound); // Number of classes returned.
}
}
pEnumInst->Release();
}
else {
PrintError( hr,
__LINE__,
TEXT(__FILE__),
TEXT("Couldn't create an instance of ")
TEXT("IEnumWbemClassObject interface, instances of %s ")
TEXT("will not be listed."),
lpszClass );
}
SysFreeString( bstrClass );
return;
}
//
// Given a class name and __RELPATH of an instance, the function lists all the
// local non-system properties in a tree list.
//
VOID EnumProperties (IWbemServices *pIWbemServices,
LPTSTR lpszClass,
LPTSTR lpszInstance,
HWND hwndPropTree)
{
IWbemClassObject *pInst;
SAFEARRAY *psaPropNames;
BSTR bstrProperty;
long lLower;
long lUpper;
long i;
HRESULT hr;
LPTSTR lpszProperty;
//
// Get a pointer to the instance.
//
pInst = GetInstanceReference( pIWbemServices,
lpszClass,
lpszInstance );
if ( pInst ) {
//
// psaPropNames must be null prior to making the call.
//
psaPropNames = NULL;
//
// Get all the properties.
//
hr = pInst->GetNames( NULL, // No qualifier names.
WBEM_FLAG_ALWAYS | // All non-system properties
WBEM_FLAG_LOCAL_ONLY,
NULL, // No qualifier values.
&psaPropNames); // Returned property names
if ( hr == WBEM_S_NO_ERROR ) {
//
// Get the number of properties returned.
//
SafeArrayGetLBound( psaPropNames, 1, &lLower );
SafeArrayGetUBound( psaPropNames, 1, &lUpper );
//
// List all properties or stop when encountered an error.
//
for (i=lLower; (hr == WBEM_S_NO_ERROR) && (i <= lUpper); i++) {
//
// Add the property name into the list box.
//
bstrProperty = NULL;
hr = SafeArrayGetElement( psaPropNames,
&i,
&bstrProperty);
if ( SUCCEEDED(hr) ) {
lpszProperty = BstrToString( bstrProperty,
-1 );
if ( lpszProperty ) {
InsertItem( hwndPropTree,
lpszProperty );
SysFreeString( (BSTR)((PVOID)lpszProperty) );
}
else {
PrintError( HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY),
__LINE__,
TEXT(__FILE__),
TEXT("Out of memory while enumerating")
TEXT(" properties of %s, no more properties")
TEXT(" will be listed"),
lpszInstance );
}
//
// Done with the property name.
//
SysFreeString( bstrProperty );
}
else {
PrintError( hr,
__LINE__,
TEXT(__FILE__),
TEXT("Couldn't get the name of a property(%d). ")
TEXT("No more properties will be listed."),
i );
}
}
//
// Done with the array of properties.
//
SafeArrayDestroy( psaPropNames );
}
else {
PrintError( hr,
__LINE__,
TEXT(__FILE__),
TEXT("Couldn't retrieve the properties of %s, ")
TEXT("an instance of class %s. Properties will not be ")
TEXT("listed."),
lpszInstance, lpszClass );
}
}
else {
PrintError( HRESULT_FROM_WIN32(ERROR_WMI_INSTANCE_NOT_FOUND),
__LINE__,
TEXT(__FILE__),
TEXT("Couldn't retrieve a pointer to instance %s of class %s.")
TEXT("Its properties will not be listed."),
lpszInstance, lpszClass );
}
return;
}
//
// Given a class name and __RELPATH of an instance, the function returns a
// pointer to the instance.
//
IWbemClassObject *GetInstanceReference (IWbemServices *pIWbemServices,
LPTSTR lpszClass,
LPTSTR lpszInstance)
{
IWbemClassObject *pInst;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -