--> Add and Remove Startup Dephi | Delphi Tips Trick

For Newbie

Tuesday, 15 November 2011

Add and Remove Startup Dephi

| Tuesday, 15 November 2011
Technique how to put our application and other applications on the Windows start-up in the registry and startup folder directory.
Function declaration:uses
  
Registry, ActiveX, ShlObj, ComObj;
const
  
CURRENT_USER = 1;
  
ALL_USERS = 2;
  
keyReg = 'Software \ Microsoft \ Windows \ CurrentVersion \ Run';
  
ShellFolder = 'Software \ Microsoft \ Windows \ CurrentVersion \ Explorer \ Shell Folders';
  
strPath = "\ Start Menu \ Programs \ Startup \ ';
/ / Function to read environment variables to WindowsEnvVar function (const v: string): string;var
  
c: array [0 .. 1023] of Char;begin
  
if ExpandEnvironmentStrings (PChar (v), c, 1024) = 0 then
  
Result: = v
  
else Result: = Trim (c);end;
/ / Function to create a ShortcutCreateShortCut function (const FromFile: String; dest: widestring): Boolean;var
  
MOBJAX: IUnknown;
  
MSHLink: IShellLink;
  
MPRFile: IPersistFile;
  
HRes: HRESULT;begin
  
MOBJAX: = CreateCOMObject (CLSID_ShellLink);
  
MSHLink: = MOBJAX as IShellLink;
  
MPRFile: = MOBJAX as IPersistFile;
  
with MSHLink do
  
begin
    
SetPath (PChar (FromFile));
    
SetRelativePath (PChar (ExtractFilePath (FromFile)), 0);
    
SetWorkingDirectory (PChar (ExtractFilePath (FromFile)));
    
SetIconLocation (PChar (FromFile), 0);
  
end;
  
HRes: = MPRFile.Save (PWChar (dest), False);
  
if HRes = S_OK then Result: = True else Result: = False;end;
/ / Function to read the location of the startup directory (slightly forcing functions)StartupFolderDir function (const tStartup: Byte): String;var
  
tmpKey, tmpEnv, tmpPath: String;
  
tmpRoot: HKEY;begin
  
Result: ='';
  
if tStartup = CURRENT_USER then
  
begin
    
tmpKey: = 'startup';
    
tmpRoot: = HKEY_CURRENT_USER;
    
tmpEnv: = '% UserProfile%';
  
end
  
else
  
begin
    
tmpKey: = 'Common Startup';
    
tmpRoot: = HKEY_LOCAL_MACHINE;
    
tmpEnv: = '% AllUsersProfile%';
  
end;
  
with TRegistry.Create (KEY_READ) do
  
begin
    
try
      
RootKey: = tmpRoot;
      
if OpenKey (ShellFolder, False) then
      
begin
        
if ValueExists (tmpKey) then tmpPath: = ReadString (tmpKey);
      
end;
    
finally
      
Free;
    
end;
  
end;
  
if Trim (tmpPath) =''then tmpPath: = EnvVar (tmpEnv) + strPath;
  
Result: = IncludeTrailingBackSlash (tmpPath);end;
ToStartupRegistry procedure (const IDApp, exefile: String; tStartup: Byte);var
  
rk: HKEY;begin
  
with TRegistry.Create do
  
begin
    
if tStartup = CURRENT_USER then
      
rk: = HKEY_CURRENT_USER
    
else
      
rk: = HKEY_LOCAL_MACHINE;
    
try
      
RootKey: = rk;
      
OpenKey (keyReg, True);
      
WriteString (IDApp, exefile);
    
finally
      
Free;
    
end;
  
end;end;
RemStartupRegistry procedure (const IDApp: String; tStartup: Byte);var
  
rk: HKEY;begin
  
with TRegistry.Create do
  
begin
    
if tStartup = CURRENT_USER then
      
rk: = HKEY_CURRENT_USER
    
else
      
rk: = HKEY_LOCAL_MACHINE;
    
try
      
RootKey: = rk;
      
if OpenKey (keyReg, False) then
        
if ValueExists (IDApp) then DeleteValue (IDApp);
    
finally
      
Free;
    
end;
  
end;end;
ToStartupFolder procedure (const IDApp, exefile: String; tStartup: Byte);begin
  
CreateShortcut (exefile, StartupFolderDir (tStartup) + + IDApp. 'Lnk');end;
RemStartupFolder procedure (const IDApp: String; tStartup: Byte);var
  
F: String;begin
  
F: = StartupFolderDir (tStartup) + + IDApp '. Lnk';
  
if FileExists (F) then DeleteFile (F);end;
If you want to access start-ups in the current user, fill in the parameters and ALL_USERS CURRENT_USER tStartup with start-ups for all users.
Example of implementation:/ / To Startup in the registry/ / Add to ...ToStartupRegistry ('myapp', Application.ExeName, ALL_USERS);/ / RemoveRemStartupRegistry ('myapp', ALL_USERS);
/ / For the Startup folder on Startup/ / Add to ...ToStartupFolder ('myapp', Application.ExeName, ALL_USERS);/ / RemoveRemStartupFolder ('myapp', ALL_USERS);

Related Posts

No comments: