📄 testwmi.cpp
字号:
switch( LOWORD(wParam) ) {
case IDB_MODIFY:
if ( HIWORD(wParam) == BN_CLICKED ) {
//
// User wants to update the instance after modifying the
// property value.
//
pPropInfo = (LPPROPERTY_INFO)GetWindowLongPtr( hwndDlg,
DWLP_USER );
ModifyArrayProperty( hwndDlg,
pPropInfo );
EndDialog( hwndDlg, 0 );
}
break;
case IDB_CANCEL:
if ( HIWORD(wParam) == BN_CLICKED ) {
EndDialog( hwndDlg, 0 );
}
break;
}
break;
case WM_SYSCOMMAND:
if ( (0xFFF0 & wParam) == SC_CLOSE ) {
EndDialog( hwndDlg, 0 );
}
}
return FALSE;
}
//
// The function populates the combo box of the main window with the classes
// defined in the lpszClasses array, selects the first class of the combo box,
// shows its instances, and properties of the first instance.
//
VOID ListDefaults (HWND hwndDlg)
{
HWND hwndClassList;
UINT i;
hwndClassList = GetDlgItem( hwndDlg,
IDL_CLASSES );
//
// Add the default classes to the combo box.
//
for (i=0; i < sizeof(lpszClasses)/sizeof(LPTSTR); ++i) {
SendMessage( hwndClassList,
CB_ADDSTRING,
0,
(LPARAM)lpszClasses[i] );
}
//
// By default, select the first one in the list which maybe different from
// the first element in the lpszClasses array since the list is sorted.
//
SendMessage( hwndClassList,
CB_SETCURSEL,
0,
0 );
//
// Show the instances and properties of the first instance.
//
RefreshOnClassSelection( hwndDlg );
return;
}
//
// The function lists all the properties of the class instance selected by the
// user.
//
VOID ShowProperties (HWND hwndDlg,
HWND hwndInstTree)
{
IWbemServices *pIWbemServices;
LPTSTR lpszInstance;
LPTSTR lpszClass;
lpszClass = GetSelectedClass( GetDlgItem(hwndDlg,
IDL_CLASSES) );
lpszInstance = GetSelectedItem( hwndInstTree );
if ( lpszInstance && lpszClass ) {
pIWbemServices = (IWbemServices *)GetWindowLongPtr(
hwndDlg,
DWLP_USER );
//
// Show properties of the selected instance.
//
TreeView_DeleteAllItems( GetDlgItem(hwndDlg,
IDT_PROPERTIES) );
EnumProperties( pIWbemServices,
lpszClass,
lpszInstance,
GetDlgItem(hwndDlg,
IDT_PROPERTIES) );
}
else {
PrintError( HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY),
__LINE__,
TEXT(__FILE__),
TEXT("Properties of the selected ")
TEXT("instance will not be listed.") );
}
if ( lpszClass ) {
SysFreeString( (BSTR)((PVOID)lpszClass) );
}
if ( lpszInstance ) {
SysFreeString( (BSTR)((PVOID)lpszInstance) );
}
return;
}
//
// The function shows a dialog box displaying the value of the selected property
// and allows the user to modify it.
//
VOID EditProperty (HWND hwndDlg,
HWND hwndPropTree)
{
PROPERTY_INFO propertyInfo;
LPTSTR lpszInstance;
LPTSTR lpszClass;
VARIANT vaValue;
//
// Get the selected class name.
//
lpszClass = GetSelectedClass( GetDlgItem(hwndDlg,
IDL_CLASSES) );
//
// Get the selected instance name which is __RELPATH value.
//
lpszInstance = GetSelectedItem( GetDlgItem(hwndDlg,
IDT_INSTANCES) );
//
// Get the selected property name.
//
propertyInfo.lpszProperty = GetSelectedItem( hwndPropTree );
if ( lpszInstance && lpszClass && propertyInfo.lpszProperty ) {
propertyInfo.pIWbemServices = (IWbemServices *)GetWindowLongPtr(
hwndDlg,
DWLP_USER );
propertyInfo.pInstance = GetInstanceReference( propertyInfo.pIWbemServices,
lpszClass,
lpszInstance );
if ( propertyInfo.pInstance ) {
if ( GetPropertyValue( propertyInfo.pInstance,
propertyInfo.lpszProperty,
&vaValue,
&propertyInfo.lpszType) ) {
propertyInfo.pvaValue = &vaValue;
if ( V_ISARRAY(&vaValue) ) {
DialogBoxParam( hInstance,
MAKEINTRESOURCE(IDD_ARRAY_PROPERTY),
hwndDlg,
DlgProcArray,
(LPARAM)&propertyInfo );
}
else {
DialogBoxParam( hInstance,
MAKEINTRESOURCE(IDD_SCALAR_PROPERTY),
hwndDlg,
DlgProcScalar,
(LPARAM)&propertyInfo );
}
VariantClear( &vaValue );
SysFreeString( (BSTR)((PVOID)propertyInfo.lpszType) );
}
else {
PrintError( HRESULT_FROM_WIN32(ERROR_WMI_TRY_AGAIN),
__LINE__,
TEXT(__FILE__),
TEXT("Couldn't read %s."),
propertyInfo.lpszProperty );
}
propertyInfo.pInstance->Release();
}
else {
PrintError( HRESULT_FROM_WIN32(ERROR_WMI_INSTANCE_NOT_FOUND),
__LINE__,
TEXT(__FILE__),
TEXT("Couldn't get a pointer to %s."),
lpszInstance );
}
}
else {
PrintError( HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY),
__LINE__,
TEXT(__FILE__),
TEXT("Properties of the selected ")
TEXT("instance will not be listed.") );
}
if ( lpszClass ) {
SysFreeString( (BSTR)((PVOID)lpszClass) );
}
if ( lpszInstance ) {
SysFreeString( (BSTR)((PVOID)lpszInstance) );
}
if ( propertyInfo.lpszProperty ) {
SysFreeString( (BSTR)((PVOID)propertyInfo.lpszProperty) );
}
return;
}
//
// The function updates the property that is modified a the user.
//
BOOL ModifyProperty (HWND hwndDlg)
{
LPPROPERTY_INFO pPropInfo;
HWND hwndValue;
VARIANT vaTemp;
VARIANT vaNewValue;
LPTSTR lpszValue;
ULONG ulLen;
HRESULT hr;
hr = S_FALSE;
pPropInfo = (LPPROPERTY_INFO)GetWindowLongPtr( hwndDlg,
DWLP_USER );
//
// Allocate memory and get new value of the property.
//
hwndValue = GetDlgItem( hwndDlg,
IDE_PROPERTY_VALUE );
ulLen = (ULONG)SendMessage( hwndValue,
WM_GETTEXTLENGTH,
0,
0 );
if ( ulLen > 0 ) {
lpszValue = (LPTSTR)SysAllocStringLen( NULL,
ulLen+1 );
if ( lpszValue ) {
SendMessage( hwndValue,
WM_GETTEXT,
ulLen+1,
(LPARAM)lpszValue );
VariantInit( &vaTemp );
//
// Change the new value from string to its original type.
//
V_VT(&vaTemp) = VT_BSTR;
V_BSTR(&vaTemp) = StringToBstr( lpszValue,
-1 );
if ( V_BSTR(&vaTemp) == NULL ) {
PrintError( HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY),
__LINE__,
TEXT(__FILE__),
TEXT("Couldn't modify the value of %s."),
pPropInfo->lpszProperty );
}
else {
VariantInit( &vaNewValue );
hr = VariantChangeType( &vaNewValue,
&vaTemp,
VARIANT_LOCALBOOL,
V_VT(pPropInfo->pvaValue) );
if ( hr == S_OK ) {
//
// Update the property and its instance.
//
hr = UpdatePropertyValue( pPropInfo->pIWbemServices,
pPropInfo->pInstance,
pPropInfo->lpszProperty,
&vaNewValue );
if ( hr == WBEM_S_NO_ERROR ) {
PrintError( 0,
__LINE__,
TEXT(__FILE__),
TEXT("%s is successfully updated with value %s."),
pPropInfo->lpszProperty,
lpszValue );
}
VariantClear( &vaNewValue );
}
else {
PrintError( hr,
__LINE__,
TEXT(__FILE__),
TEXT("Couldn't convert the specified value '%s' of ")
TEXT("property %s into %s type."),
lpszValue,
pPropInfo->lpszProperty,
pPropInfo->lpszType );
}
SysFreeString( V_BSTR(&vaTemp) );
}
SysFreeString( (BSTR)((PVOID)lpszValue) );
}
else {
PrintError( HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY),
__LINE__,
TEXT(__FILE__),
TEXT("Couldn't modify the value of %s."),
pPropInfo->lpszProperty );
}
}
else {
PrintError( HRESULT_FROM_WIN32(ERROR_WMI_TRY_AGAIN),
__LINE__,
TEXT(__FILE__),
TEXT("You must specify a value to modify %s."),
pPropInfo->lpszProperty );
}
return hr == WBEM_S_NO_ERROR;
}
//
// The function populates a tree list with the values of a property of array
// type. The property could be an array of string or integer.
//
BOOL DisplayArrayProperty (LPTSTR lpszProperty,
VARIANT *pvaValue,
HWND hwndDlg)
{
SAFEARRAY *psaValue;
VARIANT vaTemp;
VARIANT vaElement;
VARTYPE vt;
long lLBound;
long lUBound;
long i;
UINT uiSize;
BSTR lpsz;
LPVOID pv;
HRESULT hr;
//
// Make a copy of the property value.
//
psaValue = NULL;
hr = SafeArrayCopy( V_ARRAY(pvaValue),
&psaValue );
if ( hr == S_OK ) {
hr = SafeArrayGetVartype( psaValue,
&vt );
}
if ( hr == S_OK ) {
hr = SafeArrayGetLBound( psaValue,
1,
&lLBound );
}
if ( hr == S_OK ) {
hr = SafeArrayGetUBound( psaValue,
1,
&lUBound );
}
if ( hr == S_OK ) {
uiSize = SafeArrayGetElemsize( psaValue );
}
if ( hr == S_OK ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -