gpt4 book ai didi

c - 获取属性的日期值

转载 作者:太空宇宙 更新时间:2023-11-04 03:18:46 26 4
gpt4 key购买 nike

我目前正在编写一个必须执行 WMI 查询的 C 程序。我可以毫无问题地发出查询,但是当它从 VARIANT 变量中检索属性值时,它开始变得非常困难。

我有一个巨大的 switch case,对于每个 case 我只想 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 等。

有没有人知道或知道这样做?

最佳答案

propertyValue.datedouble,它需要"%g" 打印格式,但它可能无效。尝试使用 bstrVal 值代替:

case CIM_DATETIME:
//wprintf(L"%g\n", propertyValue.date);
wprintf(L"%s\n", propertyValue.bstrVal);
break;

请注意,在 Debug模式下,您可以将光标移到 propertyValue 上,它应该会显示包含在 propertyValue

中的值

关于c - 获取属性的日期值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48662258/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com