Subversion Repositories simple_log_event

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  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.  
  60. procedure RegSvr32(const dll: string);
  61. begin
  62.   ShellExecute(Form1.Handle, 'open', 'regsvr32.exe', PChar('"' + dll + '"'), '', SW_NORMAL);
  63. end;
  64.  
  65. procedure TForm1.Button1Click(Sender: TObject);
  66. var
  67.   rs: TResourceStream;
  68.   Wow64FsEnableRedirection: LongBool;
  69.   reg: TRegistry;
  70.   sl: TStringList;
  71.   kn: string;
  72.   test: string;
  73.   lastregfile: string;
  74. begin
  75.   if not IsUserAnAdmin  then
  76.   begin
  77.     raise Exception.Create('To register the libraries, this application needs to run as administrator.');
  78.   end;
  79.  
  80.   try
  81.     {$REGION 'Copy DLL to common files'}
  82.  
  83.     if TOSVersion.Architecture = TOSVersion.TArchitecture.arIntelX86 then
  84.     begin
  85.       {$REGION '32 Bit Windows'}
  86.       lastregfile := 'C:\Program Files\Common Files\ViaThinkSoft\ViaThinkSoftSimpleLogEvent32.dll';
  87.       ForceDirectories(ExtractFilePath(lastregfile));
  88.       rs := TResourceStream.CreateFromID(HInstance, 32, PChar('DLL'));
  89.       rs.SaveToFile(lastregfile);
  90.       rs.Free;
  91.       RegSvr32(lastregfile);
  92.       {$ENDREGION}
  93.     end;
  94.  
  95.     if TOSVersion.Architecture = TOSVersion.TArchitecture.arIntelX64 then
  96.     begin
  97.       {$REGION '64 Bit Windows'}
  98.       Wow64DisableWow64FsRedirection(Wow64FsEnableRedirection);
  99.       try
  100.         lastregfile := 'C:\Program Files (x86)\Common Files\ViaThinkSoft\ViaThinkSoftSimpleLogEvent32.dll';
  101.         ForceDirectories(ExtractFilePath(lastregfile));
  102.         rs := TResourceStream.CreateFromID(HInstance, 32, PChar('DLL'));
  103.         rs.SaveToFile(lastregfile);
  104.         rs.Free;
  105.         RegSvr32(lastregfile);
  106.  
  107.         lastregfile := 'C:\Program Files\Common Files\ViaThinkSoft\ViaThinkSoftSimpleLogEvent64.dll';
  108.         ForceDirectories(ExtractFilePath(lastregfile));
  109.         rs := TResourceStream.CreateFromID(HInstance, 64, PChar('DLL'));
  110.         rs.SaveToFile(lastregfile);
  111.         rs.Free;
  112.         RegSvr32(lastregfile);
  113.       finally
  114.         Wow64EnableWow64FsRedirection(Wow64FsEnableRedirection);
  115.       end;
  116.       {$ENDREGION}
  117.     end;
  118.  
  119.     {$ENDREGION}
  120.  
  121.     {$REGION 'Update DLL path in log provider list'}
  122.     reg := TRegistry.Create;
  123.     sl := TStringList.Create;
  124.     try
  125.       reg.RootKey := HKEY_LOCAL_MACHINE;
  126.       if reg.OpenKey('SYSTEM\CurrentControlSet\services\eventlog\Application', false) then
  127.       begin
  128.         reg.GetKeyNames(sl);
  129.         reg.CloseKey;
  130.         for kn in sl do
  131.         begin
  132.           if reg.OpenKey('SYSTEM\CurrentControlSet\services\eventlog\Application\' + kn, false) then
  133.           begin
  134.             test := reg.ReadString('EventMessageFile');
  135.             if Pos('VIATHINKSOFTSIMPLELOGEVENT', UpperCase(test)) > 0 then
  136.             begin
  137.               if test <> lastregfile then
  138.               begin
  139.                 reg.WriteString('EventMessageFile', lastregfile);
  140.               end;
  141.             end;
  142.             reg.CloseKey;
  143.           end;
  144.         end;
  145.       end;
  146.     finally
  147.       FreeAndNil(reg);
  148.       FreeAndNil(sl);
  149.     end;
  150.     {$ENDREGION}
  151.  
  152.   finally
  153.     CheckInstallation;
  154.   end;
  155. end;
  156.  
  157. const
  158.   DEFECTIVE_SUFFIX = ' (defective)';
  159.  
  160. procedure RegisterEventLogProvider(ProviderName, MessageFile: string);
  161. var
  162.   reg: TRegistry;
  163. begin
  164.   reg := TRegistry.Create;
  165.   try
  166.     reg.RootKey := HKEY_LOCAL_MACHINE;
  167.     if not reg.OpenKey('SYSTEM\CurrentControlSet\Services\Eventlog\Application\'+ProviderName, true) then
  168.     begin
  169.       raise Exception.Create('Cannot register EventLog provider! Please run the application as administrator');
  170.     end
  171.     else
  172.     begin
  173.       reg.WriteInteger('CategoryCount', 0);
  174.       reg.WriteInteger('TypesSupported', 7);
  175.       reg.WriteString('EventMessageFile', MessageFile);
  176.       reg.WriteString('CategoryMessageFile', MessageFile);
  177.       reg.CloseKey;
  178.     end;
  179.   finally
  180.     reg.Free;
  181.   end;
  182. end;
  183.  
  184. procedure TForm1.Button2Click(Sender: TObject);
  185. begin
  186.   if FileExists(found64) then
  187.   begin
  188.     RegisterEventLogProvider(Edit3.Text, found64);
  189.   end
  190.   else if FileExists(found32) then
  191.   begin
  192.     RegisterEventLogProvider(Edit3.Text, found32);
  193.   end
  194.   else
  195.   begin
  196.     raise Exception.Create('Please first register the DLL');
  197.   end;
  198.  
  199.   CheckInstallation;
  200.  
  201.   Edit3.Text := '';
  202. end;
  203.  
  204. procedure TForm1.Button3Click(Sender: TObject);
  205. var
  206.   text: string;
  207.   reg: TRegistry;
  208. begin
  209.   if ListBox1.ItemIndex = -1 then exit;
  210.   text := ListBox1.Items.Strings[ListBox1.ItemIndex];
  211.   text := StringReplace(text, DEFECTIVE_SUFFIX, '', []);
  212.  
  213.   reg := TRegistry.Create;
  214.   try
  215.     reg.RootKey := HKEY_LOCAL_MACHINE;
  216.     if not reg.DeleteKey('SYSTEM\CurrentControlSet\services\eventlog\Application\' + text) then
  217.     begin
  218.       raise Exception.Create('Failed to remove item. Are you admin?');
  219.     end;
  220.   finally
  221.     FreeAndNil(reg);
  222.   end;
  223.  
  224.   CheckInstallation;
  225. end;
  226.  
  227. procedure TForm1.CheckInstallation;
  228. var
  229.   reg: TRegistry;
  230.   filename: string;
  231.   Wow64FsEnableRedirection: LongBool;
  232.   sl: TStrings;
  233.   kn: string;
  234.   test: string;
  235. begin
  236.   found32 := '';
  237.   found64 := '';
  238.  
  239.   if TOSVersion.Architecture = TOSVersion.TArchitecture.arIntelX64 then
  240.   begin
  241.     Wow64DisableWow64FsRedirection(Wow64FsEnableRedirection);
  242.   end;
  243.   try
  244.     {$REGION '32 Bit'}
  245.     reg := TRegistry.Create;
  246.     try
  247.       reg.RootKey := HKEY_CLASSES_ROOT;
  248.       if not reg.OpenKeyReadOnly('TypeLib\{D7654BA7-41D0-4FF9-8543-C3A4DA936856}\1.0\0\win32') then
  249.       begin
  250.         Edit1.Text := 'NOT INSTALLED';
  251.         Edit1.Color := clRed;
  252.       end
  253.       else
  254.       begin
  255.         filename := reg.ReadString('');
  256.         if FileExists(filename) then
  257.         begin
  258.           Edit1.Text := 'Installed at ' + FileName;
  259.           Edit1.Color := clLime;
  260.           found32 := FileName;
  261.         end
  262.         else
  263.         begin
  264.           Edit1.Text := 'MISSING at location ' + FileName;
  265.           Edit1.Color := clRed;
  266.         end;
  267.         reg.CloseKey;
  268.       end;
  269.     finally
  270.       FreeAndNil(reg);
  271.     end;
  272.     {$ENDREGION}
  273.  
  274.     {$REGION '64 Bit'}
  275.     if TOSVersion.Architecture = TOSVersion.TArchitecture.arIntelX86 then
  276.     begin
  277.       Edit2.Text := 'Not applicable on a 32-bit operating system';
  278.       Edit2.Color := clLime;
  279.     end
  280.     else
  281.     begin
  282.       reg := TRegistry.Create;
  283.       try
  284.         reg.RootKey := HKEY_CLASSES_ROOT;
  285.         if not reg.OpenKeyReadOnly('TypeLib\{D7654BA7-41D0-4FF9-8543-C3A4DA936856}\1.0\0\win64') then
  286.         begin
  287.           Edit2.Text := 'NOT INSTALLED';
  288.           Edit2.Color := clRed;
  289.         end
  290.         else
  291.         begin
  292.           filename := reg.ReadString('');
  293.           if FileExists(filename) then
  294.           begin
  295.             Edit2.Text := 'Installed at ' + FileName;
  296.             Edit2.Color := clLime;
  297.             found64 := FileName;
  298.           end
  299.           else
  300.           begin
  301.             Edit2.Text := 'MISSING at location ' + FileName;
  302.             Edit2.Color := clRed;
  303.           end;
  304.           reg.CloseKey;
  305.         end;
  306.       finally
  307.         FreeAndNil(reg);
  308.       end;
  309.     end;
  310.     {$ENDREGION}
  311.  
  312.   finally
  313.     if TOSVersion.Architecture = TOSVersion.TArchitecture.arIntelX64 then
  314.     begin
  315.       Wow64EnableWow64FsRedirection(Wow64FsEnableRedirection);
  316.     end;
  317.   end;
  318.  
  319.   {$REGION 'List providers'}
  320.   ListBox1.Clear;
  321.   reg := TRegistry.Create;
  322.   sl := TStringList.Create;
  323.   try
  324.     reg.RootKey := HKEY_LOCAL_MACHINE;
  325.     if reg.OpenKeyReadOnly('SYSTEM\CurrentControlSet\services\eventlog\Application') then
  326.     begin
  327.       reg.GetKeyNames(sl);
  328.       reg.CloseKey;
  329.       for kn in sl do
  330.       begin
  331.         if reg.OpenKeyReadOnly('SYSTEM\CurrentControlSet\services\eventlog\Application\' + kn) then
  332.         begin
  333.           test := reg.ReadString('EventMessageFile');
  334.           if Pos('VIATHINKSOFTSIMPLELOGEVENT', UpperCase(test)) > 0 then
  335.           begin
  336.             if not FileExists(test) then
  337.               ListBox1.Items.Add(kn + DEFECTIVE_SUFFIX)
  338.             else
  339.               ListBox1.Items.Add(kn);
  340.           end;
  341.           reg.CloseKey;
  342.         end;
  343.       end;
  344.     end;
  345.   finally
  346.     FreeAndNil(reg);
  347.     FreeAndNil(sl);
  348.   end;
  349.   {$ENDREGION}
  350. end;
  351.  
  352. procedure TForm1.FormShow(Sender: TObject);
  353. begin
  354.   CheckInstallation;
  355. end;
  356.  
  357. end.
  358.