Question

I'm trying to get a specific value over an wmi query. I know that this value is stored in an array itself but I can't get access on it.

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"]);
        }
    }
}

on Debug investigation, I can see every single Value of drive["Capabilites"] but everything I've tried so far, I can't get this array's child values.

I want to do this in c++, every help is appreciated.

Was it helpful?

Solution

To access such values you must cast the property and an array and then iterate over the elements. try this C++ CLI sample code.

#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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top