0

ChangesEnvironment in Inno Setup Installer issue.

ChangesEnvironment=yes is the known parameter but it doesn't refresh before installer close.

I am modifying PATH environment variable using Inno-Setup. So as installation completes, default exe starts but throws "dll not found" error. Because of Inno-Setup still not closed and not sending Windows to refresh path environments. I want to make Windows-OS rechecks/updates path environments before setup closed.

So as a temporal solution; I disabled auto-run the default exe and app needs manual open from Desktop icon.

Is there a way to send a message API to windows using Inno-Setup, immediately before Inno-Setup closed ? (Hey Windows! recheck path environments now before get closed)

Zen Of Kursat
  • 2,672
  • 1
  • 31
  • 47
  • 1
    See [Environment variable not recognized \[not available\] for \[Run\] programs in Inno Setup](https://stackoverflow.com/q/21708140/850848). – Martin Prikryl Jun 08 '20 at 07:15

1 Answers1

0

With the help of Martin, after modifications below, worked perfectly:

[Run]
Filename: "{app}\{#MyAppExeName}"; BeforeInstall: AppendToPathAndRefresh;Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}";Flags: nowait postinstall shellexec skipifsilent

[Code]
////////////////////////////////////////////////////////////
const
SMTO_ABORTIFHUNG = 2;
WM_WININICHANGE = $001A;

type
WPARAM = UINT_PTR;
LPARAM = INT_PTR;
LRESULT = INT_PTR;

function SendTextMessageTimeout(hWnd: HWND; Msg: UINT;
wParam: WPARAM; lParam: PAnsiChar; fuFlags: UINT;
uTimeout: UINT; out lpdwResult: DWORD): LRESULT;
external 'SendMessageTimeoutA@user32.dll stdcall';  

procedure RefreshEnvironment;
var
S: AnsiString;
MsgResult: DWORD;
begin
S := 'Environment';
SendTextMessageTimeout(HWND_BROADCAST, WM_WININICHANGE, 0,
    PAnsiChar(S), SMTO_ABORTIFHUNG, 5000, MsgResult);
end;
////////////////////////////////////////////////////////////
function GetProgramFiles(Param: string): string;
begin
if IsWin64 then Result := ExpandConstant('{pf64}')
    else Result := ExpandConstant('{pf32}')
end;

///PATH ENVINRONMENT//////////////////////////////////////////
function Replace(Dest, SubStr, Str: string): string;
var
Position: Integer;
Ok: Integer;
begin
Ok := 1;
while Ok > 0 do
begin
    Position:=Pos(SubStr, Dest);
    if Position > 0 then
    begin
    Delete(Dest, Position, Length(SubStr));
    Insert(Str, Dest, Position);
    end else
    Ok := 0;
end;
Result:=Dest;
end;

procedure AppendToPath();
var
V: string;
Str: string;
begin
RegQueryStringValue(HKLM, 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 'Path', V)
Str := ExpandConstant('{app}\libav');
V := Replace(V, Str, '');
V := V + ';' + Str;
V := Replace(V,';;',';');
RegWriteStringValue(HKLM, 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 'Path', V)

// MsgBox(V, mbInformation, MB_OK); 
end;

procedure RemoveFromPath();
var
V: string;
Str: string;
begin
RegQueryStringValue(HKLM, 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 'Path', V)
Str := ExpandConstant('{app}\dlls');
V := Replace(V, Str, '');
V := Replace(V,';;',';');
RegWriteStringValue(HKLM, 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 'Path', V)
//MsgBox(V, mbInformation, MB_OK);
end;

procedure AppendToPathAndRefresh;
begin
AppendToPath;
RefreshEnvironment;
end;

(*
procedure DeinitializeSetup();
begin
AppendToPath();
end;
*)

procedure DeinitializeUninstall();
begin
RemoveFromPath();
end;
///END OF PATH ENVIRONMENT ///////////////////////////////////
Zen Of Kursat
  • 2,672
  • 1
  • 31
  • 47
  • 1
    If you believe your answer is better than those in the linked duplicate question, please post your answer there. Otherwise, just upvote the posts that have helped you and do not post redundant answers. – Martin Prikryl Jun 08 '20 at 09:02
  • 1
    did it. hope it may help some other ppl with a different approach. my answer is not a duplication. hence not redundant. just enhanced with extra codes. – Zen Of Kursat Jun 08 '20 at 09:24