Function GetWMIInfo(const WMIClass: string ; const WMIProperty:tstringList;
const Condition: string = ''):tlist;
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 ) // 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
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 :
uses contnrs,utilwmi, ... procedure TForm1.Button1Click(Sender: TObject); var WMIResult : TFPObjectList; i : Integer; PropNamesIndex : Integer; PropNames : Array[0..2] of String = ( 'Name','MACAddress','NetConnectionId' ); begin WMIResult := GetWMIInfo('Win32_NetworkAdapter', PropNames,'WHERE NETENABLED = TRUE'); for i := 0 to Pred(WMIResult.Count) do begin Memo1.Append('================================================'); for PropNamesIndex := Low(PropNames) to High(PropNames) do begin Memo1.Append(TStringList(WMIResult[i]).Names[PropNamesIndex] + ' : ' + TStringList(WMIResult[i]).ValueFromIndex[PropNamesIndex]); end; end; // Clean up WMIResult.Free; end;
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
utilwmi.pas(47,37) Error: Call by var for arg no. 3 has to match exactly: Got "Pointer" expected "LongWord"
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"
Does FreePascal have a WMI Query , Windows Management instrumentation library?
utilwmi.pas(47,37) Error: Call by var for arg no. 3 has to match exactly: Got "Pointer" expected "LongWord"Seems you upgraded your FPC version as well ?
with other warning as well:
main.pas(239,50) Error: Incompatible type for arg no. 2: Got "TStringList", expected "{Open} Array Of AnsiString"
I've tried v2 and now i got other error:The argument type was changed from a stringlist into an open array of strings, hence the error.Quotemain.pas(239,50) Error: Incompatible type for arg no. 2: Got "TStringList", expected "{Open} Array Of AnsiString"
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 new here, but I think the designThanks Jurasic Pork and Molly, it works!!!
(**********************************************************************************
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
Can you notice the difference ?Of course. Try must be used AFTER the resource allocation. But to write code with errors based on incorrect application is also wrong.
@ASerge:Why not? Once the memory is allocated inside the function, when internal errors - it is must to clean.
I'm uncomfortable with the solution as shown by you earlier, because it is usually end-user responsibility to act on exceptions.
In addition to that. It might be that only one 'entry' went wrong while all other entries are (still) valid. It would be unwise to clear the whole objectlist for that ?
Since when can you hardcast to a TStringlist? If that works it is by accident.... Tstringlist is not an array..... it is a class.Have you checked out what WMIResult := GetWMIInfo() really does before posting that comment ??
Why not? Once the memory is allocated inside the function, when internal errors - it is must to clean.Sure, but even better would be to have a separate wmi class that does everything internally (an act correctly when something goes wrong, e.g. not bother the user with it).
In my opinion it is more convenient when cleaning the one, who allocate. But if we delegate to function allocate only, I prefer this approach:
Here is the code but I need to pass drive letter (D, F, H) and want to get physical serial number:Not possible to accomplish with using utilWMI.
Not possible to accomplish with using utilWMI.To do this , you must use Associators of WQL Request.
Use your own instance of wmi and associate the proper objects to each other. Hint: you need to parse things the other way around and in the end match your drive-letter. (DiskDrive -> Partition -> LogicalDisk -> DriveLetter -> DiskDrive.SerialNumber)