Home > Enterprise >  Autorunning Delphi Programm runs in C:\windows\system32
Autorunning Delphi Programm runs in C:\windows\system32

Time:11-05

I've written an application that should be run while starting windows.

I used this code to use a checkbox to decide whether it runs while startup or not:

// d('randomString'); -> this is a function which adds text to a memo for debugging purposes
// GetDosOutput is a function to run cmd commands and get the output of them piped in a memo

function GetRegistryValue(KeyName: string): string;
var
  Registry: TRegistry;
begin
  Registry := TRegistry.Create(KEY_READ);
  try
    Registry.RootKey := HKEY_CURRENT_USER;

    // False weil kein Eintrag erzeugt werden soll, sofern er nicht vorhanden ist.
    Registry.OpenKey(KeyName, False);

    result := Registry.ReadString('SomeRandomAppIWantToRun');
  finally
    Registry.Free;
  end;
end;

procedure TForm1.CheckBox1Click(Sender: TObject);
var
  reg: TRegistry;
begin
  if CheckBox1.Checked = true then
  begin
    with TRegistry.Create do
      try
        RootKey := HKEY_CURRENT_USER;
        OpenKey('\Software\Microsoft\Windows\CurrentVersion\Run', False);
        if ValueExists('SomeRandomAppIWantToRun') then
        begin
          d('Wert existiert');
          if lowercase(Application.ExeName)
           = lowercase
           (GetRegistryValue('\Software\Microsoft\Windows\CurrentVersion\Run'))
          then
          begin
            d('Autostart entry exists and is correct.');
          end
          else
          begin
            d('wrong value exists... will be deleted and recreated!');
            GetDosOutput
             ('reg delete  HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /v SomeRandomAppIWantToRun /f');
            GetDosOutput
             ('REG ADD HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /f /v SomeRandomAppIWantToRun /t REG_SZ /d C:\temp\SomeRandomAppIWantToRun.exe');
          end;
        end
        else
        begin
          d('Autostart entry doesnt exists and will be created now.');
          GetDosOutput
           ('REG ADD HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /f /v SomeRandomAppIWantToRun /t REG_SZ /d C:\temp\SomeRandomAppIWantToRun.exe');
        end;
      except
        showmessage
         (d('Exception in Registry - stuff isnt working'));
      end;
  end
  else
  begin
    with TRegistry.Create do
      try
        RootKey := HKEY_CURRENT_USER;
        OpenKey('\Software\Microsoft\Windows\CurrentVersion\Run', False);
        if ValueExists('SomeRandomAppIWantToRun') then
        begin
          d('Wert existiert');

          if lowercase(Application.ExeName)
           = lowercase
           (GetRegistryValue('\Software\Microsoft\Windows\CurrentVersion\Run'))
          then
          begin
            d('correct Autostart entry will be deleted!');
            GetDosOutput
             ('reg delete  HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /v SomeRandomAppIWantToRun /f');

          end
          else
          begin
            d('wrong startup value... will be deleted and not recreated!');
            GetDosOutput
             ('reg delete  HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /v SomeRandomAppIWantToRun /f');
          end;
        end
        else
        begin
          showmessage('Autostart entry doesnt exist and thats fine.');
        end;
      except
        showmessage
         (d('something didnt work well....'));
      end;
  end;
end;

It successfulls starts while booting up my computer, but.. it seems to be working in C:\windows\system32 and I dont know why.. I placed the application (.exe file) in C:\temp and it is supposed to do things in C:\temp like renaming folders and deleting files and so on, but for this it needs to run in C:\temp so it can easily for example run a batch file located in there which itself needs to think its running in C:\temp.

When I create a shortcut in %appdata%\Microsoft\Windows\Start Menu\Programs\Startup it works fine, but I personally dont want to create a shortcut in some directory, but like to do it in the registry

CodePudding user response:

It successfulls starts while booting up my computer, but.. it seems to be working in C:\windows\system32 and I dont know why..

This is normal behavior. When Windows is running apps from the Run Registry key, they inherit the Shell's working directory, which happens to be the System32 folder.

Your program should not rely on the working directory being any particular value at runtime. If you want to use file paths that are relative to your EXE's current location, then you should retrieve your EXE's full path at runtime using Application.ExeName or ParamStr(0), and then use ExtractFilePath() to strip off the filename. You can then use the resulting string to create paths to other files as needed.

If you absolutely need to rely on the working directory being a particular value when your program is run, then see Use registry to startup a program, and also change the current working directory? for workarounds.

When I create a shortcut in %appdata%\Microsoft\Windows\Start Menu\Programs\Startup it works fine

That is because a Shortcut has its own working directory. By default, it is the same folder as the target of the shortcut. But if you were to go into the properties for that shortcut and set its Start in field to a different folder, you would see your program misbehave the same way as when it is launched from the Run Registry key.

  • Related