How to set a short delay using Inno Setup? [duplicate] - inno-setup

I have a license.exe file that I call in my setup code at the end,
The code needs the environment variable to be set before working correctly,
The code is as follows:
[Registry]
; set PATH
Root: HKLM; \
Subkey: "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"; \
ValueType: string; ValueName: "PATH"; ValueData: "{app}"
[Setup]
; Tell Windows Explorer to reload the environment
ChangesEnvironment=yes
[Run]
Filename: "{app}\temp\installation_files\license.exe";
Here the code executes, but does not find the correct path.
When I check the system environment variable, it is set correctly,
When I run the license.exe code afterwards manually, it works correctly and sees the environment variable.
Can someone tell me how to fix this?
Or how to delay the [Run] section until the system recognizes the environment variable?

The processes created for executing entries from the [Run] section inherits the environment block of its parent process, which is the installer itself. So you have to set the environment variable to the installer and let it inherit to your executed application. How to do that is shown in the below script:
[Run]
Filename: "{app}\temp\installation_files\license.exe"; BeforeInstall: SetEnvPath
[Code]
#ifdef UNICODE
#define AW "W"
#else
#define AW "A"
#endif
function SetEnvironmentVariable(lpName: string; lpValue: string): BOOL;
external 'SetEnvironmentVariable{#AW}#kernel32.dll stdcall';
procedure SetEnvPath;
begin
if not SetEnvironmentVariable('PATH', ExpandConstant('{app}')) then
MsgBox(SysErrorMessage(DLLGetLastError), mbError, MB_OK);
end;
Previous answer for notifying rest of the system about variable change:
As #Jerry pointed out in his comment, a notification about the environment changes is performed after the [Run] section is processed. Actually, it is one of the last things executed by the installer.
So, to notify the system about environment changes before processing the [Run] section, you'll need to have a workaround. I rewrote the RefreshEnvironment procedure from Inno Setup code to script. It's the same function as it's executed if you have ChangesEnvironment directive set to yes.
In the following script I have removed the ChangesEnvironment directive and added execution of the RefreshEnvironment procedure from the AfterInstall parameter function of your registry entry:
[Registry]
Root: HKLM; \
Subkey: "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"; \
ValueType: string; ValueName: "PATH"; ValueData: "{app}"; \
AfterInstall: RefreshEnvironment;
[Run]
Filename: "{app}\temp\installation_files\license.exe";
[Code]
const
SMTO_ABORTIFHUNG = 2;
WM_WININICHANGE = $001A;
WM_SETTINGCHANGE = WM_WININICHANGE;
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_SETTINGCHANGE, 0,
PAnsiChar(S), SMTO_ABORTIFHUNG, 5000, MsgResult);
end;

