【问题标题】:Error with variables/procedure变量/过程错误
【发布时间】:2016-05-06 17:31:48
【问题描述】:
procedure  GetWin32_DiskDriveInfo;
const
  WbemUser            ='';
  WbemPassword        ='';
  WbemComputer        ='localhost';
  wbemFlagForwardOnly = $00000020;
var
  FSWbemLocator : OLEVariant;
  FWMIService   : OLEVariant;
  FWbemObjectSet: OLEVariant;
  FWbemObject   : Variant;
  oEnum         : IEnumvariant;
  sValue        : string;
begin  
  FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  FWMIService   := FSWbemLocator.ConnectServer(WbemComputer, 'root\CIMV2', WbemUser, WbemPassword);
  FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM Win32_DiskDrive','WQL',wbemFlagForwardOnly);
  oEnum         := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
  while oEnum.Next(1, FWbemObject, nil) = 0 do

所以我有 [dcc32 Error] Project2.dpr(29): E2033 Types of actual and form var parameters must be same。我在上面显示的代码的最后一行有它(我想它与 FWbemObject 有关)变量。 请注意,我使用的是 Delphi 10 Seattle。

【问题讨论】:

标签: delphi delphi-10-seattle


【解决方案1】:

如果您使用的代码用于 FPC,在 Delphi 下您需要进行一些更改。

IEnumVARIANT.Next 函数是这样定义的

function Next(celt: LongWord; var rgvar : OleVariant; out pceltFetched: LongWord): HResult; stdcall;

因此您需要将FWbemObject 的类型更改为OleVariant 并为pceltFetched 参数添加另一个变量。

像这样

  FWbemObject   : OLEVariant;
  pceltFetched  :  LongWord;
begin
  ...
  ...
  while oEnum.Next(1, FWbemObject, pceltFetched) = 0 do
  ...
  ...

 end;

此外,如果您想在控制台应用程序中使用此代码,请记住调用 CoInitialize 方法。

【讨论】:

    【解决方案2】:

    对于西雅图来说,该例程需要进行一些更改 - 见下文。

    oEnum.Next 的第二个参数应该是 OleVariant,第三个参数应该是 LongWord。另外,你需要调用 CoInitialize/CoUnitialize

    procedure  GetWin32_DiskDriveInfo;
    const
      WbemUser            ='';
      WbemPassword        ='';
      WbemComputer        ='localhost';
      wbemFlagForwardOnly = $00000020;
    var
      FSWbemLocator : OLEVariant;
      FWMIService   : OLEVariant;
      FWbemObjectSet: OLEVariant;
      FWbemObject   : OleVariant;  //   NOT Variant
      oEnum         : IEnumvariant;
      sValue        : string;
      Fetched : LongWord;  // Added, required 3rd Param to oEnum.Next
    begin;
      FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
      FWMIService   := FSWbemLocator.ConnectServer(WbemComputer, 'root\CIMV2', WbemUser, WbemPassword);
      FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM Win32_DiskDrive','WQL',wbemFlagForwardOnly);
      oEnum         := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
      while oEnum.Next(1, FWbemObject, Fetched) = 0 do
      begin
        sValue:= FWbemObject.Properties_.Item('Caption').Value;
        Writeln(Format('Caption        %s',[sValue]));// String
        sValue:= FWbemObject.Properties_.Item('DeviceID').Value;
        Writeln(Format('DeviceID       %s',[sValue]));// String
        sValue:= FWbemObject.Properties_.Item('Model').Value;
        Writeln(Format('Model          %s',[sValue]));// String
        sValue:= FWbemObject.Properties_.Item('Partitions').Value;
        Writeln(Format('Partitions     %s',[sValue]));// Uint32
        sValue:= FWbemObject.Properties_.Item('PNPDeviceID').Value;
        Writeln(Format('PNPDeviceID    %s',[sValue]));// String
        sValue:= FormatFloat('#,', FWbemObject.Properties_.Item('Size').Value / (1024*1024));
        Writeln(Format('Size           %s mb',[sValue]));// Uint64
    
        Writeln;
        FWbemObject:= Unassigned;
      end;
    end;
    
    begin
      CoInitialize(Nil);  // Added
    
      try
        GetWin32_DiskDriveInfo;
      except
        on E:EOleException do
            Writeln(Format('EOleException %s %x', [E.Message,E.ErrorCode]));
        on E:Exception do
            Writeln(E.Classname, ':', E.Message);
      end;
      Writeln('Press Enter to exit');
      Readln;
      CoUnInitialize;  //Added
    end.
    

    【讨论】:

      【解决方案3】:

      您在 RRUZ answer 使用的代码用于 fpc。

      使用这个link,这个代码用于Delphi:

      {$APPTYPE CONSOLE}
      
      uses
        SysUtils,
        ActiveX,
        ComObj,
        Variants;
      
      
      
      // The Win32_DiskDrive class represents a physical disk drive as seen by a computer running the Win32 operating system. Any interface to a Win32 physical disk drive is a descendent (or member) of this class. The features of the disk drive seen through this object correspond to the logical and management characteristics of the drive. In some cases, this may not reflect the actual physical characteristics of the device. Any object based on another logical device would not be a member of this class.
      // Example: IDE Fixed Disk.
      
      procedure  GetWin32_DiskDriveInfo;
      const
        WbemUser            ='';
        WbemPassword        ='';
        WbemComputer        ='localhost';
        wbemFlagForwardOnly = $00000020;
      var
        FSWbemLocator : OLEVariant;
        FWMIService   : OLEVariant;
        FWbemObjectSet: OLEVariant;
        FWbemObject   : OLEVariant;
        oEnum         : IEnumvariant;
        iValue        : LongWord;
      begin;
        FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
        FWMIService   := FSWbemLocator.ConnectServer(WbemComputer, 'root\CIMV2', WbemUser, WbemPassword);
        FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM Win32_DiskDrive','WQL',wbemFlagForwardOnly);
        oEnum         := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
        while oEnum.Next(1, FWbemObject, iValue) = 0 do
        begin
          Writeln(Format('DeviceID    %s',[String(FWbemObject.DeviceID)]));// String
      
          Writeln('');
          FWbemObject:=Unassigned;
        end;
      end;
      
      
      begin
       try
          CoInitialize(nil);
          try
            GetWin32_DiskDriveInfo;
          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;
       Writeln('Press Enter to exit');
       Readln;      
      end.
      

      【讨论】:

        猜你喜欢
        • 2019-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多