Subversion Repositories simple_log_event

Rev

Rev 3 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 daniel-mar 1
unit SimpleLogEventSetupMain;
2
 
3
interface
4
 
5
uses
6
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
7
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
8
 
9
type
10
  TForm1 = class(TForm)
11
    GroupBox1: TGroupBox;
12
    Edit1: TEdit;
13
    Button1: TButton;
14
    Edit2: TEdit;
15
    Label1: TLabel;
16
    Label2: TLabel;
17
    GroupBox2: TGroupBox;
18
    ListBox1: TListBox;
19
    Edit3: TEdit;
20
    Button2: TButton;
21
    Button3: TButton;
22
    Label3: TLabel;
23
    Label4: TLabel;
24
    Label5: TLabel;
25
    Label6: TLabel;
26
    Label7: TLabel;
27
    Label8: TLabel;
28
    Label9: TLabel;
29
    Label10: TLabel;
30
    procedure Button1Click(Sender: TObject);
31
    procedure FormShow(Sender: TObject);
32
    procedure Button2Click(Sender: TObject);
33
    procedure Button3Click(Sender: TObject);
34
  private
35
    found32: string;
36
    found64: string;
37
    procedure CheckInstallation;
38
    { Private-Deklarationen }
39
  public
40
    { Public-Deklarationen }
41
  end;
42
 
43
var
44
  Form1: TForm1;
45
 
46
implementation
47
 
48
{$R *.dfm}
49
 
50
{$R DllRes.res}
51
 
52
uses
53
  ShellApi, ShlObj, Registry;
54
 
55
Function Wow64DisableWow64FsRedirection(Var Wow64FsEnableRedirection: LongBool): LongBool; StdCall;
56
  External 'Kernel32.dll' Name 'Wow64DisableWow64FsRedirection';
57
Function Wow64EnableWow64FsRedirection(Wow64FsEnableRedirection: LongBool): LongBool; StdCall;
58
  External 'Kernel32.dll' Name 'Wow64EnableWow64FsRedirection';
59
 
5 daniel-mar 60
procedure RunAndWaitShell(Executable, Parameter: STRING; ShowParameter: INTEGER);
61
var
62
  Info: TShellExecuteInfo;
63
  pInfo: PShellExecuteInfo;
64
  exitCode: DWord;
65
begin
66
  // Source: https://www.delphipraxis.net/31067-shellexecute-wait.html
67
  pInfo := @Info;
68
  with Info do
69
  begin
70
    cbSize := SizeOf(Info);
71
    fMask := SEE_MASK_NOCLOSEPROCESS;
72
    wnd   := application.Handle;
73
    lpVerb := NIL;
74
    lpFile := PChar(Executable);
