Recent

Author Topic: Does FreePascal have a WMI Query library?  (Read 27952 times)

vfclists

  • Hero Member
  • *****
  • Posts: 1013
    • HowTos Considered Harmful?
Does FreePascal have a WMI Query library?
« on: May 06, 2014, 05:27:24 pm »
Does FreePascal have a WMI Query , Windows Management instrumentation library?
Lazarus 3.0/FPC 3.2.2

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Does FreePascal have a WMI Query library?
« Reply #1 on: May 06, 2014, 05:41:07 pm »
As far as I know there isn't any wmi specific lirary and I don't think it is needed. Use this http://code.google.com/p/wmi-delphi-code-creator/ download binaries from http://theroadtodelphi.wordpress.com/wmi-delphi-code-creator/
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Does FreePascal have a WMI Query library?
« Reply #2 on: July 29, 2015, 12:37:49 am »
Hello,
i have made a function to get System Infos with WMI request (wmi only in Windows)  :
Code: [Select]
Function GetWMIInfo(const WMIClass: string ; const WMIProperty:tstringList;
                     const Condition: string = ''):tlist;

WMIClass : Class in which to find infos   example : Win32_NetworkAdapter
WMIProperty : a list of properties to get   example : Name,MACAddress
Condition : a Condition to limit results example : WHERE NETENABLED = TRUE

The result is a list of Properties list.

Usage example to get all the active network cards and their MACAddresses :
Code: [Select]
procedure TForm1.Button3Click(Sender: TObject);
var ResultatWMI : TList;
    Infos : TStringList;
  i,j       : Integer;
begin
  Memo1.Clear;
  Infos := TstringList.Create;
  // Prepare Infos to get  in a Name-Value StringList
  Infos.CommaText:='Name=,MACAddress=';
  ResultatWMI := GetWMIInfo('Win32_NetworkAdapter',Infos,'WHERE NETENABLED = TRUE');
   for i := 0 to ResultatWMI.Count-1 do
    begin
     Memo1.Lines.add('================================================');
     for j := 0 to Infos.Count-1 do
         begin
         Memo1.Lines.add(TstringList(ResultatWMI[i]).Names[j]+' : '+
         TstringList (ResultatWMI[i]).ValueFromIndex[j]);
         end;
    end;
  // Nettoyage   - Cleaning
    for i := 0 to ResultatWMI.Count-1 do
         begin
           TstringList(ResultatWMI[i]).Free;
         end;
    Infos.Free;
end;
Function is in the utilwmi unit  ( attachment )

hope help some people with this. If you see errors or optimizations in source code , say it to me.
To be continued if useful.

Friendly, J.P
« Last Edit: July 29, 2015, 12:40:27 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Does FreePascal have a WMI Query library?
« Reply #3 on: January 03, 2016, 01:42:53 am »
hello,
a new version of the unit has been released. Thanks to Molly for the improvements : 

Quote
// 0.1  Jurassic Pork Juillet 2015
// 0.2  Molly  Janvier 2016 : improvement : fpc 3.0 compatibility + usage of  TFPObjectList
 
// Changes 2016-jan-02 (Mol)
// - updated/corrected comments
// - Introduction of variable nrValue.
// - Generic solution for variable nr, using nrValue (inc. pointer assignment)
// - re-introduction of calling ShowMessage() when exception occurs (code only
//   active when USE_DIALOG is defined) + reorganized defines


example of use :

Code: Pascal  [Select][+][-]
  1. uses contnrs,utilwmi,
  2. ...
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. var
  5.   WMIResult         : TFPObjectList;
  6.   i                 : Integer;
  7.   PropNamesIndex    : Integer;
  8.   PropNames         : Array[0..2] of String = ( 'Name','MACAddress','NetConnectionId' );
  9. begin
  10.   WMIResult := GetWMIInfo('Win32_NetworkAdapter', PropNames,'WHERE NETENABLED = TRUE');
  11.   for i := 0 to Pred(WMIResult.Count) do
  12.   begin
  13.     Memo1.Append('================================================');
  14.     for PropNamesIndex := Low(PropNames) to High(PropNames) do
  15.     begin
  16.       Memo1.Append(TStringList(WMIResult[i]).Names[PropNamesIndex] + ' : ' +
  17.               TStringList(WMIResult[i]).ValueFromIndex[PropNamesIndex]);
  18.     end;
  19.   end;
  20.   // Clean up
  21.   WMIResult.Free;
  22. end;                    
  23.  

