* * *

Author Topic: how to get result from wmic directly in my lazarus program  (Read 310 times)

samoraj

  • New member
  • *
  • Posts: 22
how to get result from wmic directly in my lazarus program
« on: September 13, 2018, 02:43:34 am »
Hi, i call in cmd wmic diskdrive get Size >opis\hdd.txt and with timer in lazarus i get result
and change statictext1.caption. Can i get the result directly without to create text file, directly in my program ?
If i have more of one hdd, how i get second line and change another statictext.capture ?

paweld

  • Full Member
  • ***
  • Posts: 169
Re: how to get result from wmic directly in my lazarus program
« Reply #1 on: September 13, 2018, 08:48:51 am »
use TProcess: http://wiki.freepascal.org/Executing_External_Programs#TProcess
eg
Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   Process1: TProcess;
  4. begin
  5.   Process1:=TProcess.Create(Form1);
  6.   Process1.Options := [poWaitOnExit, poUsePipes, poStderrToOutPut];
  7.   Process1.Executable:='cmd.exe';
  8.   Process1.Parameters.Add('/c');
  9.   Process1.Parameters.Add('wmic diskdrive get Size');
  10.   Process1.Active:=True;
  11.   Memo1.Lines.LoadFromStream(Process1.Output);
  12. end;    
Best regards
paweld

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 6498
Re: how to get result from wmic directly in my lazarus program
« Reply #2 on: September 13, 2018, 12:12:29 pm »
Why not get it the FPC way?
Code: Pascal  [Select]
  1. {$mode delphi}
  2. uses variants,classes,comserv,comobj,sysutils,activex;
  3.  
  4. function DriveTypeStr(DriveType:integer): string;
  5.   begin
  6.     case DriveType of
  7.       0 : Result:='Unknown';
  8.       1 : Result:='No Root Directory';
  9.       2 : Result:='Removable Disk';
  10.       3 : Result:='Local Disk';
  11.       4 : Result:='Network Drive';
  12.       5 : Result:='CD/DVD Disc';
  13.       6 : Result:='RAM Disk';
  14.     end;
  15.   end;
  16.  
  17. const
  18.   wbemFlagForwardOnly = $00000020;
  19. var
  20.   FSWbemLocator : OLEVariant;
  21.   FWMIService   : OLEVariant;
  22.   FWbemObjectSet: OLEVariant;
  23.   FWbemObject   : OLEVariant;
  24.   oEnum         : IEnumvariant;
  25.   iValue        : LongWord;
  26.   v  : olevariant;
  27. begin;
  28.   FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  29.   FWMIService   := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', '');
  30.   FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM  Win32_LogicalDisk','WQL',wbemFlagForwardOnly);
  31.   oEnum         := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
  32.   while oEnum.Next(1, FWbemObject, iValue) = 0 do
  33.   begin
  34.     Writeln(Format('Device ID    %s',[String(FWbemObject.DeviceID)]));
  35.     Writeln(Format('DriveType    %s',[DriveTypeStr(FWbemObject.DriveType)]));
  36.     v:=FWbemObject.Size;
  37.     if not varisnull(v) then // can be null for removable drives.
  38.       Writeln(Format('Size     %d',[ int64(FWbemObject.Size)]));
  39.    
  40.     Writeln('');
  41.     FWbemObject:=Unassigned;
  42.   end;
  43. end.
« Last Edit: September 13, 2018, 12:31:40 pm by marcov »

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 730
Re: how to get result from wmic directly in my lazarus program
« Reply #3 on: September 13, 2018, 01:12:57 pm »
hello,
Hello,
i have made a function (with Molly) to get System Infos with WMI request (wmi only in Windows) :
Code: Pascal  [Select]
  1. function  GetWMIInfo(const WMIClass: string; const WMIPropertyNames: Array of String;
  2.                      const Condition: string = ''): TFPObjectList;  
The function is in the utilwmi unit  (last version 0.3 here)
example  of use :
Code: Pascal  [Select]
  1. implementation
  2. uses contnrs,utilwmi;
  3. {$R *.lfm}
  4.  
  5. { TForm1 }
  6.  
  7. procedure TForm1.Button1Click(Sender: TObject);
  8.     var
  9.       WMIResult         : TFPObjectList;
  10.       i                 : Integer;
  11.       PropNamesIndex    : Integer;
  12.       PropNames         : Array[0..2] of String = ( 'Caption','Name','Size' );
  13.     begin
  14.       WMIResult := GetWMIInfo('Win32_DiskDrive', PropNames);
  15.       for i := 0 to Pred(WMIResult.Count) do
  16.       begin
  17.         Memo1.Append('================================================');
  18.         for PropNamesIndex := Low(PropNames) to High(PropNames) do
  19.         begin
  20.           Memo1.Append(TStringList(WMIResult[i]).Names[PropNamesIndex] + ' : ' +
  21.                   TStringList(WMIResult[i]).ValueFromIndex[PropNamesIndex]);
  22.         end;
  23.       end;
  24.       // Clean up
  25.       WMIResult.Free;
  26.     end;        
  27.      

Résult  in attachment.

Friendly, JP

« Last Edit: September 13, 2018, 01:15:04 pm by Jurassic Pork »

samoraj

  • New member
  • *
  • Posts: 22
Re: how to get result from wmic directly in my lazarus program
« Reply #4 on: September 14, 2018, 08:04:32 pm »
Thanks all !!! :)

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus