如何最佳地将组件库路径修改/安装到Delphi IDE中而不必手动完成?

3
我正在准备一个安装程序(Inno Setup),以便将我的组件包安装到Delphi XE中,而无需手动在IDE中操作。
我需要修改Delphi库路径,例如删除其中的一部分(例如xxx;MyOldPath;yyy),并插入新路径xxxx;MyNewPath;yyyy。有没有更好的方法来做这个?还是我必须编写一个实用程序来完成它?
谢谢

库路径是注册表中的一个子键;请注意可能存在多个注册表键(使用传递给bds可执行文件的-r命令行开关)。安装程序应该枚举所有现有的注册表键,并提供一个复选框或类似的界面,以便用户可以选择要修改哪些注册表键。 - Ondrej Kelle
如果你正在做我认为你在做的事情(使用innosetup制作组件安装程序),这可能是值得写一篇小文章并发布到你的博客上的东西。 - Warren P
1个回答

5
修改路径是基本的字符串操作:您可以从注册表中读取当前路径,对其进行操作以满足您的需求,然后写回去。
您可能可以编写一个Inno Setup脚本函数,这样您就没有任何外部依赖项。或者编写一个Delphi DLL,用于从Inno Setup的脚本中使用,这样更容易调试。

编辑

这是我实际在生产中使用的例程的修改版本。它将从“搜索路径”注册表值或“浏览路径”(任何其他路径)读取整个路径列表,可能会删除一些路径,并添加一些路径(如果它们不存在)。
procedure UpdateDelphiPaths(const RegistryKey, RegistryValue: string; PathsToRemove, PathsToAdd: TStrings);
var R:TRegistry;
      SKeys:TStringList;
      Found:Boolean;
      Updated:Boolean;
      i,j:Integer;
      s:string;
      R_Globals:TRegistry;

  // This function normalises paths in comparasions
  function PrepPathForComparasion(const Path:string):string;
  begin
    if Path = '' then Result := '\'
    else
      if Path[Length(Path)] = '\' then
        Result := LowerCase(Path)
      else
        Result := LowerCase(Path) + '\';
  end;

  function PathMatchesRemoveCriteria(const Path:string): Boolean;
  var i:Integer;
  begin
    // This needs to be addapted to match your criteria!
    for i:=0 to PathsToRemove.Count-1 do
      if AnsiPos(PathsToRemove[i], Path) <> 0 then
        Exit(True);
    Result := False;
  end;

begin
  R := TRegistry.Create;
  try
    R.RootKey := HKEY_CURRENT_USER;
    if R.OpenKey(RegistryKey + '\Library', False) then
      if R.ValueExists(RegistryValue) then
      begin
        SKeys := TStringList.Create;
        try
          SKeys.Delimiter := ';';
          SKeys.StrictDelimiter := True;
          SKeys.DelimitedText := R.ReadString(RegistryValue);

          Updated := False;

          // Look at all the paths in the PathsToAdd list, if any one's missing add it to the list and mark
          // "Updated".
          for i:=0 to PathsToAdd.Count-1 do
          begin
            Found := False;
            for j:=0 to SKeys.Count-1 do
              if LowerCase(Trim(SKeys[j])) = LowerCase(Trim(PathsToAdd[i])) then
                Found := True;
            if not Found then
            begin
              SKeys.Add(PathsToAdd[i]);
              Updated := True;
            end;
          end;

          // Look at every single path in the current list, if it's not in the "PathsToAdd" and it matches
          // a name in "PathsToRemove", drop it and mark "Updated"
          i := 0;
          while i < SKeys.Count do
          begin
            if PathMatchesRemoveCriteria(SKeys[i]) then
              begin
                // Path matches remove criteria! It only gets removed if it's not actually present in
                // PathsToAdd
                Found := False;
                for j:=0 to PathsToAdd.Count-1 do
                begin
                  if PrepPathForComparasion(SKeys[i]) = PrepPathForComparasion(PathsToAdd[j]) then
                    Found := True;
                end;
                if not Found then
                  begin
                    SKeys.Delete(i);
                    Updated := True;
                  end
                else
                  Inc(i);
              end
            else
              Inc(i);
          end;

          // If I've updated the SKeys in any way, push changes back to registry and force updates
          if Updated then
          begin
            s := SKeys[0];
            for i:=1 to SKeys.Count-1 do
              if SKeys[i] <> '' then
              begin
                s := s + ';' + SKeys[i];
              end;
            R.WriteString(RegistryValue, s);

            // Force delphi to re-load it's paths.
            R_Globals := TRegistry.Create;
            try
              R_Globals.OpenKey(RegistryKey + '\Globals', True);
              R_Globals.WriteString('ForceEnvOptionsUpdate', '1');
            finally R_Globals.Free;
            end;

          end;

        finally SKeys.Free;
        end;
      end;
  finally R.Free;
  end;
end;

我可以使用Delphi代码调用这个例程,以确保给定库的最新搜索路径已安装:

var ToRemove, ToAdd: TStringList;
begin
  ToRemove := TStringList.Create;
  try
    ToAdd := TStringList.Create;
    try
      ToRemove.Add('LibraryName\Source');
      ToAdd.Add('C:\LibraryName\Source');
      UpdateDelphiPaths('Software\CodeGear\BDS\7.0', 'Test Path', ToRemove, ToAdd);
    finally ToAdd.Free;
    end;
  finally ToRemove.Free;
  end;
end;

注意要删除和添加的ToRemoveToAdd。我可以安全地在删除和添加列表中指定搜索路径:只有当路径符合“Remove”标准但不在“ToAdd”列表中时,才会删除该路径。还请注意PathMatchesRemoveCriteria函数。
您可能可以修改代码,使其直接从InnoScript中工作,或者将代码放入DLL并从安装程序中使用该DLL。 DLL变体具有在Delphi中轻松调试并对Inno自身非常友好的优点; Inno变体则具有没有外部依赖项的优点,但需要适应和调试代码。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接