无法从 Win32_OperatingSystem 类中获取“描述”
Posted
技术标签:
【中文标题】无法从 Win32_OperatingSystem 类中获取“描述”【英文标题】:Cant get "Description" from Win32_OperatingSystem class 【发布时间】:2021-05-14 21:18:52 【问题描述】:我尝试从 Win32_OperatingSystem 获取描述,这是我采用空字符串的主要问题。 我不明白为什么,当我尝试从 Win32_OperatingSystem 获取其他类型的字符串时,我可以得到它。 会不会有Description为空的情况?或者它只是我的代码中的错误......? 代码:
STDMETHODIMP CSystemInfo::GetOS(CString* SystemInfo )
HRESULT hres;
CString tmp;
hres = GetInfo( TEXT( "Win32_OperatingSystem" ), TEXT( "Description" ), &tmp );
if( FAILED( hres ) )
return E_FAIL;
SystemInfo->SetString( tmp.GetString() );
return S_OK;
STDMETHODIMP CSystemInfo::GetInfo( CString className, CString propertyName, CString* info )
HRESULT hres;
IWbemLocator* pLoc = NULL;
IWbemServices* pSvc = NULL;
IEnumWbemClassObject* pEnumerator = NULL;
bool initialized = true;
hres = CoInitialize( NULL );
if( FAILED( hres ) )
return E_FAIL;
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
);
if( FAILED( hres ) && hres != RPC_E_TOO_LATE )
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, ( LPVOID* )&pLoc );
if( FAILED( hres ) )
return E_FAIL;
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 indicates current
NULL, // Security flags.
0, // Authority (for example, Kerberos)
0, // Context object
&pSvc // pointer to IWbemServices proxy
);
if( FAILED( hres ) )
return E_FAIL;
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 ) )
return E_FAIL;
CString tmp = TEXT( "SELECT * FROM ");
tmp += className.GetString();
hres = pSvc->ExecQuery(
bstr_t( "WQL" ),
bstr_t(tmp.GetString()),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator );
if( FAILED( hres ) )
return E_FAIL;
IWbemClassObject* pclsObj = NULL;
ULONG uReturn = 0;
while( pEnumerator )
HRESULT hr = pEnumerator->Next( WBEM_INFINITE, 1, &pclsObj, &uReturn );
if( uReturn == 0 )
break;
VARIANT vtProp;
hr = pclsObj->Get(propertyName.GetString(), 0, &vtProp, 0, 0 );
if( FAILED( hres ) )
return E_FAIL;
info->SetString( vtProp.bstrVal );
VariantClear( &vtProp );
pclsObj->Release();
CoUninitialize();
return S_OK;
这就是我调用函数的方式
HRESULT hr;
CoInitialize( NULL );
CSystemInfo* cSystem = NULL;
CLSID clsid;
hr = CLSIDFromProgID( L"Server.Inproc.1" , &clsid );
if( FAILED( hr ) )
std::cout << "Cant get CLSID " << std::endl;
hr = CoCreateInstance( clsid,NULL, CLSCTX_INPROC_SERVER, IID_ISystemInfo, ( void** )&cSystem );
if( FAILED( hr ) )
std::cout << "Cant Create Instance" << std::endl;
CString tmp;
hr = cSystem->GetOS( &tmp );
std::wcout << "OS Info: \t" ;
std::wcout << tmp.GetString() << std::endl;
我也无法从 Win32_DesktopMonitor 获得描述。
【问题讨论】:
【参考方案1】:您可以通过在 Windows PowerShell 上执行 gwmi Win32_OperatingSystem
命令来检查“Description
”属性是否存在。我在我的电脑上找不到 Win32_OperatingSystem 的“描述”属性。
【讨论】:
以上是关于无法从 Win32_OperatingSystem 类中获取“描述”的主要内容,如果未能解决你的问题,请参考以下文章
WMI Win32_OperatingSystem OSArchitecture 字段导致异常
如何解决错误:命令失败:powershell (Get-CimInstance -ClassName Win32_OperatingSystem>
Win32_LogicalMemoryConfiguration在win7下无法使用