为什么 MSFT_PhysicalDisk 总是 returns 磁盘大小 = 0?

Why does MSFT_PhysicalDisk always returns disk Size = 0?

我需要使用 WMI MSFT_PhysicalDisk class 获取有关物理磁盘的一些信息。

它有我需要的一切,但返回的磁盘 Size 始终为零...为什么会这样?我是做错了什么还是这种行为很正常?

我使用这个代码:

program WMI_PhysicalDisk;

{$APPTYPE CONSOLE}

uses
  System.SysUtils,
  Winapi.ActiveX,
  System.Win.ComObj,
  System.Variants;

function VarToInt(const AVariant: Variant): INT64;// integer;
begin Result := StrToIntDef(Trim(VarToStr(AVariant)), 0); end;

procedure GetMSFT_PhysicalDiskInfo;
const
  WbemUser =''; WbemPassword =''; WbemComputer ='localhost';
  wbemFlagForwardOnly = [=12=]000020;
var
  FSWbemLocator : OLEVariant;
  FWMIService : OLEVariant;
  FWbemObjectSet: OLEVariant;
  FWbemObject : OLEVariant;
  oEnum : IEnumvariant;
  iValue : LongWord;
begin;
  FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  FWMIService := FSWbemLocator.ConnectServer(WbemComputer, 'root\Microsoft\Windows\Storage', WbemUser, WbemPassword);
  FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM MSFT_PhysicalDisk','WQL',wbemFlagForwardOnly);
  oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
  while oEnum.Next(1, FWbemObject, iValue) = 0 do
  begin
    Writeln(Format('DeviceId %s',[VarToStr(FWbemObject.DeviceId)]));                      // String
    Writeln(Format('LogicalSectorSize %d',[VarToInt(FWbemObject.LogicalSectorSize)]));    // Uint64
    Writeln(Format('MediaType %d',[VarToInt(FWbemObject.MediaType)]));                    // Uint16
    Writeln(Format('Model %s',[VarToStr(FWbemObject.Model)]));                            // String
    Writeln(Format('PhysicalSectorSize %d',[VarToInt(FWbemObject.PhysicalSectorSize)]));  // Uint64
    Writeln(Format('Size %d',[VarToInt(FWbemObject.Size)]));                              // Uint64
    Writeln('-----------------------------------------------------');
    FWbemObject:=Unassigned;
  end;
end;
begin
 try
    CoInitialize(nil);
    try
      GetMSFT_PhysicalDiskInfo;
    finally
      CoUninitialize;
    end;
 except
    on E:EOleException do
        Writeln(Format('EOleException %s %x', [E.Message,E.ErrorCode]));
    on E:Exception do
        Writeln(E.Classname, ':', E.Message);
 end;
end.

然后我得到...:[=​​15=]

DeviceId 1
LogicalSectorSize 512
MediaType 3
Model WDC WD10SPZX-22Z10T0
PhysicalSectorSize 4096
Size 0
-------------------------------------------------------------------
DeviceId 0
LogicalSectorSize 512
MediaType 4
Model KBG40ZNS256G NVMe TOSHIBA 256GB
PhysicalSectorSize 4096
Size 0
-------------------------------------------------------------------
DeviceId 2
LogicalSectorSize 512
MediaType 3
Model ST1000LM024 HN-M101MBB
PhysicalSectorSize 4096
Size 0
-------------------------------------------------------------------

你的 VarToInt 函数有一个小问题。它应该使用 StrToInt64Def.

更正后的函数如下所示:

function VarToInt(const AVariant: Variant): INT64;// integer;
begin Result := StrToInt64Def(Trim(VarToStr(AVariant)), 0); end;