How to Display blocking message box using inno setup pascal scripting - inno-setup

I'm creating installer for my application using inno setup (pascal script),in my setup there is a requirement that if user runs the setup twice a message has to be displayed saying that "another instance is already running" by checking the mutex, i have impletemented that now the problem is it displays the message box but if user still try's to run the setup again and again the above message willbe displayed multiple times which i don't want.so my message box should block him doing anything on his machine untill the messagebox is closed.
EDIT: I wanted to block the current window from where the user is running the setup, to prevent the user from running the setup from same location again.
my sample code:
[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
[Code]
const
MB_APPLMODAL = $00002000;
MB_SYSTEMMODAL = $00001000;
MB_ICONHAND = $00000010;
MB_ICONQUESTION = $00000020;
MB_ICONEXCLAMATION = $00000030;
MB_ICONASTERISK = $00000040;
MB_ICONSTOP = MB_ICONHAND;
MB_ICONINFORMATION = MB_ICONASTERISK;
#ifdef UNICODE
#define AW "W"
#else
#define AW "A"
#endif
function MessageBox(hWnd: HWND; lpText, lpCaption: string;
uType: UINT): Integer; external 'MessageBox{#AW}#user32.dll stdcall';
function SysModalMsgBox(Owner: HWND; const Text: string; MsgType: TMsgBoxType;
Buttons: Integer): Integer;
var
Flags: UINT;
Caption: string;
begin
Flags := Buttons;
case MsgType of
mbInformation: Flags := Flags or MB_ICONINFORMATION;
mbConfirmation: Flags := Flags or MB_ICONQUESTION;
mbError: Flags := Flags or MB_ICONEXCLAMATION;
mbCriticalError: Flags := Flags or MB_ICONSTOP;
end;
if not IsUninstaller then
Caption := SetupMessage(msgSetupAppTitle)
else
Caption := SetupMessage(msgUninstallAppTitle);
Result := MessageBox(Owner, Text, Caption, MB_SYSTEMMODAL);
end;
procedure InitializeWizard;
begin
Sleep(5000);
SysModalMsgBox(WizardForm.Handle, 'Another instance is runinng please try again!',mbInformation , MB_OK);
end;

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.

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.

How to get the full computer name in Inno Setup

I would like to know how to get the Full computer name in Inno Setup, for example Win8-CL01.cpx.local in the following image.
I already know how to get the computer name with GetComputerNameString but I also would like to have the Domain name of the computer. How can I get this full computer name or this domain name ?
There's no built-in function for this in Inno Setup. You can use the GetComputerNameEx Windows API function:
[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
[Code]
#ifdef UNICODE
#define AW "W"
#else
#define AW "A"
#endif
const
ERROR_MORE_DATA = 234;
type
TComputerNameFormat = (
ComputerNameNetBIOS,
ComputerNameDnsHostname,
ComputerNameDnsDomain,
ComputerNameDnsFullyQualified,
ComputerNamePhysicalNetBIOS,
ComputerNamePhysicalDnsHostname,
ComputerNamePhysicalDnsDomain,
ComputerNamePhysicalDnsFullyQualified,
ComputerNameMax
);
function GetComputerNameEx(NameType: TComputerNameFormat; lpBuffer: string; var nSize: DWORD): BOOL;
external 'GetComputerNameEx{#AW}#kernel32.dll stdcall';
function TryGetComputerName(Format: TComputerNameFormat; out Output: string): Boolean;
var
BufLen: DWORD;
begin
Result := False;
BufLen := 0;
if not Boolean(GetComputerNameEx(Format, '', BufLen)) and (DLLGetLastError = ERROR_MORE_DATA) then
begin
SetLength(Output, BufLen);
Result := GetComputerNameEx(Format, Output, BufLen);
end;
end;
procedure InitializeWizard;
var
Name: string;
begin
if TryGetComputerName(ComputerNameDnsFullyQualified, Name) then
MsgBox(Name, mbInformation, MB_OK);
end;
With built-in functions you can get the full name this way:
[Code]
procedure InitializeWizard;
begin
MsgBox(GetComputerNameString + '.' + GetEnv('UserDnsDomain'), mbInformation, MB_OK);
end;
Although TLama's solution gives you wider possibilities of further development.

Inno Setup check if a machine is joined to a Domain

Is there a way of checking whether the machine you are installing on is joined to a Domain or in a Workgroup?
I found this article about how to do this in Delphi, but I am unable to get this working within Inno Setup. Can anyone assist with this? Is this even possible?
http://delphi.about.com/od/delphitips2009/qt/computer-in-a-domain.htm
I would translate (and shorten) it this way:
[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
[Code]
const
NERR_BASE = 2100;
NERR_SetupNotJoined = NERR_BASE + 592;
type
NET_API_STATUS = DWORD;
function NetRenameMachineInDomain(lpServer: WideString;
lpNewMachineName: WideString; lpAccount: WideString;
lpPassword: WideString; fRenameOptions: DWORD): NET_API_STATUS;
external 'NetRenameMachineInDomain#netapi32.dll stdcall';
function IsInDomain: Boolean;
begin
Result := NetRenameMachineInDomain('', '', '', '', 0) <> NERR_SetupNotJoined;
end;
procedure InitializeWizard;
begin
if IsInDomain then
MsgBox('Is in domain.', mbInformation, MB_OK)
else
MsgBox('Is not in domain.', mbInformation, MB_OK);
end;

Make Inno Setup installer request privileges elevation only when needed

Inno Setup installer has the PrivilegesRequired directive that can be used to control, if privileges elevation is required, when installer is starting. I want my installer to work even for non-admin users (no problem about installing my app to user folder, instead of the Program Files). So I set the PrivilegesRequired to none (undocumented value). This makes UAC prompt popup for admin users only, so they can install even to the Program Files. No UAC prompt for non-admin users, so even them can install the application (to user folder).
This has some drawbacks though:
Some people use distinct admin and non-admin accounts on their machines, working with non-admin account normally. In general, when launching installation using non-admin account, when they get UAC prompt, they enter credentials for the admin account to proceed. But this won't work with my installer, because there's no UAC prompt.
(Overly suspicious) people with admin account, who want to install to user folder, cannot launch my installer without (not-needed) admin privileges.
Is there some way to make Inno Setup request privileges elevation only when needed (when user selects installation folder writable by admin account only)?
I assume there's no setting for this in Inno Setup. But possibly, there's a programmatic solution (Inno Setup Pascal scripting) or some kind of plugin/DLL.
Note that Inno Setup 6 has a built-in support for non-administrative install mode.
Inno Setup 6 has a built-in support for non-administrative install mode.
Basically, you can simply set PrivilegesRequiredOverridesAllowed:
[Setup]
PrivilegesRequiredOverridesAllowed=commandline dialog
Additionally, you will likely want to use the auto* variants of the constants. Notably the {autopf} for the DefaultDirName.
[Setup]
DefaultDirName={pf}\My Program
The following is my (now obsolete) solution for Inno Setup 5, based on #TLama's answer.
When the setup is started non-elevated, it will request elevation, with some exceptions:
Only on Windows Vista and newer (though it should work on Windows XP too)
When upgrading, the setup will check if the current user has a write access to the previous installation location. If the user has the write access, the setup won't request the elevation. So if the user has previously installed the application to user folder, the elevation won't be requested on upgrade.
If the user rejects the elevation on a new install, the installer will automatically fall back to "local application data" folder. I.e. C:\Users\standard\AppData\Local\AppName.
Other improvements:
the elevated instance won't ask for language again
by using PrivilegesRequired=none, the installer will write uninstall information to HKLM, when elevated, not to HKCU.
#define AppId "myapp"
#define AppName "MyApp"
#define InnoSetupReg \
"Software\Microsoft\Windows\CurrentVersion\Uninstall\" + AppId + "_is1"
#define InnoSetupAppPathReg "Inno Setup: App Path"
[Setup]
AppId={#AppId}
PrivilegesRequired=none
...
[Code]
function IsWinVista: Boolean;
begin
Result := (GetWindowsVersion >= $06000000);
end;
function HaveWriteAccessToApp: Boolean;
var
FileName: string;
begin
FileName := AddBackslash(WizardDirValue) + 'writetest.tmp';
Result := SaveStringToFile(FileName, 'test', False);
if Result then
begin
Log(Format(
'Have write access to the last installation path [%s]', [WizardDirValue]));
DeleteFile(FileName);
end
else
begin
Log(Format('Does not have write access to the last installation path [%s]', [
WizardDirValue]));
end;
end;
procedure ExitProcess(uExitCode: UINT);
external 'ExitProcess#kernel32.dll stdcall';
function ShellExecute(hwnd: HWND; lpOperation: string; lpFile: string;
lpParameters: string; lpDirectory: string; nShowCmd: Integer): THandle;
external 'ShellExecuteW#shell32.dll stdcall';
function Elevate: Boolean;
var
I: Integer;
RetVal: Integer;
Params: string;
S: string;
begin
{ Collect current instance parameters }
for I := 1 to ParamCount do
begin
S := ParamStr(I);
{ Unique log file name for the elevated instance }
if CompareText(Copy(S, 1, 5), '/LOG=') = 0 then
begin
S := S + '-elevated';
end;
{ Do not pass our /SL5 switch }
if CompareText(Copy(S, 1, 5), '/SL5=') <> 0 then
begin
Params := Params + AddQuotes(S) + ' ';
end;
end;
{ ... and add selected language }
Params := Params + '/LANG=' + ActiveLanguage;
Log(Format('Elevating setup with parameters [%s]', [Params]));
RetVal :=
ShellExecute(0, 'runas', ExpandConstant('{srcexe}'), Params, '', SW_SHOW);
Log(Format('Running elevated setup returned [%d]', [RetVal]));
Result := (RetVal > 32);
{ if elevated executing of this setup succeeded, then... }
if Result then
begin
Log('Elevation succeeded');
{ exit this non-elevated setup instance }
ExitProcess(0);
end
else
begin
Log(Format('Elevation failed [%s]', [SysErrorMessage(RetVal)]));
end;
end;
procedure InitializeWizard;
var
S: string;
Upgrade: Boolean;
begin
Upgrade :=
RegQueryStringValue(HKLM, '{#InnoSetupReg}', '{#InnoSetupAppPathReg}', S) or
RegQueryStringValue(HKCU, '{#InnoSetupReg}', '{#InnoSetupAppPathReg}', S);
{ elevate }
if not IsWinVista then
begin
Log(Format('This version of Windows [%x] does not support elevation', [
GetWindowsVersion]));
end
else
if IsAdminLoggedOn then
begin
Log('Running elevated');
end
else
begin
Log('Running non-elevated');
if Upgrade then
begin
if not HaveWriteAccessToApp then
begin
Elevate;
end;
end
else
begin
if not Elevate then
begin
WizardForm.DirEdit.Text := ExpandConstant('{localappdata}\{#AppName}');
Log(Format('Falling back to local application user folder [%s]', [
WizardForm.DirEdit.Text]));
end;
end;
end;
end;
There is no built-in way for conditional elevation of the setup process during its lifetime in Inno Setup. However, you can execute the setup process by using runas verb and kill the non-elevated one. The script that I wrote is a bit tricky, but shows a possible way how to do it.
Warning:
The code used here attempts to execute the elevated setup instance always; there is no check whether the elevation is actually required or not (how to decide whether the elevation is needed optionally ask in a separate question, please). Also, I can't tell at this time, if it's safe to do such manual elevation. I'm not sure if Inno Setup doesn't (or will not) rely on the value of the PrivilegesRequired directive in some way. And finally, this elevation stuff should be executed only on related Windows versions. No check for this is done in this script:
[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
PrivilegesRequired=lowest
[Code]
#ifdef UNICODE
#define AW "W"
#else
#define AW "A"
#endif
type
HINSTANCE = THandle;
procedure ExitProcess(uExitCode: UINT);
external 'ExitProcess#kernel32.dll stdcall';
function ShellExecute(hwnd: HWND; lpOperation: string; lpFile: string;
lpParameters: string; lpDirectory: string; nShowCmd: Integer): HINSTANCE;
external 'ShellExecute{#AW}#shell32.dll stdcall';
var
Elevated: Boolean;
PagesSkipped: Boolean;
function CmdLineParamExists(const Value: string): Boolean;
var
I: Integer;
begin
Result := False;
for I := 1 to ParamCount do
if CompareText(ParamStr(I), Value) = 0 then
begin
Result := True;
Exit;
end;
end;
procedure InitializeWizard;
begin
{ initialize our helper variables }
Elevated := CmdLineParamExists('/ELEVATE');
PagesSkipped := False;
end;
function ShouldSkipPage(PageID: Integer): Boolean;
begin
{ if we've executed this instance as elevated, skip pages unless we're }
{ on the directory selection page }
Result := not PagesSkipped and Elevated and (PageID <> wpSelectDir);
{ if we've reached the directory selection page, set our flag variable }
if not Result then
PagesSkipped := True;
end;
function NextButtonClick(CurPageID: Integer): Boolean;
var
Params: string;
RetVal: HINSTANCE;
begin
Result := True;
{ if we are on the directory selection page and we are not running the }
{ instance we've manually elevated, then... }
if not Elevated and (CurPageID = wpSelectDir) then
begin
{ pass the already selected directory to the executing parameters and }
{ include our own custom /ELEVATE parameter which is used to tell the }
{ setup to skip all the pages and get to the directory selection page }
Params := ExpandConstant('/DIR="{app}" /ELEVATE');
{ because executing of the setup loader is not possible with ShellExec }
{ function, we need to use a WinAPI workaround }
RetVal := ShellExecute(WizardForm.Handle, 'runas',
ExpandConstant('{srcexe}'), Params, '', SW_SHOW);
{ if elevated executing of this setup succeeded, then... }
if RetVal > 32 then
begin
{ exit this non-elevated setup instance }
ExitProcess(0);
end
else
{ executing of this setup failed for some reason; one common reason may }
{ be simply closing the UAC dialog }
begin
{ handling of this situation is upon you, this line forces the wizard }
{ stay on the current page }
Result := False;
{ and possibly show some error message to the user }
MsgBox(Format('Elevating of this setup failed. Code: %d', [RetVal]),
mbError, MB_OK);
end;
end;
end;

Resources