home *** CD-ROM | disk | FTP | other *** search
/ PC Administrator / spravce.iso / StartRight / source / UnitStartupRunner.pas < prev   
Encoding:
Pascal/Delphi Source File  |  2002-10-26  |  9.4 KB  |  357 lines

  1. unit UnitStartupRunner;
  2. {
  3.     Purpose:
  4.         To execute all of StartRight's runkey and startup folder
  5.         programs.
  6.  
  7.         Called by FrmDummyRunner 
  8. }
  9. interface
  10.  
  11.  
  12. uses Windows, Registry, SysUtils, StrUtils, TLHelp32,
  13.     INIFiles, Contnrs, ShlObj, ShellAPI,
  14.     Forms {for Application object},
  15.     Classes {for StringList & other TStrings objects},
  16.     Dialogs,
  17.     UnitMyKeys, UnitStartupMover, UnitErrorLog;
  18.  
  19. type TStartupRunner = class(TObject)
  20.     private
  21.         r : TRegistry;
  22.         h : THandle;
  23.  
  24.         MyRunkeyList : TObjectList;
  25.         MyStartupFolderList : TObjectList;
  26.  
  27.         procedure ExecuteRunKeyValue(value : string);
  28.         function RunCommandLine(command : string) : longbool;
  29.         function RunShortcut(command : string) : longbool;
  30.         function RunProgram(name, params : string) : longbool;
  31.  
  32.         procedure GatherMyRunkeyItems;
  33.         procedure GatherMyStartupItems;
  34.     public
  35.         constructor Create(Handle : THandle);
  36.         destructor Destroy(); override;
  37.  
  38.         procedure ExecuteRunkeyPrograms;
  39.         procedure ExecuteStartupFolderPrograms;
  40.  
  41.         function GetMyRunkeyItemsCount : cardinal;
  42.         function GetMyRunkeyItem(index : cardinal) : TRunkeyItem;
  43.         function GetMyStartupFolderItemsCount : cardinal;
  44.         function GetMyStartupFolderItem(index : cardinal) : TStartupFolderItem;
  45.  
  46.         procedure RefreshItems;
  47. end;
  48.  
  49. implementation
  50.  
  51.  
  52.  
  53. constructor TStartupRunner.Create(Handle : THandle);
  54. begin
  55.     r := TRegistry.Create();
  56.     h := Handle;
  57.  
  58.     MyRunkeyList := TObjectList.Create();
  59.     MyStartupFolderList := TObjectList.Create();
  60.  
  61.     self.GatherMyRunkeyItems;
  62.     self.GatherMyStartupItems;
  63. end;
  64.  
  65. destructor TStartupRunner.Destroy();
  66. begin
  67.     MyStartupFolderList.Clear;
  68.     MyRunkeyList.Clear;
  69.     MyStartupFolderList.Free;
  70.     MyRunkeyList.Free;
  71.     r.free;
  72.     inherited destroy;
  73. end;
  74.  
  75.  
  76. procedure TStartupRunner.GatherMyRunkeyItems;
  77. var ri : TRunkeyItem;
  78.     sorted : array of string;
  79.     sortIndex : integer;
  80.     i : integer;
  81.     s : string;
  82. begin
  83.     //
  84.     // Get the names of My RunKey names, in order of execution
  85.     // each key value is named 0 to X, where X is one
  86.     // less than SR_SORTINDEX_VALUE
  87.     // The data is the name of the Run value in SR_RUN_KEY
  88.     //
  89.     r.RootKey := HKEY_LOCAL_MACHINE;
  90.     sortIndex := 0;
  91.     if (r.OpenKey(SR_RUNSORT_KEY, false)) then begin
  92.         sortIndex := r.ReadInteger(SR_SORTINDEX_VALUE);
  93.         SetLength(sorted, sortIndex);
  94.  
  95.         for i := 0 to (sortIndex - 1) do begin
  96.             sorted[i] := r.ReadString(IntToStr(i));
  97.         end;
  98.         r.CloseKey;
  99.     end;
  100.  
  101.     //
  102.     // get all command line's to run
  103.     // using sorted to fetch the data in order
  104.     //
  105.     MyRunkeyList.Clear;
  106.     r.RootKey := HKEY_LOCAL_MACHINE;
  107.     if (r.OpenKey(SR_RUN_KEY, false)) then begin
  108.         for i := 0 to (sortIndex - 1) do begin
  109.             try
  110.                s := r.ReadString(sorted[i]);
  111.                if (s <> '') then begin
  112.                     ri := TRunkeyItem.Create();
  113.  
  114.                     ri.RunKey := sorted[i];
  115.                     ri.RunValue := s;
  116.  
  117.                     MyRunkeyList.Add(ri);
  118.                end;
  119.             finally
  120.             end;
  121.         end;
  122.         r.CloseKey;
  123.     end;
  124.  
  125. end;
  126.  
  127. procedure TStartupRunner.GatherMyStartupItems;
  128. var sfi : TStartupFolderItem;
  129.     i, sortindex : integer;
  130.     sl : THashedStringList;
  131. begin
  132.     MyStartupFolderList.Clear;
  133.  
  134.     r.RootKey := HKEY_LOCAL_MACHINE;
  135.     if (r.OpenKey(SR_STARTUPSORT_KEY, false)) then begin
  136.         sl := THashedStringList.Create();
  137.         r.GetValueNames(sl);
  138.  
  139.         sortIndex := sl.IndexOf(SR_SORTINDEX_VALUE);
  140.         if (sortIndex = -1) then begin
  141.             sortIndex := 0;
  142.         end else begin
  143.             sortIndex := r.ReadInteger(SR_SORTINDEX_VALUE);
  144.         end;
  145.  
  146.         for i := 0 to (sortIndex - 1) do begin
  147.             sfi := TStartupFolderItem.Create();
  148.             sfi.FullName := r.ReadString(IntToStr(i));
  149.             MyStartupFolderList.Add( sfi );
  150.         end;
  151.         sl.Free;
  152.         r.CloseKey;
  153.     end;
  154.  
  155. end;
  156.  
  157.  
  158.  
  159.  
  160.  
  161. function TStartupRunner.GetMyRunkeyItemsCount : cardinal;
  162. begin
  163.     result := MyRunkeyList.Count;
  164. end;
  165. function TStartupRunner.GetMyRunkeyItem(index : cardinal) : TRunkeyItem;
  166. begin
  167.     result := TRunkeyItem( MyRunkeyList.items[index] );
  168. end;
  169.  
  170. function TStartupRunner.GetMyStartupFolderItemsCount : cardinal;
  171. begin
  172.     result := MyStartupFolderList.Count;
  173. end;
  174. function TStartupRunner.GetMyStartupFolderItem(index : cardinal) : TStartupFolderItem;
  175. begin
  176.     result := TStartupFolderItem( MyStartupFolderList.items[index] );
  177. end;
  178.  
  179.  
  180.  
  181.  
  182.  
  183. procedure TStartupRunner.RefreshItems;
  184. begin
  185.     self.GatherMyRunkeyItems;
  186.     self.GatherMyStartupItems;
  187. end;
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194. //------------------------------------------------------------
  195. // Run all startup programs shadowed by this program
  196. //------------------------------------------------------------
  197.  
  198. procedure TStartupRunner.ExecuteRunkeyPrograms;
  199. var i : integer;
  200.     ri : TRunkeyItem;
  201. begin
  202.     //
  203.     // Run all managed runkey items
  204.     //
  205.  
  206.     for i := 0 to MyRunkeyList.Count - 1 do begin
  207.         ri := TRunkeyItem( MyRunkeyList.Items[i] );
  208.  
  209.         self.ExecuteRunKeyValue( ri.RunValue );
  210.     end;
  211. end;
  212.  
  213. procedure TStartupRunner.ExecuteStartupFolderPrograms;
  214. var i : integer;
  215.     sfi : TStartupFolderItem;
  216. begin
  217.     for i := 0 to MyStartupFolderList.Count - 1 do begin
  218.         sfi := TStartupFolderItem( MyStartupFolderList.Items[ i ] );
  219.  
  220.         if (not self.RunShortcut( sfi.FullName )) then begin
  221.             ShowMessage(SysErrorMessage(GetLastError));
  222.         end;
  223.     end;
  224. end;
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231. procedure TStartupRunner.ExecuteRunKeyValue(value : string);
  232. var s, name, params : string;
  233. begin
  234.     //
  235.     // remove quotes around program names
  236.     //
  237.     {if (LeftStr(value,1) = '"') then begin
  238.         value := StringReplace(value, '"', '', []);
  239.         value := StringReplace(value, '"', '', []);
  240.     end;
  241.     }
  242.     //
  243.     // turn full names into short names and break apart the
  244.     // program and it's parameters
  245.     // Otherwise, treat it as a command line to be run
  246.     // (like mobsync.exe & point32.exe
  247.     //
  248.     if (pos(':', value) <> 0) then begin
  249.          if (LeftStr(value,1) = '"') then begin
  250.             value := StringReplace(value, '"', '', []);
  251.             value := StringReplace(value, '"', '', []);
  252.         end;
  253.  
  254.         name := UnitMyKeys.GetEXEPathFromRunValue(value);
  255.         params := StringReplace(value, name, '',[rfIgnoreCase]);
  256.  
  257.         if (self.RunCommandLine('"' +  name + '" ' + Trim(params))) then begin
  258.             EXIT;
  259.         end;
  260.     end else begin
  261.         if (self.RunCommandLine(value)) then begin
  262.             EXIT;
  263.         end;
  264.     end;
  265.  
  266.  
  267.     //
  268.     // If it's not a full path, and not is the system path
  269.     // the program's path may be located in "App Paths";
  270.     //
  271.     {cannot find path}
  272.  
  273.     r.RootKey := HKEY_LOCAL_MACHINE;
  274.     if r.OpenKey(WINDOWS_APPSPATH_KEY + '\' + value, false) then begin
  275.         try
  276.             s := r.ReadString('');
  277.         finally
  278.         end;
  279.         r.CloseKey;
  280.  
  281.         if (s <> '') then begin
  282.             if (not self.RunCommandLine(s)) then begin
  283.                 // I am most unhappy if this part is reached
  284.                 ShowMessage('damn you');
  285.             end;
  286.         end;
  287.     end;
  288.  
  289. end;
  290.  
  291. function TStartupRunner.RunCommandLine(command : string) : longbool;
  292. var  StartInfo  : _StartupInfoA;
  293.      ProcInfo   : _PROCESS_INFORMATION;
  294. begin
  295.     // nil for the program name treats the program name
  296.     // as a command line to execute
  297.     FillChar(StartInfo, SizeOf(StartInfo), #0);
  298.     FillChar(ProcInfo, SizeOf(ProcInfo), #0);
  299.     StartInfo.cb := SizeOf(TStartupInfo);
  300.  
  301.     result := CreateProcess(nil, PChar(command),
  302.                 nil, nil, False,
  303.                 CREATE_NEW_PROCESS_GROUP + NORMAL_PRIORITY_CLASS,
  304.                 nil, nil, StartInfo, ProcInfo);
  305.  
  306.     if (result) then begin
  307.         WaitForInputIdle(ProcInfo.hProcess, 5000);
  308.     end else begin
  309.         ErrorLog.Add(Windows.GetLastError(), command);
  310.     end;
  311. end;
  312.  
  313. function TStartupRunner.RunProgram(name, params : string) : longbool;
  314. var  StartInfo  : _StartupInfoA;
  315.      ProcInfo   : _PROCESS_INFORMATION;
  316. begin
  317.     // nil for the program name treats the program name
  318.     // as a command line to execute
  319.     FillChar(StartInfo, SizeOf(StartInfo), #0);
  320.     FillChar(ProcInfo, SizeOf(ProcInfo), #0);
  321.     StartInfo.cb := SizeOf(TStartupInfo);
  322.  
  323.     result := CreateProcess(pchar(name), PChar(params),
  324.                 nil, nil, False,
  325.                 CREATE_NEW_PROCESS_GROUP + NORMAL_PRIORITY_CLASS,
  326.                 nil, nil, StartInfo, ProcInfo);
  327.  
  328.     if (result) then begin
  329.         WaitForInputIdle(ProcInfo.hProcess, 5000);
  330.     end else begin
  331.         ErrorLog.Add(Windows.GetLastError(), name + ' ' + params);
  332.     end;
  333. end;
  334.  
  335.  
  336. function TStartupRunner.RunShortcut(command : string) : longbool;
  337. var c : cardinal;
  338.     {StartInfo  : _StartupInfoA;
  339.     ProcInfo   : _PROCESS_INFORMATION;}
  340. begin
  341.     command := ExtractShortPathName(command);
  342.     c := ShellAPI.ShellExecute(h, nil, PChar( ExtractShortPathName(command) ), nil, nil, SW_SHOWNORMAL);
  343.  
  344.     result := (c > 32);
  345.     if (result) then begin
  346.         Sleep(2000);
  347.     end else begin
  348.         ErrorLog.Add(Windows.GetLastError(), command);
  349.     end;
  350. end;
  351.  
  352.  
  353.  
  354.  
  355.  
  356. end.
  357.