75
    lpParameters := PChar(Parameter + #0);
76
    lpDirectory := NIL;
77
    nShow       := ShowParameter;
78
    hInstApp    := 0;
79
  end;
80
  ShellExecuteEx(pInfo);
81
  repeat
82
    exitCode := WaitForSingleObject(Info.hProcess, 500);
83
    Application.ProcessMessages;
84
  until (exitCode <> WAIT_TIMEOUT);
85
end;
86
 
3 daniel-mar 87
procedure RegSvr32(const dll: string);
88
begin
5 daniel-mar 89
  //ShellExecute(Form1.Handle, 'open', 'regsvr32.exe', PChar('"' + dll + '"'), '', SW_NORMAL);
90
  RunAndWaitShell('regsvr32.exe', '"'+dll+'"', SW_NORMAL);
3 daniel-mar 91
end;
92
 
93
procedure TForm1.Button1Click(Sender: TObject);
94
var
95
  rs: TResourceStream;
96
  Wow64FsEnableRedirection: LongBool;
97
  reg: TRegistry;
98
  sl: TStringList;
99
  kn: string;
100
  test: string;
101
  lastregfile: string;
102
begin
103
  if not IsUserAnAdmin  then
104
  begin
105
    raise Exception.Create('To register the libraries, this application needs to run as administrator.');
106
  end;
107
 
108
  try
109
    {$REGION 'Copy DLL to common files'}
110
 
111
    if TOSVersion.Architecture = TOSVersion.TArchitecture.arIntelX86 then
112
    begin
113
      {$REGION '32 Bit Windows'}
114
      lastregfile := 'C:\Program Files\Common Files\ViaThinkSoft\ViaThinkSoftSimpleLogEvent32.dll';
115
      ForceDirectories(ExtractFilePath(lastregfile));
116
      rs := TResourceStream.CreateFromID(HInstance, 32, PChar('DLL'));
117
      rs.SaveToFile(lastregfile);
118
      rs.Free;
119
      RegSvr32(lastregfile);
120
      {$ENDREGION}
121
    end;
122
 
123
    if TOSVersion.Architecture = TOSVersion.TArchitecture.arIntelX64 then
124
    begin
125
      {$REGION '64 Bit Windows'}
126
      Wow64DisableWow64FsRedirection(Wow64FsEnableRedirection);
127
      try
128
        lastregfile := 'C:\Program Files (x86)\Common Files\ViaThinkSoft\ViaThinkSoftSimpleLogEvent32.dll';
129
        ForceDirectories(ExtractFilePath(lastregfile));
130
        rs := TResourceStream.CreateFromID(HInstance, 32, PChar('DLL'));
131
        rs.SaveToFile(lastregfile);
132
        rs.Free;
133
        RegSvr32(lastregfile);
134
 
135
        lastregfile := 'C:\Program Files\Common Files\ViaThinkSoft\ViaThinkSoftSimpleLogEvent64.dll';
136
        ForceDirectories(ExtractFilePath(lastregfile));
137
        rs := TResourceStream.CreateFromID(HInstance, 64, PChar('DLL'));
138
        rs.SaveToFile(lastregfile);
139
        rs.Free;
140
        RegSvr32(lastregfile);
141
      finally
142
        Wow64EnableWow64FsRedirection(Wow64FsEnableRedirection);
143
      end;
144
      {$ENDREGION}
145
    end;
146
 
147
    {$ENDREGION}
148
 
149
    {$REGION 'Update DLL path in log provider list'}
150
    reg := TRegistry.Create;
151
    sl := TStringList.Create;
152
    try
153
      reg.RootKey := HKEY_LOCAL_MACHINE;
154
      if reg.OpenKey('SYSTEM\CurrentControlSet\services\eventlog\Application', false) then
155
      begin
156
        reg.GetKeyNames(sl);
157
        reg.CloseKey;
158
        for kn in sl do
159
        begin
160
          if reg.OpenKey('SYSTEM\CurrentControlSet\services\eventlog\Application\' + kn, false) then
161
          begin
162
            test := reg.ReadString('EventMessageFile');
163
            if Pos('VIATHINKSOFTSIMPLELOGEVENT', UpperCase(test)) > 0 then
164
            begin
165
              if test <> lastregfile then
166
              begin
167
                reg.WriteString('EventMessageFile', lastregfile);
168
              end;
169
            end;
170
            reg.CloseKey;
171
          end;
172
        end;
173
      end;
174
    finally
175
      FreeAndNil(reg);
176
      FreeAndNil(sl);
177
    end;
178
    {$ENDREGION}
179
 
180
  finally
181
    CheckInstallation;
182
  end;
183
end;
184
 
185
const
186
  DEFECTIVE_SUFFIX = ' (defective)';
187
 
188
procedure RegisterEventLogProvider(ProviderName, MessageFile: string);
189
var
190
  reg: TRegistry;
191
begin
192
  reg := TRegistry.Create;
193
  try
194
    reg.RootKey := HKEY_LOCAL_MACHINE;
195
    if not reg.OpenKey('SYSTEM\CurrentControlSet\Services\Eventlog\Application\'+ProviderName, true) then
196
    begin
197
      raise Exception.Create('Cannot register EventLog provider! Please run the application as administrator');
198
    end
199
    else
200
    begin
201
      reg.WriteInteger('CategoryCount', 0);
202
      reg.WriteInteger('TypesSupported', 7);
203
      reg.WriteString('EventMessageFile', MessageFile);
204
      reg.WriteString('CategoryMessageFile', MessageFile);
205
      reg.CloseKey;
206
    end;
207
  finally
208
    reg.Free;
209
  end;
210
end;
211
 
212
procedure TForm1.Button2Click(Sender: TObject);
213
begin
214
  if FileExists(found64) then
215
  begin
216
    RegisterEventLogProvider(Edit3.Text, found64);
217
  end
218
  else if FileExists(found32) then
219
  begin
220
    RegisterEventLogProvider(Edit3.Text, found32);
221
  end
222
  else
223
  begin
224
    raise Exception.Create('Please first register the DLL');
225
  end;
226
 
227
  CheckInstallation;
228
 
229
  Edit3.Text := '';
230
end;
231
 
232
procedure TForm1.Button3Click(Sender: TObject);
233
var
234
  text: string;
235
  reg: TRegistry;
236
begin
237
  if ListBox1.ItemIndex = -1 then exit;
238
  text := ListBox1.Items.Strings[ListBox1.ItemIndex];
239
  text := StringReplace(text, DEFECTIVE_SUFFIX, '', []);
240
 
241
  reg := TRegistry.Create;
242
  try
243
    reg.RootKey := HKEY_LOCAL_MACHINE;
244
    if not reg.DeleteKey('SYSTEM\CurrentControlSet\services\eventlog\Application\' + text) then
245
    begin
246
      raise Exception.Create('Failed to remove item. Are you admin?');
247
    end;
248
  finally
249
    FreeAndNil(reg);
250
  end;
251
 
252
  CheckInstallation;
253
end;
254
 
255
procedure TForm1.CheckInstallation;
256
var
257
  reg: TRegistry;
258
  filename: string;
259
  Wow64FsEnableRedirection: LongBool;
260
  sl: TStrings;
261
  kn: string;
262
  test: string;
263
begin
264
  found32 := '';
265
  found64 := '';
266
 
267
  if TOSVersion.Architecture = TOSVersion.TArchitecture.arIntelX64 then
268
  begin
269
    Wow64DisableWow64FsRedirection(Wow64FsEnableRedirection);
270
  end;
271
  try
272
    {$REGION '32 Bit'}
273
    reg := TRegistry.Create;
274
    try
275
      reg.RootKey := HKEY_CLASSES_ROOT;
276
      if not reg.OpenKeyReadOnly('TypeLib\{D7654BA7-41D0-4FF9-8543-C3A4DA936856}\1.0\0\win32') then
277
      begin
278
        Edit1.Text := 'NOT INSTALLED';
279
        Edit1.Color := clRed;
280
      end
281
      else
282
      begin
283
        filename := reg.ReadString('');
284
        if FileExists(filename) then
285
        begin
286
          Edit1.Text := 'Installed at ' + FileName;
287
          Edit1.Color := clLime;
288
          found32 := FileName;
289
        end
290
        else
291
        begin
292
          Edit1.Text := 'MISSING at location ' + FileName;
293
          Edit1.Color := clRed;
294
        end;
295
        reg.CloseKey;
296
      end;
297
    finally
298
      FreeAndNil(reg);
299
    end;
300
    {$ENDREGION}
301
 
302
    {$REGION '64 Bit'}
303
    if TOSVersion.Architecture = TOSVersion.TArchitecture.arIntelX86 then
304
    begin
305
      Edit2.Text := 'Not applicable on a 32-bit operating system';
306
      Edit2.Color := clLime;
307
    end
308
    else
309
    begin
310
      reg := TRegistry.Create;
311
      try
312
        reg.RootKey := HKEY_CLASSES_ROOT;
313
        if not reg.OpenKeyReadOnly('TypeLib\{D7654BA7-41D0-4FF9-8543-C3A4DA936856}\1.0\0\win64') then
314
        begin
315
          Edit2.Text := 'NOT INSTALLED';
316
          Edit2.Color := clRed;
317
        end
318
        else
319
        begin
320
          filename := reg.ReadString('');
321
          if FileExists(filename) then
322
          begin
323
            Edit2.Text := 'Installed at ' + FileName;
324
            Edit2.Color := clLime;
325
            found64 := FileName;
326
          end
327
          else
328
          begin
329
            Edit2.Text := 'MISSING at location ' + FileName;
330
            Edit2.Color := clRed;
331
          end;
332
          reg.CloseKey;
333
        end;
334
      finally
335
        FreeAndNil(reg);
336
      end;
337
    end;
338
    {$ENDREGION}
339
 
340
  finally
341
    if TOSVersion.Architecture = TOSVersion.TArchitecture.arIntelX64 then
342
    begin
343
      Wow64EnableWow64FsRedirection(Wow64FsEnableRedirection);
344
    end;
345
  end;
346
 
347
  {$REGION 'List providers'}
348
  ListBox1.Clear;
349
  reg := TRegistry.Create;
350
  sl := TStringList.Create;
351
  try
352
    reg.RootKey := HKEY_LOCAL_MACHINE;
353
    if reg.OpenKeyReadOnly('SYSTEM\CurrentControlSet\services\eventlog\Application') then
354
    begin
355
      reg.GetKeyNames(sl);
356
      reg.CloseKey;
357
      for kn in sl do
358
      begin
359
        if reg.OpenKeyReadOnly('SYSTEM\CurrentControlSet\services\eventlog\Application\' + kn) then
360
        begin
361
          test := reg.ReadString('EventMessageFile');
362
          if Pos('VIATHINKSOFTSIMPLELOGEVENT', UpperCase(test)) > 0 then
363
          begin
364
            if not FileExists(test) then
365
              ListBox1.Items.Add(kn + DEFECTIVE_SUFFIX)
366
            else
367
              ListBox1.Items.Add(kn);
368
          end;
369
          reg.CloseKey;
370
        end;
371
      end;
372
    end;
373
  finally
374
    FreeAndNil(reg);
375
    FreeAndNil(sl);
376
  end;
377
  {$ENDREGION}
378
end;
379
 
380
procedure TForm1.FormShow(Sender: TObject);
381
begin
382
  CheckInstallation;
383
end;
384
 
385
end.