c++ - PropertyData 值数组

标签 c++ arrays wmi

我正在尝试通过 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++ 来做这个, 感谢您的帮助。

最佳答案

要访问这些值,您必须转换属性和数组,然后遍历元素。试试这个 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;
}

关于c++ - PropertyData 值数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23084919/

相关文章:

c++ - 在构建期间强制关闭 Visual Studio 后文件无效或损坏

c++ - 错误 C2893 : Failed to specialize function template C++

c - while 循环内出现段错误

c++ - 在 C++ 中定义数组并在结构中使用它

python - 如何在 Windows 10 上使用 Python 和 WMI 检测亮度变化?

c# - 如何以编程方式更改 WMI 的设置而不是使用 wmimgmt.msc snappin?

c++ - Strcmp 未按预期运行,比较两个不相等的字符串时返回 0

c++ - 读取 C 中的二进制 (.hgt) 文件(将代码从 C++ 迁移到 C)

php - 如何从 IPN 访问事务数组值

python - 如何监视远程 PC 上的事件窗口