2010-09-05 81 views
2

我想轉換this example德爾福代碼,我的問題是獲得的聲明中查詢「的社員」的結果:問題在得到聲明「社員」結果在WMI查詢

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星的行中!我得到一個錯誤:「無效的變式操作」而在上面的相同的代碼中,沒有任何錯誤!

問題是什麼? ,在「協會的」聲明或...?!

非常感謝......

回答

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

...

「很難說沒有看到所有的 相關的代碼和聲明「。

但是沒有更多相關的代碼! ,我已經改變了下面的代碼:

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; 

雖然循環,在驅動器枚舉變型,各驅動器,我得到一個「的DeviceID」,我應該通過「的DeviceID」陳述「的社員」到作爲查詢的結果獲得與驅動器相關聯的分區列表「DeviceID」...

在下一行中,我把PItems分區作爲IEnumVARIANT,接下來我檢查是否將分區的第一個元素放入分區「S_OK」(成功!)然後我檢查分區,如果它不是名爲「DeviceID」的項目,但在這一行中,我收到一條錯誤消息:「無效的Va riant操作「

有相同的方式來獲取驅動器的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. **"** 

,因爲這可以解釋,我覺得現在的問題是路徑!,意思是我應該給我的目標(這裏是分區)到WMIService對象來得到結果!, there is some help它,但我完全困惑!

Source page

當然,我將其轉換,isn`t任何關於路徑解釋...!

非常感謝...