forked from JurassicPork/TLazSerial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilwmi.pas
More file actions
188 lines (167 loc) · 6.85 KB
/
utilwmi.pas
File metadata and controls
188 lines (167 loc) · 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
(**********************************************************************************
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;
//Downloaded from https://forum.lazarus.freepascal.org/index.php/topic,24490.msg232201.html#msg232201
//and the modified to 0.4
// 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
// 0.4 СМ630 2026 : Added Attempt and RetryInterval properties, to allow calls without Async
{$MODE OBJFPC}{$H+}{$HINTS ON}
{$IF FPC_FULLVERSION > 29999} // FPC > 2.9.9
{$INFO "Use new WMI"}
{$ELSE}
{$INFO "Use old WMI"}
{$DEFINE USE_OLD_WMI}
{$ENDIF}
// Enable this define if wanting to use ShowMessage() to inform about an
// exception.
// Please realize that using unit Dialogs can 'clash', as both Freevision and
// lazarus have a unit with the same name.
//{$DEFINE USE_DIALOG}
interface
uses
Classes,contnrs;
function GetWMIInfo(const WMIClass: string; const WMIPropertyNames: Array of String; const Condition: string = ''; Attempts: integer=1; RetryInterval: Integer= 1000): TFPObjectList;
implementation
Uses
{$IFDEF USE_DIALOG}
Dialogs,
{$ENDIF}
Variants,
ActiveX,
ComObj,
SysUtils;
function VarArrayToStr(Value: Variant): String;
var
i : Integer;
begin
Result := '[';
for i := VarArrayLowBound(Value, 1) to VarArrayHighBound(Value, 1) do
begin
if Result <> '[' then Result := Result + ',';
if not VarIsNull(Value[i]) then
begin
if VarIsArray(Value[i])
then Result := Result + VarArrayToStr(Value[i])
else Result := Result + VartoStr(Value[i])
end
else Result := Result + '<null>';
end;
Result := Result + ']';
end;
function GetobjWMIService(Attempts: integer = 1; RetryInterval: Integer = 1000): variant;
var
FSWbemLocator : variant;
i: integer;
success: boolean = false;
debtime: QWord;
begin
debtime := GetTickCount64;
for i:= 1 to Attempts do
begin
try
FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
Result := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', ''); //Executing this line might take a few seconds!
success := true;
except
Sleep(RetryInterval);
end;
if success then Break;
end;
end;
function GetWMIInfo(const WMIClass: string; const WMIPropertyNames: Array of String; const Condition: string = ''; Attempts: integer=1; RetryInterval: Integer= 1000): TFPObjectList;
const
wbemFlagForwardOnly = $00000020;
var
objWMIService : Variant;
colWMI : Variant;
oEnumWMI : IEnumvariant;
nrValue : LongWord;
{$IFDEF USE_OLD_WMI}
objWMI : Variant; // FPC < 3.0 requires WMIobj to be an variant, not an OleVariant
nr : PLongWord; // FPC < 3.0 requires IEnumvariant.next to supply a pointer to a longword for # returned values
{$ELSE}
objWMI : OLEVariant; // FPC 3.0 requires WMIobj to be an olevariant, not a variant
nr : LongWord absolute nrValue; // FPC 3.0 requires IEnumvariant.next to supply a longword variable for # returned values
{$ENDIF}
WMIproperties : String;
WMIProp : TStringList;
Request : String;
PropertyName : String;
PropertyStrVal: String;
i : integer;
begin
{$IFDEF USE_OLD_WMI}
nr := @nrValue;
{$ENDIF}
// Prepare the search query
WMIProperties := '';
for i := low(WMIPropertyNames) to High(WMIPropertyNames)
do WMIProperties := WMIProperties + WMIPropertyNames[i] + ',';
Delete(WMIProperties, length(WMIProperties), 1);
// Let FPObjectList take care of freeing the objects
Result:= TFPObjectList.Create(True);
try
objWMIService := GetobjWMIService(Attempts, RetryInterval);
if Condition = ''
then Request := Format('SELECT %s FROM %s' , [WMIProperties, WMIClass])
else Request := Format('SELECT %s FROM %s %s', [WMIProperties, WMIClass, Condition]);
// Start Request
colWMI := objWMIService.ExecQuery(WideString(Request), 'WQL', wbemFlagForwardOnly);
// Enum for requested results
oEnumWMI := IUnknown(colWMI._NewEnum) as IEnumVariant;
// Enumerate results from query, one by one
while oEnumWMI.Next(1, objWMI, nr) = 0 do
begin
// Store all property name/value pairs for this enum to TStringList.
WMIprop := TStringList.Create;
for i := low(WMIPropertyNames) to High(WMIPropertyNames) do
begin
PropertyName := WMIPropertyNames[i];
If not VarIsNull(objWMI.Properties_.Item(WideString(PropertyName)).value) then
begin
if VarIsArray(objWMI.Properties_.Item(WideString(PropertyName)).value)
then PropertyStrVal := VarArrayToStr(objWMI.Properties_.Item(WideString(PropertyName)).value)
else PropertyStrVal := VartoStr(objWMI.Properties_.Item(WideString(PropertyName)).value)
end
else PropertyStrVal := '<null>';
WMIProp.Add(PropertyName + '=' + PropertyStrVal);
end; //for i
// Add properties from this enum to FPObjectList as TStringList
Result.Add(WMIProp);
end; //while
except
{$IFDEF USE_DIALOG}
on e: Exception do
ShowMessage('Error WMI with ' + Request + #13#10 + 'Error : ' + e.Message);
{$ELSE}
// Replace Raise with more appropiate exception if wanted.
on e: Exception do Raise;
{$ENDIF}
end; //try
end;
end.