首页 > 解决方案 > 自动运行 Delphi 程序在 C:\windows\system32 中运行

问题描述

我编写了一个应在启动 Windows 时运行的应用程序。

我使用此代码使用复选框来决定它是否在启动时运行:

// 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;

它在启动我的计算机时成功启动,但是..它似乎在 C:\windows\system32 中工作,我不知道为什么..我将应用程序(.exe 文件)放在 C:\temp 中,它应该在 C:\temp 中执行操作,例如重命名文件夹和删除文件等,但为此它需要在 C:\temp 中运行,因此它可以轻松地运行位于其中的批处理文件,该批处理文件本身需要考虑在其中运行C:\温度。

当我在 %appdata%\Microsoft\Windows\Start Menu\Programs\Startup 中创建快捷方式时,它工作正常,但我个人不想在某个目录中创建快捷方式,而是喜欢在注册表中进行

标签: windowsdelphivclautostartdelphi-10.4-sydney

解决方案


它在启动我的计算机时成功启动,但是..它似乎在 C:\windows\system32 中工作,我不知道为什么..

这是正常行为。当 Windows 从Run注册表项运行应用程序时,它们会继承 Shell 的工作目录,恰好是 System32 文件夹。

您的程序不应依赖工作目录在运行时是任何特定值。如果您想使用相对于您的 EXE 当前位置的文件路径,那么您应该在运行时使用Application.ExeNameor检索您的 EXE 的完整路径ParamStr(0),然后使用ExtractFilePath()去除文件名。然后,您可以根据需要使用生成的字符串创建指向其他文件的路径。

如果您在程序运行时绝对需要依赖工作目录作为特定值,请参阅使用注册表启动程序,并更改当前工作目录?解决方法。

当我在 %appdata%\Microsoft\Windows\Start Menu\Programs\Startup 创建快捷方式时,它工作正常

那是因为快捷方式有自己的工作目录。默认情况下,它与快捷方式的目标相同。但是,如果您要进入该快捷方式的属性并将其字段设置为不同的文件夹,您会看到您的程序与从注册表项Start in启动时的行为方式相同。Run


推荐阅读