Edit : oops , sorry  :-[   in the example , something was wrong (Win32_NetwokAdapter to check exception handling). Now it is OK with Win32_NetworkAdapter.
Friendly J.P
« Last Edit: January 03, 2016, 08:40:10 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

chineselzh

  • Newbie
  • Posts: 3
Re: Does FreePascal have a WMI Query library?
« Reply #4 on: March 18, 2016, 01:52:20 pm »
thank you very much!

hello,
a new version of the unit has been released. Thanks to Molly for the improvements : 

Quote
// 0.1  Jurassic Pork Juillet 2015
// 0.2  Molly  Janvier 2016 : improvement : fpc 3.0 compatibility + usage of  TFPObjectList
 
// Changes 2016-jan-02 (Mol)
// - updated/corrected comments
// - Introduction of variable nrValue.
// - Generic solution for variable nr, using nrValue (inc. pointer assignment)
// - re-introduction of calling ShowMessage() when exception occurs (code only
//   active when USE_DIALOG is defined) + reorganized defines


example of use :

Code: Pascal  [Select][+][-]
  1. uses contnrs,utilwmi,
  2. ...
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. var
  5.   WMIResult         : TFPObjectList;
  6.   i                 : Integer;
  7.   PropNamesIndex    : Integer;
  8.   PropNames         : Array[0..2] of String = ( 'Name','MACAddress','NetConnectionId' );
  9. begin
  10.   WMIResult := GetWMIInfo('Win32_NetworkAdapter', PropNames,'WHERE NETENABLED = TRUE');
  11.   for i := 0 to Pred(WMIResult.Count) do
  12.   begin
  13.     Memo1.Append('================================================');
  14.     for PropNamesIndex := Low(PropNames) to High(PropNames) do
  15.     begin
  16.       Memo1.Append(TStringList(WMIResult[i]).Names[PropNamesIndex] + ' : ' +
  17.               TStringList(WMIResult[i]).ValueFromIndex[PropNamesIndex]);
  18.     end;
  19.   end;
  20.   // Clean up
  21.   WMIResult.Free;
  22. end;                    
  23.  

Edit : oops , sorry  :-[   in the example , something was wrong (Win32_NetwokAdapter to check exception handling). Now it is OK with Win32_NetworkAdapter.
Friendly J.P

christensen

  • Full Member
  • ***
  • Posts: 127
Re: Does FreePascal have a WMI Query library?
« Reply #5 on: June 10, 2016, 06:13:49 pm »
Hi,

I've been working on a project for long time which is using this WMI library, now i'm trying to reinstall the system after my HDD fail and lost everything.
So from backup of my source code i'm trying to recover as much i can.

When i'm trying to compile i got this error on WMI lib:

Quote
utilwmi.pas(47,37) Error: Call by var for arg no. 3 has to match exactly: Got "Pointer" expected "LongWord"


with other warning as well:

Quote
Messages, Warnings: 1
Exit code 1, Errors: 1, Warnings: 5
utilwmi.pas(41,15) Warning: Implicit string type conversion from "AnsiString" to "WideString"
utilwmi.pas(43,15) Warning: Implicit string type conversion from "AnsiString" to "WideString"
utilwmi.pas(47,37) Error: Call by var for arg no. 3 has to match exactly: Got "Pointer" expected "LongWord"
utilwmi.pas(53,22) Warning: Implicit string type conversion from "AnsiString" to "WideString"
utilwmi.pas(56,36) Warning: Symbol "SysToUTF8" is deprecated: "Use the function in LazUTF8 unit"
utilwmi.pas(61,67) Warning: Implicit string type conversion with potential data loss from "WideString" to "AnsiString"
Lazarus 1.4.4, FPC 2.6.4, Windows 7 64bit, AMD Athlon 7750 black edition, Asus M3N78-CM
Lenovo L540 i7-4702MQ, Windows 10 x64,Lazarus 1.6.2,FPC 3.0

d.ioannidis

  • Full Member
  • ***
  • Posts: 221
    • Nephelae
Re: Does FreePascal have a WMI Query library?
« Reply #6 on: June 10, 2016, 09:29:33 pm »
Hi,

Does FreePascal have a WMI Query , Windows Management instrumentation library?

 I played a little with this WMI Delphi Code Creator ( don't let the name misguide you, it works with fpc also, or at least it worked  ) in the past. You could look at it if it's suitable for you.

[EDIT] Ooops, I din't see that taazz already mention it. Sorry...

regards,
« Last Edit: June 10, 2016, 09:33:47 pm by Dimitrios Chr. Ioannidis »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Does FreePascal have a WMI Query library?
« Reply #7 on: June 11, 2016, 05:48:15 am »
utilwmi.pas(47,37) Error: Call by var for arg no. 3 has to match exactly: Got "Pointer" expected "LongWord"

with other warning as well:
Seems you upgraded your FPC version as well ?

Try version 0.2 of wmiutil unit. In theory that version should be able to support fpc 3.0.0 (as well as previous versions).

christensen

  • Full Member
  • ***
  • Posts: 127
Re: Does FreePascal have a WMI Query library?
« Reply #8 on: June 14, 2016, 10:36:15 pm »
Yes, i've upgrade everything.


I've tried v2 and now i got other error:
Quote
main.pas(239,50) Error: Incompatible type for arg no. 2: Got "TStringList", expected "{Open} Array Of AnsiString"

in this section of code:

Code: Pascal  [Select][+][-]
  1.  // read bios sn
  2.     Infos := TstringList.Create;
  3.         Infos.CommaText:='SerialNumber=';
  4.  
  5.      ResultatWMI := GetWMIInfo('Win32_BIOS',Infos);
  6.       Result:=TstringList (ResultatWMI[f]).ValueFromIndex[j];
  7.  
  8.       Infos.Free;      
« Last Edit: June 14, 2016, 10:40:55 pm by christensen »
Lazarus 1.4.4, FPC 2.6.4, Windows 7 64bit, AMD Athlon 7750 black edition, Asus M3N78-CM
Lenovo L540 i7-4702MQ, Windows 10 x64,Lazarus 1.6.2,FPC 3.0

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Does FreePascal have a WMI Query library?
« Reply #9 on: June 14, 2016, 11:22:21 pm »
I've tried v2 and now i got other error:
Quote
main.pas(239,50) Error: Incompatible type for arg no. 2: Got "TStringList", expected "{Open} Array Of AnsiString"
The argument type was changed from a stringlist into an open array of strings, hence the error.

Code: Pascal  [Select][+][-]
  1. program example;
  2.  
  3. {$MODE OBJFPC}{$H+}
  4.  
  5. uses
  6.   classes,
  7.   utilwmi2,
  8.   contnrs;
  9.  
  10. procedure BIOS;
  11. var
  12.   WMIResult         : TFPObjectList;
  13.   f,j               : integer;
  14.   retVal            : String;
  15. begin
  16.   WMIResult := GetWMIInfo('Win32_BIOS', ['SerialNumber', 'Manufacturer']);
  17.  
  18.   for f := 0 to Pred(WMIResult.Count) do
  19.   begin
  20.     j := 0;  // first entry = SerialNumber
  21.     retVal := TStringList(WMIResult[f]).ValueFromIndex[j];
  22.     WriteLn(retVal);
  23.  
  24.     j := 1;  // second entry = Manufacturer
  25.     retVal := TStringList(WMIResult[f]).ValueFromIndex[j];
  26.     WriteLn(retVal);
  27.   end;
  28.  
  29.   WMIResult.Free;
  30. end;
  31.  
  32. begin
  33.   BIOS;
  34. end.
  35.  

jma_sp

  • Full Member
  • ***
  • Posts: 150
  • El conocimiento si ocupa lugar.
Re: Does FreePascal have a WMI Query library?
« Reply #10 on: December 19, 2016, 02:46:09 pm »
Thanks Jurasic Pork and Molly, it works!!! What license uses it?

I have been trying with tprocess and wmic but it stops and nothing returns. With this all ok. :)

There is a lot of information that can be returned, by example:


wmic -?

Returns a list of parameters... if it get BIOS we change as Win32_BIOS, if CPU Win32_CPU,

If Desktop Win32_Desktop,............





For people searching for info in BIOS.....thereis also a project:

https://github.com/RRUZ/tsmbios 

It can be compiled easily with Lazarus.
« Last Edit: December 19, 2016, 03:21:14 pm by jma_sp »
Devuan Beowulf 3.0( JWM/ROX/iDesk) - Puppy Linux,  Haiku OS,.ReactOS 0.4.xx  - FreeDos .

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Does FreePascal have a WMI Query library?
« Reply #11 on: December 19, 2016, 03:04:35 pm »
Thanks Jurasic Pork and Molly, it works!!! What license uses it?
I have no idea. afaik It's Jurasic Pork's project so whatever J.P. says/tells goes as far as i am concerned.

I'm just glad that you liked it and/or the project was useful for you.

Thanks for the link to tsmbios as it is a very nice project. I already stumbled upon it a while ago but forgot about it already  :-[

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: Does FreePascal have a WMI Query library?
« Reply #12 on: December 19, 2016, 04:10:32 pm »
Thanks Jurasic Pork and Molly, it works!!!
I'm new here, but I think the design
Code: Pascal  [Select][+][-]
  1. try
  2. ...
  3. except
  4.   on e: Exception do raise;
  5. end;
is not very good. And with this approach you need to free the memory occupied by the Result. I.e.
Code: Pascal  [Select][+][-]
  1. except
  2.   Result.Free;
  3.   raise;
  4. end;

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Does FreePascal have a WMI Query library?
« Reply #13 on: December 19, 2016, 04:32:17 pm »
@ASerge:
You are correct.

I would choose another solution though, e.g. the way exceptions are intended to be used.

The example codes seen can sometimes be wrong in that regards, and actually should look more like:
Code: Pascal  [Select][+][-]
  1. begin
  2.   try
  3.     WMIResult := GetWMIInfo('Win32_BIOS', ['SerialNumber', 'Manufacturer']);
  4.     for i := 0 to Pred(WMIResult.Count) do
  5.     begin
  6.       ..
  7.     end;
  8.   finally
  9.     WMIResult.Free;
  10.   end;
  11. end;
  12.  

Can you notice the difference ?

Also note that v3 was released here, which adds support for WMI array return values.

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Does FreePascal have a WMI Query library?
« Reply #14 on: December 19, 2016, 05:24:31 pm »
hello,
here is now the official version of utilwmi in attachment with a MIT Licence :
Quote
(**********************************************************************************
 Copyright (c) 2016 Jurassic Pork - Molly

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in the
Software without restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the
following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
***********************************************************************************)
unit Utilwmi;
 
// 0.1  Jurassic Pork Juillet 2015
// 0.2  Molly  Janvier 2016 : improvement : fpc 3.0 compatibility + usage of  TFPObjectList
 
// Changes 2016-jan-02 (Mol)
// - updated/corrected comments
// - Introduction of variable nrValue.
// - Generic solution for variable nr, using nrValue (inc. pointer assignment)
// - re-introduction of calling ShowMessage() when exception occurs (code only
//   active when USE_DIALOG is defined) + reorganized defines
 
// 0.3  Molly  November 2016 : improvement : support for variant arrays
// Changes 2016-nov-11 (Mol)
// - Add support for variant arrays   

to test the support for variant arrays  try :
Code: Pascal  [Select][+][-]
  1.  WMIResult := GetWMIInfo('Win32_BIOS', ['BIOSVersion', 'Manufacturer']);  

BIOSVersion wmi result is a variant array.

Friendly, J.P

« Last Edit: December 19, 2016, 05:40:54 pm by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

 

TinyPortal © 2005-2018