The solution with SetEnvironmentVariable in TLama's answer is correct for many situations.
But it won't work for [Run] tasks with runasoriginaluser flag (what is implied by postinstall flag). I.e. the variable won't be propagated to an application run with common "Run My Program" check box on the "Finished" page.
The reason is that the tasks with runasoriginaluser are executed by a un-elevated hidden parent process of the Inno Setup installer. The SetEnvironmentVariable will change environment for the installer, but not for its parent process. Unfortunately, the parent process of the installer cannot be controlled (imo).
As a workaround, to set the variable for the runasoriginaluser tasks, you have to inject an intermediate process between the installer parent process and the task, and have the intermediate process set the variable.
Such an intermediate process can easily be the cmd.exe with its set command:
[Run]
Filename: "{cmd}"; Parameters: "/C set PATH=%PATH%;{app} & ""{app}\MyProg.exe"""; \
Description: "Run My Program"; Flags: postinstall runhidden
The runhidden flag hides the cmd.exe console window, not the application (assuming it's a GUI application). If it's a console application and you want the output to be visible, remove the runhidden flag. Alternatively, you can use start command to start the application in its own console window.

after some 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;
///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 DeinitializeUninstall();
begin
RemoveFromPath();
end;
///END OF PATH ENVIRONMENT ///////////////////////////////////

Related

Can we make the DwinHs Inno Setup download plugin work faster?

DwinHs has a DwinsHs_Data_Buffer_Length macro. I have set it to a value of 8192 but the download speeds are still rather slow.
For example, I have a 200 MBit connection and the file is 25 MB. It takes two and a half minutes to download.
I have read through some of the answers here:
What is the best memory buffer size to allocate to download a file from Internet?
It suggests a 16K buffer instead. Either way, is there any way we can use a maximum buffer length with Pascal given the user system?
Browsers download faster so why can’t we here in Inno Setup?
Example
[ISPP]
#define HelpDocSetupURL "https://www.publictalksoftware.co.uk/downloads/PublicTalksHelpDocumentationSetup.exe"
[Setup]
AppID = TestID
AppName = Test App
OutputBaseFilename = My_Test_App_Setup
AppVersion = 1.0
DefaultDirName = {pf}\MyTestApp
DefaultGroupName = My Test App
[Tasks]
Name: "downloadhelp"; Description: "Task Desc"; GroupDescription: "Group Desc";
[Files]
Source: "{tmp}\HelpDocSetup.exe"; \
DestDir: "{app}"; \
Flags: external deleteafterinstall; \
Tasks: downloadhelp; \
Check: DwinsHs_Check( ExpandConstant('{tmp}\HelpDocSetup.exe'), '{#HelpDocSetupURL}', 'My_Setup', 'Get', 0, 0 )
[Code]
program Setup;
#define DwinsHs_Use_Predefined_Downloading_WizardPage
#define DwinsHs_Data_Buffer_Length 8192
#include "dwinshs.iss"
procedure InitializeWizard();
begin
DwinsHs_InitializeWizard(wpPreparing);
end;
procedure CurPageChanged(CurPageID: Integer);
begin
DwinsHs_CurPageChanged(CurPageID, nil, nil);
end;
function ShouldSkipPage(CurPageId: Integer): Boolean;
begin
Result := False;
DwinsHs_ShouldSkipPage(CurPageId, Result);
end;
function BackButtonClick(CurPageID: Integer): Boolean;
begin
Result := True;
DwinsHs_BackButtonClick(CurPageID);
end;
function NextButtonClick(CurPageID: Integer): Boolean;
begin
Result := True;
DwinsHs_NextButtonClick(CurPageID, Result);
end;
procedure CancelButtonClick(CurPageID: Integer; var Cancel, Confirm: Boolean);
begin
DwinsHs_CancelButtonClick(CurPageID, Cancel, Confirm);
end;
The software author got back to me about this issue and proposed:
You can try to set the cache size to 16384, 32768 or 655536.
So I set it to:
#define DwinsHs_Data_Buffer_Length 655536
The results are much better:
As you can see, only 10 seconds.
I don't know what impact such a cache value would have for those who had a slower internet connection.

Run post install file replace before creating service

I have configured the below script to ask the user for an IP address as part of the install wizard, this address gets written to a config file which the application will reference to know where to communicate with.
However currently, the service is created before the configuration file is updated (Run section happens before CurStep = ssPostInstall), so the default value has already been read from the file before it is updated.
This means that a further manual service restart is currently required to get the application to communicate.
I understand it is possible to have the Run section happen after the ssPostInstall.
I have read the article Inno Setup: How to run a code procedure in Run section or before Run section? which suggests I can use the BeforeInstall: parameter as part of the run command to perform the CurStepChanged procedure first. However, this leads to an error of
Required function or procedure 'CurStepChanged' found but not with a compatible prototype.
I also tried moving the run line and command above the CurStepChanged section (to match the article mentioned as closely as possible), but this still returned the same error.
Can anyone offer some guidance as to where I am going wrong with the configuration?
[Code]
var
PrimaryServerPage: TInputQueryWizardPage;
function FileReplaceString(ReplaceString: string):boolean;
var
MyFile : TStrings;
MyText : string;
begin
Log('Replacing in file');
MyFile := TStringList.Create;
try
Result := true;
try
MyFile.LoadFromFile(ExpandConstant('{app}' + '\providers\print\win\print-provider.conf'));
Log('File loaded');
MyText := MyFile.Text;
{ Only save if text has been changed. }
if StringChangeEx(MyText, 'REPLACE_WITH_CUSTOMER_IP', ReplaceString, True) > 0 then
begin;
Log('IP address inserted');
MyFile.Text := MyText;
MyFile.SaveToFile(ExpandConstant('{app}' + '\providers\print\win\print-provider.conf'));
Log('File saved');
end;
except
Result := false;
end;
finally
MyFile.Free;
end;
Result := True;
end;
procedure InitializeWizard;
begin
PrimaryServerPage :=
CreateInputQueryPage(
wpWelcome, 'Application Server Details', 'Where is installed?',
'Please specify the IP address or hostname of your ' +
'Primary Application Server, then click Next.');
PrimaryServerPage.Add('Primary Application Server IP/Hostname:', False);
end;
procedure CurStepChanged(CurStep: TSetupStep);
begin
if CurStep = ssPostInstall then
begin
Log('File installed, replacing IP address');
FileReplaceString(PrimaryServerPage.Values[0]);
end;
end;
[run]
Filename: {sys}\sc.exe; Parameters: "create PCPrintProvider start= auto binPath= ""{app}\providers\print\win\pc-print.exe PCPrintProvider"" depend= Spooler" ; Flags: runhidden ; BeforeInstall: CurStepChanged
Filename: {sys}\sc.exe; Parameters: "start PCPrintProvider" ; Flags: runhidden ; BeforeInstall: CurStepChanged
You have to define your own procedure for the BeforeInstall parameter:
[Run]
Filename: {sys}\sc.exe; \
Parameters: "create PCPrintProvider start= auto binPath= ""{app}\providers\print\win\pc-print.exe PCPrintProvider"" depend= Spooler"; \
Flags: runhidden; BeforeInstall: ReplaceIPAddress
[Code]
procedure ReplaceIPAddress;
begin
FileReplaceString(PrimaryServerPage.Values[0]);
end;
Try using Check param
[run]
Filename: {sys}\sc.exe; Parameters: "create PCPrintProvider start= auto binPath= ""{app}\providers\print\win\pc-print.exe PCPrintProvider"" depend= Spooler" ; Flags: runhidden ; Check: IsIpChanged;
[code]
function IsIpChanged: Boolean;
begin
FileReplaceString(PrimaryServerPage.Values[0]);
Result := True;
end;

Inno Setup - How to force programs started from Inno Setup to open on a certain place on the screen?

With this code: Install DirectX & VCRedist in freearc default script when progress bar is full & paused after main file extraction I can install DirectX and VCRedist with Inno Setup. But, is it possible to force the installation window of these programs to a certain place on the screen? For example:
It's hardly possible to make an application to start at desired position, unless the application explicitly supports it.
So in general, what you can do is to watch for a certain window to appear and move it afterwards. You can identify the window by its caption (FindWindowByWindowName) or class (FindWindowByClassName). Drawback is that the window will briefly appear on its default position.
[Files]
Source: "DXWebSetup.exe"; DestDir: "{tmp}"; Flags: deleteafterinstall
[Run]
Filename: "{tmp}\DXWebSetup.exe"; StatusMsg: "Installing DirectX..."; \
BeforeInstall: StartWaitingForDirectXWindow; \
AfterInstall: StopWaitingForDirectXWindow
[Code]
function SetTimer(hWnd, nIDEvent, uElapse, lpTimerFunc: LongWord): LongWord;
external 'SetTimer#User32.dll stdcall';
function KillTimer(hWnd, nIDEvent: LongWord): LongWord;
external 'KillTimer#User32.dll stdcall';
function GetTickCount: DWord; external 'GetTickCount#kernel32 stdcall';
function SetWindowPos(hWnd: HWND; hWndInsertAfter: HWND; X: Integer; Y: Integer;
cx: Integer; cy: Integer; uFlags: UINT): BOOL;
external 'SetWindowPos#user32.dll stdcall';
const
SWP_NOSIZE = $01;
SWP_NOZORDER = $04;
var
WindowWaitTimer: LongWord;
WindowWaitStarted: DWord;
MoveWindowRunning: Boolean;
procedure MoveDirectXWindowProc(
H: LongWord; Msg: LongWord; IdEvent: LongWord; Time: LongWord);
var
Retry: Boolean;
Handle: HWND;
begin
Handle := FindWindowByWindowName('Installing Microsoft(R) DirectX(R)');
if Handle = 0 then
begin
if DWord(GetTickCount - WindowWaitStarted) < 5000 then
begin
Log('DirectX window not found, will try again shortly');
Retry := True;
end
else
begin
Log('Giving up waiting for DirectX window');
Retry := False;
end
end
else
begin
Log('DirectX window found');
SetWindowPos(
Handle, 0, WizardForm.Left + ScaleX(150), WizardForm.Top + ScaleX(30),
0, 0, SWP_NOSIZE or SWP_NOZORDER);
Retry := False;
end;
if not Retry then
begin
Log('Stopping timer');
KillTimer(0, WindowWaitTimer);
WindowWaitTimer := 0;
end;
end;
procedure StartWaitingForDirectXWindow;
begin
Log('Starting waiting for DirectX window');
WindowWaitTimer := SetTimer(0, 0, 100, CreateCallback(#MoveDirectXWindowProc));
WindowWaitStarted := GetTickCount;
end;
procedure StopWaitingForDirectXWindow;
begin
if WindowWaitTimer <> 0 then
begin
Log('DirectX installer finished, and we are still waiting for its window, stopping');
KillTimer(0, WindowWaitTimer);
WindowWaitTimer := 0;
end
else
begin
Log('DirectX installer finished, and we are no longer waiting for its window');
end;
end;
For CreateCallback function, you need Inno Setup 6. If you are stuck with Inno Setup 5, you can use WrapCallback function from InnoTools InnoCallback library.

Get Image File Information using MediaInfo library in Inno Setup Pascal Script

I have been trying for more than two days to get JPEG Image and MP4 Video File Information using MediaInfo.DLL in my Pascal Script.
But I keep getting error
Runtime Error (at 6:366) - Access Violation at address 0042FD23. Read of address 8065241E.'
The error mostly points to (at 6:366).
I can't think what problem is causing this exception when trying to get Media Information using MediaInfo.DLL.
The code I added to my Script:
[Files]
Source: Lamborghini_Aventador.jpg; DestDir: {tmp}; Flags: dontcopy
Source: MediaInfo.dll; DestDir: {tmp}; Flags: dontcopy
[Code]
#ifdef UNICODE
type
PWideChar = WideString;
#endif
const
StreamKind_Image = 5;
InfoKind_Text = 1;
function MediaInfo_New: Cardinal;
external 'MediaInfo_New#{tmp}\MediaInfo.dll stdcall delayload';
function MediaInfo_Open(Handle: Cardinal; File__: PWideChar): Boolean;
external 'MediaInfo_Open#{tmp}\MediaInfo.dll stdcall delayload';
function MediaInfo_Get(Handle: Cardinal; StreamKind: Integer; StreamNumber: Integer; Parameter: PWideChar; KindOfInfo: Integer; KindOfSearch: Integer): PWideChar;
external 'MediaInfo_Get#{tmp}\MediaInfo.dll stdcall delayload';
procedure RetrieveImageInformation;
var
IHandle: Cardinal;
Width: PWideChar;
begin
ExtractTemporaryFile('Lamborghini_Aventador.jpg');
ExtractTemporaryFile('MediaInfo.dll');
IHandle := MediaInfo_New();
MediaInfo_Open(IHandle, PWideChar(ExpandConstant('{tmp}\Lamborghini_Aventador.jpg')));
Width := MediaInfo_Get(IHandle, StreamKind_Image, 0, 'Width', InfoKind_Text, 0);
Log('Width of the JPEG Image: ' + PWideChar(Width) + '.');
end;
The line which the exception is generating is:
Width := MediaInfo_Get(IHandle, StreamKind_Image, 0, 'Width', InfoKind_Text, 0);
I expected that the compiler output will be Width of the JPEG Image: 1920.
I use latest version of Unicode Inno Setup Compiler (5.5.9 - U)
Thanks in advance for your important help.
I do not think you can call a function that returns a pointer to a string (character buffer) from Inno Setup Pascal Script.
But you can hack it like this:
Declare the function as if it returns Cardinal;
Use some available function that takes a pointer and copies it to another pointer. Declare the source pointer as Cardinal and the target pointer as string. One such function is the StrCpyN.
function MediaInfo_Get(
Handle: Cardinal; StreamKind: Integer; StreamNumber: Integer;
Parameter: string; KindOfInfo: Integer; KindOfSearch: Integer): Cardinal;
external 'MediaInfo_Get#{tmp}\MediaInfo.dll stdcall delayload';
function StrCpyN(S1: string; S2: Cardinal; Max: Cardinal): Cardinal;
external 'StrCpyNW#shlwapi.dll stdcall';
var
P: Cardinal;
S: string;
begin
P := MediaInfo_Get(IHandle, StreamKind_Image, 0, 'Width', InfoKind_Text, InfoKind_Name);
S := StringOfChar(' ', 1024);
StrCpyN(S, P, Length(S) - 1);
S := Trim(S);
...
end;
The code requires Unicode Inno Setup (the only version as of Inno Setpu 6).
You can use the MediaInfo Command Line Interface with Inno Setup Ansi or Unicode Version.
Usage Example:
[Files]
Source: MediaInfo.exe; DestDir: {tmp}; Flags: Dontcopy
[code]
function InitializeSetup(): Boolean;
var
ErrorCode: Integer;
begin
ExtractTemporaryFile('MediaInfo.exe');
ShellExec('Open', 'MediaInfo.exe', ExpandConstant('"YourFileName.mp4" --LogFile="YourFileName Prperties.log"'), ExpandConstant('{tmp}'), SW_HIDE, ewWaitUntilTerminated, ErrorCode);
if SysErrorMessage(DLLGetLastError) = SysErrorMessage(0) then
Result := True;
end;
Now, navigate to the Inno Setup Temporary Directory as Administrator using Run (Windows Key + R) Command and see your Media Information Log File exists there which contains the Information about the File you given in the Command.

Inno UninstallRun How To get global variable by executing

My target is to read a registry entry at [UninstallRun].
The issue is, if Inno Setup is running in an install process the Function GetRegistryVar() is called here ? It should be called when the UnInstall process is running. If I start the UnInstall process the Function GetRegistryVar() will not called here ? Only the CurUninstallStepChanged() and PGetRegistryVal(). So I can't receive my global variable gStrPrnName ?
[UninstallRun]
Filename: "{#dInst64bitDir}\{#dUnInstDrvExeName}"; Parameters: "-f {code:GetGlobalRegistryVar}"; WorkingDir: "{#dInst64bitDir}"; Flags: runhidden runascurrentuser;
[Code]
var
gStrPrnName: string;
function GetGlobalRegistryVar(Value: string): string;
begin
Result := gStrPrnName;
end;
procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
begin
case CurUninstallStep of
usUninstall:
begin
// ...insert code to perform pre-uninstall tasks here...
PGetRegistryVal(gStrPrnName);
end;
usPostUninstall:
begin
// ...insert code to perform post-uninstall tasks here...
end;
end;
end;
function PGetRegistryVal(Value: String): String;
var
strPrnName: string;
i: Integer;
begin
for i:=0 to 2 do
begin
if RegQueryStringValue(HKLM, 'SOFTWARE\Microsoft\Windows NT\CurrentVersion\GetDevice_1', 'Name', strPrnName) then
Result := strPrnName;
if RegQueryStringValue(HKLM, 'SOFTWARE\Microsoft\Windows NT\CurrentVersion\GetDevice_2', 'Name', strPrnName) then
Result := strPrnName;
...
end;
end;
Has somebody a solution for this issue ?
Thanks

Resources