我试图通过wmi查询获取特定值。我知道这个值存储在一个数组中,但我无法访问它。
ManagementObjectSearcher ^searcher =
gcnew ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_DiskDrive WHERE PNPDeviceID LIKE '" + marshal_as<String^>(DEV_FILTER) +"%'");
for each (ManagementObject ^drive in searcher->Get())
{
for each (ManagementObject ^o in drive->GetRelated("Win32_DiskPartition"))
{
for each (ManagementObject ^logDisk in o->GetRelated("Win32_LogicalDisk"))
{
Console::WriteLine(
"Disk: " + drive["Caption"]
+ "\n\tSize : " + drive["Size"]
+ "\n\tDriveLetter : " + logDisk["Name"]
+ "\n\t" + drive["PNPDeviceID"]
+ "\n\tSerialNumber: " + drive["SerialNumber"]
+ "\n\t" + drive["Capabilities"]
+ "\n\tRevision : " + drive["FirmwareRevision"]);
}
}
}
在调试调查中,我可以看到drive["Capabilites"]
的每一个值,但到目前为止我已尝试过的所有内容,我无法获得此数组的子值。
我想在c ++中这样做, 感谢每一个帮助。
答案 0 :(得分:2)
要访问此类值,必须强制转换属性和数组,然后迭代元素。试试这个C ++ CLI示例代码。
#include "stdafx.h"
#using <system.management.dll>
using namespace System;
using namespace System::Management;
int main(array<System::String ^> ^args)
{
ManagementObjectSearcher ^searcher =
gcnew ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_DiskDrive");
for each (ManagementObject ^drive in searcher->Get())
{
for each (ManagementObject ^o in drive->GetRelated("Win32_DiskPartition"))
{
for each (ManagementObject ^logDisk in o->GetRelated("Win32_LogicalDisk"))
{
Console::WriteLine(
"Disk: " + drive["Caption"]
+ "\n\tSize : " + drive["Size"]
+ "\n\tDriveLetter : " + logDisk["Name"]
+ "\n\t" + drive["PNPDeviceID"]
+ "\n\tSerialNumber: " + drive["SerialNumber"]
+ "\n\tRevision : " + drive["FirmwareRevision"]);
Console::WriteLine("Capabilities");
for each(UInt16 v in (array<UInt16>^)(drive->Properties["Capabilities"]->Value))
{
Console::WriteLine(v);
}
}
}
}
Console::ReadLine();
return 0;
}