我目前正在编写一个必须进行WMI查询的C程序。 我可以毫无困难地发出查询,但是当它从VARIANT变量中检索属性的值时,它开始变得非常困难。
我有一个巨大的切换案例,对于每种情况我只想wprintf值(最终目的是检索wchar_t字符串中的值)。
到目前为止,对于整数类型,它并不那么困难,但对于日期时间类型,我根本无法获得某些东西。
以下是我正在处理的例子(为了隐身而没有功能检查,但是我这样做了:
typedef struct cpwmi {
IWbemLocator *locator;
IWbemServices *services;
IEnumWbemClassObject *results;
IWbemClassObject *result;
} cpwmi_s;
void CpWmi_Constructor(cpwmi_s *self)
{
self->locator = NULL;
self->services = NULL;
self->results = NULL;
self->result = NULL;
}
void CpWmi_Destructor(cpwmi_s *self)
{
if (self->result) {
self->result->lpVtbl->Release(self->result);
}
if (self->results) {
self->results->lpVtbl->Release(self->results);
}
if (self->services) {
self->services->lpVtbl->Release(self->services);
}
if (self->locator) {
self->locator->lpVtbl->Release(self->locator);
}
}
void CpWmi_MinimalExample(cpwmi_s *self)
{
// Connection
CoCreateInstance(&CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER, &IID_IWbemLocator, &self->locator);
BSTR ressource = SysAllocString(L"ROOT\\CIMV2");
self->locator->lpVtbl->ConnectServer(self->locator, ressource, NULL, NULL, NULL, 0, NULL, NULL, &self->services);
SysFreeString(ressource);
// Issue WMI query
BSTR query = SysAllocString(L"SELECT ReleaseDate FROM Win32_BIOS");
BSTR language = SysAllocString(L"WQL");
self->services->lpVtbl->ExecQuery(self->services, language, query, WBEM_FLAG_BIDIRECTIONAL, NULL, &self->results);
SysFreeString(query);
SysFreeString(language);
// Go to first result
ULONG count = 0;
self->results->lpVtbl->Next(self->results, WBEM_INFINITE, 1, &self->result, &count);
// Get propperty's value
BSTR propertyName = SysAllocString(L"ReleaseDate");
VARIANT propertyValue;
CIMTYPE propertyType;
self->result->lpVtbl->Get(self->result, propertyName, 0, &propertyValue, &propertyType, 0);
wprintf(L"%s: ", propertyName);
if (propertyValue.vt != VT_NULL && propertyValue.vt != VT_EMPTY) {
switch (propertyType) {
/* Many case here */
case CIM_DATETIME:
wprintf(L"%f", propertyValue.date);
break;
}
}
VariantClear(&propertyValue);
wprintf(L"\n");
}
int main(void)
{
cpwmi_s wmi;
CpWmi_Constructor(&wmi);
// initialize COM
CoInitializeEx(NULL, COINIT_MULTITHREADED);
CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);
CpWmi_MinimalExample(&wmi);
CpWmi_Destructor(&wmi);
// unwind everything else we've allocated
CoUninitialize();
printf("Stop\n");
getchar();
return;
}
我希望有像
这样的东西发布日期:20140401000000.000000 + 000
但我最终还是
发布日期:-92559592126069970772275711289628980735378270670621907468943360.000000
我必须误读msdn文档或者不知道如何搜索/阅读,因为到目前为止,我不知道如何在C中做到这一点! 我也担心CIM_REFERENCE_TYPE,CIM_OBJECT_TYPE等。
是否有人有提示或知道这样做?
答案 0 :(得分:1)
propertyValue.date
为double
,需要"%g"
打印格式,但可能无效。请尝试使用bstrVal
值:
case CIM_DATETIME:
//wprintf(L"%g\n", propertyValue.date);
wprintf(L"%s\n", propertyValue.bstrVal);
break;
注意,在调试模式下,您可以将光标移到propertyValue
上,它应显示propertyValue