2016-05-06 1 views
1
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 

donc je vais avoir [Erreur dcc32] Project2.dpr (29): E2033 Types de paramètres var et formels doivent être identiques. Je l'ai sur la dernière ligne du code montré ci-dessus (je suppose que cela a quelque chose à voir avec FWbemObject) variable. Nota que j'utilise Delphi 10 Seattle.erreur avec des variables/procédure

+2

Comprenez-vous ce que l'erreur signifie? –

+0

Je lis http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/FR/html/devcommon/cm_var_type_conflict_xml.html et je suis sûr que je comprends mais je n'arrive pas à le réparer . –

+0

Source provient de http://stackoverflow.com/questions/8519658/how-to-call-a-list-of-the-physically-attached-hard-disks-using-free-pascal-ou ce qui me fait même Je me demande pourquoi cela ne fonctionne pas pour moi si cela fonctionne pour l'OP. –

Répondre

2

Le code que vous avez utilisé de RRUZ answer est pour fpc.

Utilisez cette link, et ce code pour 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. 
3

Cette routine nécessite quelques changements pour Seattle - voir ci-dessous.

Le second paramètre de oEnum.Next doit être un OleVariant, et le troisième, un LongWord. , Vous devez également appeler 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. 
4

Le code que vous utilisez si pour CPF, sous Delphi, vous devez faire quelques changements.

Cette façon dont la fonction est définie IEnumVARIANT.Next

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

Vous avez donc besoin de changer le type de FWbemObject à OleVariant et ajouter une autre variable pour le pceltFetched param.

Comme si

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

end; 

Aussi, si vous voulez utiliser ce code à partir d'une console App souvenez-vous appel à la méthode CoInitialize.