-
Notifications
You must be signed in to change notification settings - Fork 46
/
Main.pas
570 lines (501 loc) · 18.1 KB
/
Main.pas
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
// **************************************************************************************************
//
// Unit Main
// unit for the WMI Delphi Code Creator
// https://github.com/RRUZ/wmi-delphi-code-creator
//
// The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License");
// you may not use this file except in compliance with the License. You may obtain a copy of the
// License at http://www.mozilla.org/MPL/
//
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
// ANY KIND, either express or implied. See the License for the specific language governing rights
// and limitations under the License.
//
// The Original Code is Main.pas.
//
// The Initial Developer of the Original Code is Rodrigo Ruz V.
// Portions created by Rodrigo Ruz V. are Copyright (C) 2011-2023 Rodrigo Ruz V.
// All Rights Reserved.
//
// **************************************************************************************************
unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, ExtCtrls, Rtti, Generics.Collections, WDCC.HostsAdmin,
SynEdit, ImgList, ToolWin, WDCC.Settings, Menus, Buttons, Vcl.Styles.ColorTabs,
Vcl.PlatformDefaultStyleActnCtrls, Vcl.ActnPopup, Vcl.ActnList, Vcl.ActnMan,
System.Actions, System.ImageList;
type
TFrmMain = class(TForm)
PanelMain: TPanel;
StatusBar1: TStatusBar;
ToolBar1: TToolBar;
ToolButtonAbout: TToolButton;
ToolButton4: TToolButton;
MemoConsole: TMemo;
PanelConsole: TPanel;
ImageList1: TImageList;
PageControl2: TPageControl;
TabSheet3: TTabSheet;
ToolButtonSettings: TToolButton;
PopupActionBar1: TPopupActionBar;
TreeViewTasks: TTreeView;
Splitter1: TSplitter;
PageControlTasks: TPageControl;
TabSheet1: TTabSheet;
PageControl3: TPageControl;
TabSheetTask: TTabSheet;
TabSheet2: TTabSheet;
MemoLog: TMemo;
Splitter2: TSplitter;
ActionManager1: TActionManager;
ActionRegisterHost: TAction;
RegisterHost1: TMenuItem;
ActionConnect: TAction;
ConnecttoHost1: TMenuItem;
ActionPing: TAction;
PingHost1: TMenuItem;
ToolButton1: TToolButton;
ActionDisconnect: TAction;
DisconnectHost1: TMenuItem;
ToolButtonExit: TToolButton;
PopupActionBar2: TPopupActionBar;
procedure FormCreate(Sender: TObject);
procedure ToolButtonAboutClick(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure ToolButtonSettingsClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure TreeViewTasksChange(Sender: TObject; Node: TTreeNode);
procedure FormShow(Sender: TObject);
procedure ActionRegisterHostUpdate(Sender: TObject);
procedure ActionRegisterHostExecute(Sender: TObject);
procedure ActionConnectUpdate(Sender: TObject);
procedure ActionConnectExecute(Sender: TObject);
procedure ActionPingUpdate(Sender: TObject);
procedure ActionPingExecute(Sender: TObject);
procedure ActionDisconnectUpdate(Sender: TObject);
procedure ToolButton1Click(Sender: TObject);
procedure ToolButtonExitClick(Sender: TObject);
procedure TreeViewTasksCustomDrawItem(Sender: TCustomTreeView; Node: TTreeNode; State: TCustomDrawState;
var DefaultDraw: Boolean);
private
FSettings: TSettings;
FCtx: TRttiContext;
FRegisteredInstances: TDictionary<string, TForm>;
FListWINHosts: TObjectList<TWMIHost>;
procedure RegisterTask(const ParentTask, Name: string; ImageIndex: Integer; LinkObject: TRttiType);
procedure RegisterWMIHosts;
procedure SetLog(const Log: string);
procedure SetMsg(const Msg: string);
public
property Settings: TSettings read FSettings;
end;
var
FrmMain: TFrmMain;
implementation
uses
WDCC.StdActions.PopMenu,
WDCC.Misc,
uWmi_Metadata,
WDCC.Log,
WDCC.Globals,
WDCC.Sql.WMI.Container,
WDCC.WMI.Classes.Container,
WDCC.WMI.Database,
WDCC.WMI.Classes.Tree,
WDCC.WMI.Events.Container,
WDCC.WMI.Methods.Container,
WDCC.WMI.Tree,
WDCC.WMI.Info,
Vcl.Styles.FormStyleHooks,
Vcl.Styles.Ext,
Vcl.Themes,
WDCC.About;
Const
HostCIMStr = 'CIM Repository (%s)';
{$R *.dfm}
function GetNodeByText(ATree: TTreeView; const AValue: String; AVisible: Boolean = False): TTreeNode;
var
Node: TTreeNode;
begin
Result := nil;
if ATree.Items.Count = 0 then
Exit;
Node := ATree.Items[0];
while Node <> nil do
begin
if SameText(Node.Text, AValue) then
begin
Result := Node;
if AVisible then
Result.MakeVisible;
Break;
end;
Node := Node.GetNext;
end;
end;
procedure TFrmMain.ActionConnectExecute(Sender: TObject);
Var
LForm: TFrmWMIInfo;
LWMIHost: TWMIHost;
LNameSpaces: TStrings;
LIndex: Integer;
LNode: TTreeNode;
begin
LWMIHost := TWMIHost(TreeViewTasks.Selected.Data);
if LWMIHost.Form = nil then
begin
if Ping(LWMIHost.Host, 4, 32, MemoConsole.Lines) then
begin
LForm := TFrmWMIInfo.Create(Self);
LForm.Parent := TabSheetTask;
LForm.BorderStyle := bsNone;
LForm.Align := alClient;
LForm.SetLog := SetLog;
LForm.WMIHost := LWMIHost;
LWMIHost.Form := LForm;
LWMIHost.Form.Show;
TreeViewTasks.Selected.ImageIndex := 6;
TreeViewTasks.Selected.SelectedIndex := 6;
LNameSpaces := TStringList.Create;
SetMsg(Format('Getting WMI namespaces from [%s]', [LWMIHost.Host]));
try
LNameSpaces.AddStrings(CachedWMIClasses.GetNameSpacesHost(LWMIHost.Host, LWMIHost.User, LWMIHost.Password));
for LIndex := 0 to LNameSpaces.Count - 1 do
RegisterTask(Format(HostCIMStr, [LWMIHost.Host]), LNameSpaces[LIndex], 58, FCtx.GetType(TFrmWMITree));
LNode := GetNodeByText(TreeViewTasks, Format(HostCIMStr, [LWMIHost.Host]));
if LNode <> nil then
LNode.Expand(True);
finally
LNameSpaces.Free;
SetMsg('');
end;
end
else
begin
TreeViewTasks.Selected.ImageIndex := 12;
TreeViewTasks.Selected.SelectedIndex := 12;
MsgWarning('Was not possible establish a connection with the host');
end;
end;
end;
procedure TFrmMain.ActionConnectUpdate(Sender: TObject);
begin
TAction(Sender).Enabled := (PageControlTasks.Visible) and (TreeViewTasks.Selected <> nil) and
(TObject(TreeViewTasks.Selected.Data) is TWMIHost) and (TWMIHost(TreeViewTasks.Selected.Data).Form = nil);
end;
procedure TFrmMain.ActionDisconnectUpdate(Sender: TObject);
begin
TAction(Sender).Enabled := (PageControlTasks.Visible) and (TreeViewTasks.Selected <> nil) and
(TObject(TreeViewTasks.Selected.Data) is TWMIHost) and (TWMIHost(TreeViewTasks.Selected.Data).Form <> nil);
end;
procedure TFrmMain.ActionPingExecute(Sender: TObject);
begin
Ping(TWMIHost(TreeViewTasks.Selected.Data).Host, 4, 32, MemoConsole.Lines);
end;
procedure TFrmMain.ActionPingUpdate(Sender: TObject);
begin
TAction(Sender).Enabled := (PageControlTasks.Visible) and (TreeViewTasks.Selected <> nil) and
(TObject(TreeViewTasks.Selected.Data) is TWMIHost);
end;
procedure TFrmMain.ActionRegisterHostExecute(Sender: TObject);
Var
Frm: TFrmHostAdmin;
begin
Frm := TFrmHostAdmin.Create(Self);
Frm.ShowModal;
end;
procedure TFrmMain.ActionRegisterHostUpdate(Sender: TObject);
begin
TAction(Sender).Enabled := True;
end;
procedure TFrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
begin
WriteSettings(Settings);
end;
procedure TFrmMain.FormCreate(Sender: TObject);
Var
LNameSpaces: TStrings;
LIndex: Integer;
begin
{$WARN SYMBOL_PLATFORM OFF}
// ReportMemoryLeaksOnShutdown:=DebugHook<>0;
{$WARN SYMBOL_PLATFORM ON}
// FillPopupActionBar(PopupActionBar1);
FillPopupActionBar(PopupActionBar2);
AssignStdActionsPopUpMenu(Self, PopupActionBar2);
FListWINHosts := nil;
FCtx := TRttiContext.Create;
FRegisteredInstances := TDictionary<string, TForm>.Create;
FSettings := TSettings.Create;
SetLog('Reading settings');
ReadSettings(FSettings);
LoadVCLStyle(Settings.VCLStyle);
if FSettings.DisableVClStylesNC then
begin
TStyleManager.Engine.RegisterStyleHook(TCustomForm, TFormStyleHookNC);
TStyleManager.Engine.RegisterStyleHook(TForm, TFormStyleHookNC);
// GlassFrame.Enabled:=True;
end
// else
// if FSettings.ActivateCustomForm then
// begin
// TStyleManager.Engine.RegisterStyleHook(TCustomForm, TFormStyleHookBackground);
// TStyleManager.Engine.RegisterStyleHook(TForm, TFormStyleHookBackground);
//
// if FSettings.CustomFormNC then
// begin
// TFormStyleHookBackground.NCSettings.Enabled := True;
// TFormStyleHookBackground.NCSettings.UseColor := FSettings.UseColorNC;
// TFormStyleHookBackground.NCSettings.Color := FSettings.ColorNC;
// TFormStyleHookBackground.NCSettings.ImageLocation := FSettings.ImageNC;
// end;
//
// if FSettings.CustomFormBack then
// begin
// TFormStyleHookBackground.BackGroundSettings.Enabled := True;
// TFormStyleHookBackground.BackGroundSettings.UseColor := FSettings.UseColorBack;
// TFormStyleHookBackground.BackGroundSettings.Color := FSettings.ColorBack;
// TFormStyleHookBackground.BackGroundSettings.ImageLocation := FSettings.ImageBack;
// end;
//
// end
;
// RegisterTask('','Code Generation', 30, nil);
RegisterTask('', 'WMI Class Code Generation', 40, FCtx.GetType(TFrmWMiClassesContainer));
RegisterTask('', 'WMI Methods Code Generation', 41, FCtx.GetType(TFrmWmiMethodsContainer));
RegisterTask('', 'WMI Events Code Generation', 45, FCtx.GetType(TFrmWmiEventsContainer));
// RegisterTask('','WMI Explorer', 29, Ctx.GetType(TFrmWMITree));
RegisterTask('', 'WMI Classes Tree', 47, FCtx.GetType(TFrmWmiClassTree));
RegisterTask('', 'WMI Finder', 57, FCtx.GetType(TFrmWmiDatabase));
RegisterTask('', 'WQL', 56, FCtx.GetType(TFrmSqlWMIContainer));
// RegisterTask('','Events Monitor', 28, nil);
// RegisterTask('','Log', 32, Ctx.GetType(TFrmLog));
RegisterTask('', 'CIM Repository (localhost)', 6, FCtx.GetType(TFrmWMIInfo));
LNameSpaces := TStringList.Create;
try
LNameSpaces.AddStrings(CachedWMIClasses.NameSpaces);
for LIndex := 0 to LNameSpaces.Count - 1 do
RegisterTask('CIM Repository (localhost)', LNameSpaces[LIndex], 58, FCtx.GetType(TFrmWMITree));
finally
LNameSpaces.Free;
end;
{
for LHost in GetWMIRegisteredHosts do
RegisterTask('',Format('CIM Repository (%s)', [LHost]), 31, nil);
}
RegisterWMIHosts;
TreeViewTasks.FullExpand;
// TreeViewTasks.Selected:=TreeViewTasks.Items[0];
MemoConsole.Color := Settings.BackGroundColor;
MemoConsole.Font.Color := Settings.ForeGroundColor;
MemoLog.Color := MemoConsole.Color;
MemoLog.Font.Color := MemoConsole.Font.Color;
StatusBar1.Panels[2].Text := Format('WMI installed version %s', [GetWmiVersion]);
{
AssignStdActionsPopUpMenu(Self, PopupActionBar1);
ApplyVclStylesOwnerDrawFix(Self, True);
}
end;
procedure TFrmMain.FormDestroy(Sender: TObject);
Var
Pair: TPair<string, TForm>;
begin
if FListWINHosts <> nil then
FreeAndNil(FListWINHosts);
for Pair in FRegisteredInstances do
begin
Pair.Value.Close;
Pair.Value.Free;
end;
FRegisteredInstances.Free;
Settings.Free;
end;
procedure TFrmMain.FormShow(Sender: TObject);
begin
if TreeViewTasks.Selected = nil then
TreeViewTasks.Selected := TreeViewTasks.Items[0];
end;
procedure TFrmMain.SetLog(const Log: string);
begin
MemoLog.Lines.Add(Log);
end;
procedure TFrmMain.SetMsg(const Msg: string);
begin
StatusBar1.Panels[0].Text := Msg;
StatusBar1.Update;
end;
procedure TFrmMain.ToolButton1Click(Sender: TObject);
begin
PageControlTasks.Visible := not PageControlTasks.Visible;
end;
procedure TFrmMain.ToolButtonAboutClick(Sender: TObject);
var
Frm: TFrmAbout;
begin
Frm := TFrmAbout.Create(nil);
Frm.ShowModal();
end;
procedure TFrmMain.ToolButtonExitClick(Sender: TObject);
begin
Close();
end;
procedure TFrmMain.RegisterTask(const ParentTask, Name: string; ImageIndex: Integer; LinkObject: TRttiType);
Var
PNode: TTreeNode;
Node: TTreeNode;
begin
if ParentTask = '' then
Node := TreeViewTasks.Items.AddObject(nil, Name, LinkObject)
else
begin
PNode := GetNodeByText(TreeViewTasks, ParentTask);
Node := TreeViewTasks.Items.AddChildObject(PNode, Name, LinkObject);
end;
Node.ImageIndex := ImageIndex; // add BN ??
Node.SelectedIndex := ImageIndex;
end;
procedure TFrmMain.RegisterWMIHosts;
Var
Node: TTreeNode;
LWMIHost: TWMIHost;
begin
if FListWINHosts <> nil then
FreeAndNil(FListWINHosts);
FListWINHosts := GetListWMIRegisteredHosts;
for LWMIHost in FListWINHosts do
begin
Node := TreeViewTasks.Items.AddObject(nil, Format(HostCIMStr, [LWMIHost.Host]), LWMIHost);
Node.ImageIndex := 31; // add BN ??
Node.SelectedIndex := 31;
end;
end;
procedure TFrmMain.ToolButtonSettingsClick(Sender: TObject);
var
Frm: TFrmSettings;
begin
Frm := TFrmSettings.Create(nil);
try
Frm.Form := Self;
Frm.LoadSettings;
Frm.ShowModal();
finally
Frm.Free;
ReadSettings(FSettings);
end;
end;
procedure TFrmMain.TreeViewTasksChange(Sender: TObject; Node: TTreeNode);
var
LRttiInstanceType: TRttiInstanceType;
LValue: TValue;
LRttiProperty: TRttiProperty;
LForm: TForm;
LIndex: Integer;
LProc: TProcLog;
begin
if Node.Text <> '' then
begin
TabSheetTask.Caption := Node.Text;
TabSheetTask.ImageIndex := Node.ImageIndex;
for LIndex := 0 to TabSheetTask.ControlCount - 1 do
if (TabSheetTask.Controls[LIndex] is TForm) and (TForm(TabSheetTask.Controls[LIndex]).Visible) then
begin
TForm(TabSheetTask.Controls[LIndex]).Hide;
Break;
end;
if (Node.Data <> nil) and (Node.Parent <> nil) and (TObject(Node.Parent.Data) is TWMIHost) then
begin
if FRegisteredInstances.ContainsKey(Node.Parent.Text + Node.Text) then
FRegisteredInstances.Items[Node.Parent.Text + Node.Text].Show
else if Node.Data <> nil then
begin
LRttiInstanceType := TRttiInstanceType(Node.Data);
LValue := LRttiInstanceType.GetMethod('Create').Invoke(LRttiInstanceType.MetaclassType, [Self]);
LForm := TForm(LValue.AsObject);
LForm.Parent := TabSheetTask;
LForm.BorderStyle := bsNone;
LForm.Align := alClient;
LRttiProperty := LRttiInstanceType.GetProperty('Console');
if LRttiProperty <> nil then
LRttiProperty.SetValue(LForm, MemoConsole);
LProc := SetMsg;
LRttiProperty := LRttiInstanceType.GetProperty('SetMsg');
if LRttiProperty <> nil then
LRttiProperty.SetValue(LForm, TValue.From<TProcLog>(LProc));
LProc := SetLog;
LRttiProperty := LRttiInstanceType.GetProperty('SetLog');
if LRttiProperty <> nil then
LRttiProperty.SetValue(LForm, TValue.From<TProcLog>(LProc));
LRttiProperty := LRttiInstanceType.GetProperty('Settings');
if LRttiProperty <> nil then
LRttiProperty.SetValue(LForm, FSettings);
LRttiProperty := LRttiInstanceType.GetProperty('NameSpace');
if LRttiProperty <> nil then
LRttiProperty.SetValue(LForm, Node.Text);
LRttiProperty := LRttiInstanceType.GetProperty('WMIHost');
if LRttiProperty <> nil then
LRttiProperty.SetValue(LForm, TWMIHost(Node.Parent.Data));
LForm.Show;
FRegisteredInstances.Add(Node.Parent.Text + Node.Text, LForm);
end;
end
else if (Node.Data <> nil) and (TObject(Node.Data) is TWMIHost) then
begin
if TWMIHost(Node.Data).Form <> nil then
TWMIHost(Node.Data).Form.Show;
end
else
begin
if FRegisteredInstances.ContainsKey(Node.Text) then
FRegisteredInstances.Items[Node.Text].Show
else if Node.Data <> nil then
begin
LRttiInstanceType := TRttiInstanceType(Node.Data);
LValue := LRttiInstanceType.GetMethod('Create').Invoke(LRttiInstanceType.MetaclassType, [Self]);
LForm := TForm(LValue.AsObject);
LForm.Parent := TabSheetTask;
LForm.BorderStyle := bsNone;
LForm.Align := alClient;
LRttiProperty := LRttiInstanceType.GetProperty('Console');
if LRttiProperty <> nil then
LRttiProperty.SetValue(LForm, MemoConsole);
LProc := SetMsg;
LRttiProperty := LRttiInstanceType.GetProperty('SetMsg');
if LRttiProperty <> nil then
LRttiProperty.SetValue(LForm, TValue.From<TProcLog>(LProc));
LProc := SetLog;
LRttiProperty := LRttiInstanceType.GetProperty('SetLog');
if LRttiProperty <> nil then
LRttiProperty.SetValue(LForm, TValue.From<TProcLog>(LProc));
LRttiProperty := LRttiInstanceType.GetProperty('Settings');
if LRttiProperty <> nil then
LRttiProperty.SetValue(LForm, FSettings);
LRttiProperty := LRttiInstanceType.GetProperty('NameSpace');
if LRttiProperty <> nil then
LRttiProperty.SetValue(LForm, Node.Text);
LForm.Show;
FRegisteredInstances.Add(Node.Text, LForm);
end;
end;
end;
end;
procedure TFrmMain.TreeViewTasksCustomDrawItem(Sender: TCustomTreeView; Node: TTreeNode; State: TCustomDrawState;
var DefaultDraw: Boolean);
begin
if not StyleServices.IsSystemStyle then
if cdsSelected in State then
begin
TTreeView(Sender).Canvas.Brush.Color := StyleServices.GetSystemColor(clHighlight);
TTreeView(Sender).Canvas.Font.Color := StyleServices.GetSystemColor(clHighlightText);
end;
end;
initialization
if not IsStyleHookRegistered(TCustomSynEdit, TScrollingStyleHook) then
TStyleManager.Engine.RegisterStyleHook(TCustomSynEdit, TScrollingStyleHook);
{
TCustomStyleEngine.RegisterStyleHook(TCustomTabControl, TTabColorControlStyleHook);
TCustomStyleEngine.RegisterStyleHook(TTabControl, TTabColorControlStyleHook);
}
end.