0

I need to set the python path during an inno setup install script.

As far as I know, setting it from the command line is done by typing:

set path= C:\pythonTargetDir;%path%

In my inno setup, at the [run] section I tried to add the line:

Filename: "set"; Parameters:  " ""path ={#pythonTargetDir}"" TARGETDIR=""{#pythonTargetDir}"" ALLUSERS=1 "; StatusMsg: "Adding Python to system path..."

Running the script ended out in the fallowing error message box:

Unable to execute file: Set

CreateProcess Failed; code 2; The system cannot find the file specified

I guess I could write a batch file that will do the set command and I will order inno to call it from the run section, and that would do the trick. But even if it will work I don't really wanna to go that way. Seems not very elegant solution. Is there another way?

The [ini] and [Registry] sections dosn't seems to be right for this purpose either.

Ron Zukerman
  • 61
  • 1
  • 8
  • SET is not an executable program, it is a command that the command processor understands. Cmd.exe. Using it in an installer is always wrong. – Hans Passant Jun 29 '14 at 15:51
  • 1
    possible duplicate of [How do I modify the PATH environment variable when running an Inno Setup Installer?](http://stackoverflow.com/questions/3304463/how-do-i-modify-the-path-environment-variable-when-running-an-inno-setup-install) – Hans Passant Jun 29 '14 at 15:51

1 Answers1

1

I have few functions in pascal. Look at the code and deside if they fits your needs. I made it long time ago and Im not shure wich version is better. How do I modify the PATH environment variable when running an Inno Setup Installer? is fine solution to.

Get path in array:

function GetEnvStrings(VarName:string;AllUsers:Boolean):TArrayOfString;
var
    Path:string;
    i:Longint;
    p:Integer;
begin
    Path:='';
    // See http://www.jrsoftware.org/isfaq.php#env
    if AllUsers then 
    begin
      RegQueryStringValue(HKEY_LOCAL_MACHINE,'SYSTEM\CurrentControlSet\Control\Session Manager\Environment',VarName,Path);
    end 
    else 
    begin
      RegQueryStringValue(HKEY_CURRENT_USER,'Environment',VarName,Path);
    end;
    Path:=Path+';';

    i:=0;
    SetArrayLength(Result,0);

    p:=Pos(';',Path);
    while p>0 do 
    begin
      SetArrayLength(Result,i+1);
      if p>1 then 
      begin
        Result[i]:=Copy(Path,1,p-1);
        i:=i+1;
      end;
      Path:=Copy(Path,p+1,Length(Path));
      p:=Pos(';',Path);
    end;
end;

Check if you already have dir in path:

function NeedsAddPath(Param: string): boolean;
var
  OrigPath: string;
  ok: Boolean;
begin
  ok:= False;

  if not RegQueryStringValue(HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 'Path', OrigPath) then 
  begin
    ok:= True;
    exit;
  end;

  ok:= Pos(';' + Param + ';', ';' + OrigPath + ';') = 0;

  Result:= ok;
end;

Set path 1:

function SetEnvStrings(VarName:string; AllUsers,DeleteIfEmpty: Boolean; DirStrings:TArrayOfString):Boolean;
var
  Path,KeyName:string;
  i:Longint;
begin
  Path:='';
  for i:=0 to GetArrayLength(DirStrings)-1 do 
  begin
    if Length(DirStrings[i])>0 then 
    begin
      if Length(Path)>0 then 
      begin
        Path:=Path+';'+DirStrings[i];
      end 
      else 
      begin
        Path:=DirStrings[i];
      end;
    end;
  end;
  Log(Path);
  if AllUsers then
  begin
    Log('All');
    KeyName:='SYSTEM\CurrentControlSet\Control\Session Manager\Environment';
    if DeleteIfEmpty and (Length(Path)=0) then 
    begin
      Result:=(not RegValueExists(HKEY_LOCAL_MACHINE,KeyName,VarName))
                or RegDeleteValue(HKEY_LOCAL_MACHINE,KeyName,VarName);
    end 
    else 
    begin
      Log(VarName + ' All');
      RegWriteStringValue(HKEY_LOCAL_MACHINE,KeyName,VarName,Path);
      KeyName:='SYSTEM\ControlSet2002\Control\Session Manager\Environment';
      RegWriteStringValue(HKEY_LOCAL_MACHINE,KeyName,VarName,Path);
    end;
  end 
  else 
  begin
    KeyName:='Environment';
    Log(KeyName);
    if DeleteIfEmpty and (Length(Path)=0) then 
    begin
      Result:=(not RegValueExists(HKEY_CURRENT_USER,KeyName,VarName))
                or RegDeleteValue(HKEY_CURRENT_USER,KeyName,VarName);
    end 
    else 
    begin
      Log(VarName + ' Environment');
      Result:=RegWriteStringValue(HKEY_CURRENT_USER,KeyName,VarName,Path);
    end;
  end;
end;

Set path 2:

procedure SetPath(pEnvPath: String);
var 
  Msg :String;
  EnvPath: TArrayOfString;
  i: integer;
begin
  EnvPath:=GetEnvStrings('PATH', IsAdminLoggedOn);
  if NeedsAddPath(pEnvPath) then 
  begin
    i:=GetArrayLength(EnvPath);
    SetArrayLength(EnvPath,i+1);
    log(pEnvPath);
    EnvPath[i]:= pEnvPath;          
  end;

  if not SetEnvStrings('PATH', True, True, EnvPath) then 
  begin
    Msg:='Line {#__LINE__}: Unable to set the PATH environment variable.';
    Log(Msg);
  end; 
  if not SetEnvStrings('PATH', False, True, EnvPath) then 
  begin
    Msg:='Line {#__LINE__}: Unable to set the PATH environment variable.';
    Log(Msg);
  end;
end;
Community
  • 1
  • 1
user1969258
  • 99
  • 2
  • 7