变量/过程出错

时间:2016-05-06 17:31:49

标签: delphi delphi-10-seattle

r = /
    \b              # match a word break
    [Bb]            # match "B" or "b"
    udget:          # match string
    \s+\$           # match 1 or more spaces followed by a dollar sign
    \K              # discard all matches so far
    \d{1,3}         # match between 1 and 3 digits
    (?:             # begin a non-capture group
      (?![\,\d])    # match a comma or digit in a negative lookahead
      |             # or
      (?:           # begin a non-capture group
        (?:\,\d{3}) # match a comma followed by 3 digits in a non-capture group
        +           # perform preceding match 1 or more times
      )             # end non-capture group
    )               # end non-capture group
    (?:\.\d\d)      # match a period followed by 2 digits in a non-capture group
    ?               # make the preceding match optional
    /x

"Some text Budget: $25\nsome more text"[r]            #=> "25"
"Some text Budget: $25.42\nsome more text"[r]         #=> "25.24"
"Some text Budget: $25,642,328\nsome more text"[r]    #=> "25,642,328"
"Some text Budget: $25,642,328.01\nsome more text"[r] #=> "25,642,328.01"
"Some text Budget: $25,64,328.01\nsome more text"[r]  #=> nil

所以我有 [dcc32错误] Project2.dpr(29):E2033实际和正式var参数的类型必须相同。我在上面显示的代码的最后一行(我想它与FWbemObject有关)变量。 Nota,我使用的是Delphi 10 Seattle。

3 个答案:

答案 0 :(得分:4)

如果对于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;

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

答案 1 :(得分:3)

这个例程需要对西雅图进行一些更改 - 见下文。

oEnum.Next的第二个Param应该是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.

答案 2 :(得分:2)

您在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.