📄 testwmi.cpp
字号:
hr = SafeArrayAccessData( psaValue,
&pv );
}
if ( hr == S_OK ) {
lpsz = (BSTR)pv;
//
// Change each element into string.
//
for (i=0; (hr == S_OK) && (i < (lUBound-lLBound+1)); ++i) {
VariantInit( &vaElement );
V_VT(&vaElement) = VT_BYREF | vt;
V_BYREF(&vaElement) = (LPVOID)lpsz;
VariantInit( &vaTemp );
hr = VariantChangeType( &vaTemp,
&vaElement,
VARIANT_LOCALBOOL,
VT_BSTR );
if ( hr == S_OK ) {
hr = AddToList( hwndDlg,
&vaTemp );
VariantClear( &vaTemp );
}
else {
PrintError( hr,
__LINE__,
TEXT(__FILE__),
TEXT("Couldn't format the value of %s into ")
TEXT("displayable text. The value cannot be ")
TEXT(" viewed/modified."),
lpszProperty );
}
lpsz = (BSTR)((LONG_PTR)lpsz + uiSize);
}
SafeArrayUnaccessData( psaValue );
}
else {
PrintError( hr,
__LINE__,
TEXT(__FILE__),
TEXT("Couldn't read the values of %s."),
lpszProperty );
}
if ( psaValue ) {
SafeArrayDestroy( psaValue );
}
return hr == S_OK;
}
//
// The function add a property value to the tree list.
//
HRESULT AddToList (HWND hwndDlg,
VARIANT *pvaValue)
{
TV_INSERTSTRUCT tvInsertStruc;
ZeroMemory(
&tvInsertStruc,
sizeof(TV_INSERTSTRUCT) );
tvInsertStruc.hParent = TVI_ROOT;
tvInsertStruc.hInsertAfter = TVI_LAST;
tvInsertStruc.item.mask = TVIF_TEXT | TVIF_PARAM;
tvInsertStruc.item.pszText = BstrToString( V_BSTR(pvaValue),
-1 );
if ( tvInsertStruc.item.pszText ) {
tvInsertStruc.item.cchTextMax = _tcslen( tvInsertStruc.item.pszText ) + 1;
tvInsertStruc.item.lParam = (LPARAM)tvInsertStruc.item.cchTextMax;
TreeView_InsertItem( GetDlgItem(hwndDlg,
IDT_PROPERTY_VALUE),
&tvInsertStruc );
SysFreeString( (BSTR)((PVOID)tvInsertStruc.item.pszText) );
}
else {
PrintError( HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY),
__LINE__,
TEXT(__FILE__),
TEXT("Cannot show the values of the property.") );
return S_FALSE;
}
return S_OK;
}
VOID ModifyArrayProperty(HWND hwndDlg,
LPPROPERTY_INFO pPropInfo)
{
MessageBox( hwndDlg,
TEXT("This feature is currently not implemented."),
TEXT("Modify Array"),
MB_ICONINFORMATION | MB_OK );
return;
}
//
// The function lists the instances of the selected class and properties of
// the first instance.
//
VOID RefreshOnClassSelection (HWND hwndDlg)
{
IWbemServices *pIWbemServices;
HWND hwndClassList;
HWND hwndInstTree;
HWND hwndPropTree;
LPTSTR lpszClass;
LPTSTR lpszInstance;
HTREEITEM hItem;
pIWbemServices = (IWbemServices *)GetWindowLongPtr( hwndDlg,
DWLP_USER );
//
// Find the selected class.
//
//
hwndClassList = GetDlgItem( hwndDlg,
IDL_CLASSES );
hwndInstTree = GetDlgItem( hwndDlg,
IDT_INSTANCES );
hwndPropTree = GetDlgItem( hwndDlg,
IDT_PROPERTIES );
TreeView_DeleteAllItems( hwndInstTree );
TreeView_DeleteAllItems( hwndPropTree );
lpszClass = GetSelectedClass( hwndClassList );
if ( lpszClass ) {
//
// List all the instances of the selected class.
//
EnumInstances( pIWbemServices,
lpszClass,
hwndInstTree ); // Tree to populate.
//
// By default, first instance is selected and its properties
// are shown.
//
hItem = TreeView_GetChild( hwndInstTree,
TVI_ROOT );
//
// hItem == NULL ==> No instances found.
//
if ( hItem ) {
//
// Select the first instance.
//
TreeView_SelectItem( hwndInstTree,
hItem );
//
// Find the selected instance.
//
lpszInstance = GetSelectedItem( hwndInstTree );
if ( lpszInstance ) {
//
// Show properties of the selected instance.
//
EnumProperties( pIWbemServices,
lpszClass,
lpszInstance,
hwndPropTree ); // Tree to populate.
SysFreeString( (BSTR)((PVOID)lpszInstance) );
}
else {
PrintError( HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY),
__LINE__,
TEXT(__FILE__),
TEXT("Properties of the selected ")
TEXT("instance will not be listed.") );
}
}
SysFreeString( (BSTR)((PVOID)lpszClass) );
}
else {
PrintError( HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY),
__LINE__,
TEXT(__FILE__),
TEXT("Instances of the selected ")
TEXT("class will not be listed.") );
}
return;
}
//
// Given a handle to a combo box, the function returns the name of the
// selected item i.e. class.
//
LPTSTR GetSelectedClass (HWND hwndClassList)
{
LPTSTR lpszClass;
ULONG ulIndex;
ULONG ulLen;
lpszClass = NULL;
//
// Find the selected class.
//
ulIndex = (ULONG)SendMessage( hwndClassList,
CB_GETCURSEL,
0,
0 );
//
// Find the length of the selected class name.
//
ulLen = (ULONG)SendMessage( hwndClassList,
CB_GETLBTEXTLEN,
(WPARAM)ulIndex,
0 );
lpszClass = (LPTSTR)SysAllocStringLen( NULL,
ulLen + 1 );
if ( lpszClass ) {
SendMessage( hwndClassList,
CB_GETLBTEXT,
(WPARAM)ulIndex,
(LPARAM)lpszClass );
}
return lpszClass;
}
//
// Given a handle to the tree list, the function returns the name of the
// selected item.
//
LPTSTR GetSelectedItem (HWND hwndTree)
{
LPTSTR lpszItem;
HTREEITEM hItem;
TVITEM tvItem;
lpszItem = NULL;
//
// Find the selected item.
//
hItem = TreeView_GetSelection( hwndTree );
if ( hItem ) {
//
// Find out the length of the selected item and allocate memory.
//
ZeroMemory( &tvItem,
sizeof(TVITEM) );
tvItem.hItem = hItem;
tvItem.mask = TVIF_PARAM;
TreeView_GetItem( hwndTree,
&tvItem );
lpszItem = (LPTSTR)SysAllocStringLen( NULL,
(UINT)tvItem.lParam );
if ( lpszItem ) {
tvItem.hItem = hItem;
tvItem.mask = TVIF_TEXT;
tvItem.pszText = lpszItem;
tvItem.cchTextMax = (INT)tvItem.lParam;
TreeView_GetItem( hwndTree,
&tvItem );
}
}
return lpszItem;
}
//
// The function inserts an item into a tree list.
//
VOID InsertItem (HWND hwndTree,
LPTSTR lpszItem)
{
TVINSERTSTRUCT tvInsertStruc;
ZeroMemory(
&tvInsertStruc,
sizeof(TVINSERTSTRUCT) );
tvInsertStruc.hParent = TVI_ROOT;
tvInsertStruc.hInsertAfter = TVI_LAST;
tvInsertStruc.item.mask = TVIF_TEXT | TVIF_PARAM;
tvInsertStruc.item.pszText = lpszItem;
tvInsertStruc.item.cchTextMax = _tcslen(lpszItem) + 1;
tvInsertStruc.item.lParam = tvInsertStruc.item.cchTextMax;
TreeView_InsertItem( hwndTree,
&tvInsertStruc );
return;
}
VOID PrintError (HRESULT hr,
UINT uiLine,
LPTSTR lpszFile,
LPCTSTR lpFmt,
...)
{
LPTSTR lpSysMsg;
TCHAR buf[400];
ULONG offset;
va_list vArgList;
if ( hr != 0 ) {
_stprintf( buf,
TEXT("Error %#lx (%s, %d): "),
hr,
lpszFile,
uiLine );
}
else {
_stprintf( buf,
TEXT("(%s, %d): "),
lpszFile,
uiLine );
}
offset = _tcslen( buf );
va_start( vArgList,
lpFmt );
_vstprintf( buf+offset,
lpFmt,
vArgList );
va_end( vArgList );
if ( hr != 0 ) {
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
hr,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpSysMsg,
0,
NULL );
if ( lpSysMsg ) {
offset = _tcslen( buf );
_stprintf( buf+offset,
TEXT("\n\nPossible cause:\n\n") );
offset = _tcslen( buf );
_tcscat( buf+offset,
lpSysMsg );
LocalFree( (HLOCAL)lpSysMsg );
}
MessageBox( NULL,
buf,
TEXT("TestWMI"),
MB_ICONERROR | MB_OK );
}
else {
MessageBox( NULL,
buf,
TEXT("TestWMI"),
MB_ICONINFORMATION | MB_OK );
}
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -