在WMI查询中获取" Associators of "语句结果的问题(无效变量操作!)。

问题描述 投票:2回答:3

我想转换 本例 在Delphi代码中,我的问题是在获取" Associators of " 语句查询的结果。

procedure TUSB.GetInfo;
var
 WMIService : OLEVariant;
 DItems, PItems, LItems, VItems: OLEVariant;
 Drive, Partition, Logical, Volume : OLEVariant;
 Drives, Partitions, Logicals, Volumes : IEnumVARIANT;
 IValue : LongWord;
begin
 WMIService := GetWMIObject('winmgmts:\\localhost\root\cimv2');
 DItems := WMIService.ExecQuery('select DeviceID, Model from Win32_DiskDrive where InterfaceType='+QuotedStr('USB'));

 Drives := IUnKnown(DItems._NewEnum) as IEnumVARIANT;
 Drives.Next(1, Drive, IValue);
 DeviceID := Drive.Properties_.Item('DeviceID', 0);

 PItems := WMIService.ExecQuery('associators of {{Win32_DiskDrive.DeviceID='+QuotedStr(DeviceID)+'}} where AssocClass = Win32_DiskDriveToDiskPartition');

 Partitions := IUnKnown(PItems._NewEnum) as IEnumVARIANT;
 Partitions.Next(1, Partition, IValue);
 **PDeviceID := Partition.Properties_.Item('DeviceID', 0);**

...

end;

在标有2颗星的那一行! 我得到了一个错误:" 无效变量操作 "而在上面的相同代码中,没有任何错误!

问题出在哪里?在" Associators of " 语句或...!

非常感谢...

delphi wmi
3个回答
0
投票

在没有看到所有相关代码和声明的情况下,很难判断。当你写。

Partitions.Next(1, Partition, IValue);  

你应该检查一下你是否真的得到了你想要的东西。Partition. 而且更普遍的是,为了调试,你应该总是打破你的复合语句来测试每个中间步骤。

Partition.Properties_  // is it correct? how many items?
Partition.Properties_.Item('DeviceID', 0) // if not OK try to iterate through all items

0
投票

感谢Francois...

"在没有看到所有相关代码和声明的情况下,很难说。"

但没有更多的相关代码! , 我已经改变了下面的代码 :

procedure TUSBItem.GetInfo;
var
 WMIService : OLEVariant;
 DItems, PItems, LItems, VItems: OLEVariant;
 Drive, Partition, Logical, Volume : OLEVariant;
 Drives, Partitions, Logicals, Volumes : IEnumVARIANT;
 IValue : LongWord;
begin
 WMIService := GetWMIObject('winmgmts:\\localhost\root\cimv2');

 DItems := WMIService.ExecQuery('select DeviceID, Model from Win32_DiskDrive where InterfaceType='+QuotedStr('USB'));
 Drives := IUnKnown(DItems._NewEnum) as IEnumVARIANT;
 while Drives.Next(1, Drive, IValue) = S_OK do
  begin
   DeviceID := Drive.Properties_.Item('DeviceID', 0);
   PItems := WMIService.ExecQuery('associators of {{Win32_DiskDrive.DeviceID='+QuotedStr(DeviceID)+'}} where AssocClass = Win32_DiskDriveToDiskPartition');
   Partitions := IUnKnown(PItems._NewEnum) as IEnumVARIANT;
   if Partitions.Next(1, Partition, IValue) = S_OK then
    begin
     if not VarIsNull(Partition) then
      PDeviceID := Partition.Properties_.Item('DeviceID', 0);
    end;
   ...
  end;
 ...
end;

循环 , 对于每个驱动器在Drives枚举变量 , 我得到一个 "DeviceID" , 我应该传递 "DeviceID" 到 " Associators of " 语句,以获得与驱动器相关联的分区列表 "DeviceID" 作为查询结果... ...

在下一行 , 我把分区中的项目作为一个IEnumVARIANT , 接下来我检查是否把分区的第一个元素在分区中返回 " S_OK " ( 成功 !),然后我检查分区,如果它不是。t null then i get one of it命名为 "DeviceID "的项目,但在这一行,我得到了一个错误的消息:"无效变量操作"

有相同的方法来获取驱动器的DeviceID和分区的DeviceID,但当我想获取分区的DeviceID时,我得到了一个错误,他们之间有一个不同的,它的WMI查询,我想问题是,但我不知道!我想这是我的问题。

这里有一些有用的句子。

" 但是,在编写框架提供者时,你应该记住以下几点。

* Make sure you support standard queries in your association class, especially queries where the reference properties are used in a WHERE clause. For more information, see CFrameworkQuery::GetValuesForProp.
* In your association class support, when you check to see if the endpoints exist, ensure you use the CWbemProviderGlue::GetInstanceKeysByPath or CWbemProviderGlue::GetInstancePropertiesByPath methods.

  These methods allow the endpoints to skip populating expensive or unneeded properties.
* Make sure any association endpoint classes support per-property Get methods. For more information, see Supporting Partial-Instance Operations. For more information about the query parameter, see CFrameworkQuery. **"**

因为这个解释!我认为问题出在Path上! , 意味着我应该把我的目标(这里是Partition)给WMIService对象以获得结果! , 我认为问题出在Path上。, 有所帮助 但我完全不明白!!!

当然,在 资料页 我转换它,没有任何解释的路径...!

非常感谢...

© www.soinside.com 2019 - 2024. All rights reserved.