Currently I'm creating a shortcut as so:
SetShellVarContext all
SetOutPath "$INSTDIR"
CreateShortCut "$SMPROGRAMS\MyApp.lnk" "$INSTDIR\MyApp.exe"
I would like to change the working directory of this shortcut from C:\Program Files\MyApp to %UserProfile%.
The tricky part is that I don't want %UserProfile% to be expanded, I want to keep it as an environment variable, so the program starts in the profile directory of current user.
Can I achieve this with NSIS? If not, what would be the simplest workaround?
Reference: CreateShortcut
NSIS calls IShellLink::SetWorkingDirectory on the shortcut with the path set by SetOutPath ($OutDir).
It is possible to set $OutDir to something that is not a valid path and then call CreateShortcut:
Push $OutDir ; Save
StrCpy $OutDir "%UserProfile%"
CreateShortcut "$temp\test1.lnk" "$sysdir\Calc.exe"
Pop $OutDir ; Restore
It does work but is perhaps bending the rules a bit. You can also do it without relying on undocumented NSIS quirks:
!define CLSCTX_INPROC_SERVER 1
!define STGM_READWRITE 2
!define IID_IPersistFile {0000010b-0000-0000-C000-000000000046}
!define CLSID_ShellLink {00021401-0000-0000-c000-000000000046}
!define IID_IShellLinkA {000214ee-0000-0000-c000-000000000046}
!define IID_IShellLinkW {000214f9-0000-0000-c000-000000000046}
!ifdef NSIS_UNICODE
!define IID_IShellLink ${IID_IShellLinkW}
!else
!define IID_IShellLink ${IID_IShellLinkA}
!endif
!include LogicLib.nsh
Function Lnk_SetWorkingDirectory
Exch $9 ; New working directory
Exch
Exch $8 ; Path
Push $0 ; HRESULT
Push $1 ; IShellLink
Push $2 ; IPersistFile
System::Call 'OLE32::CoCreateInstance(g "${CLSID_ShellLink}",i 0,i ${CLSCTX_INPROC_SERVER},g "${IID_IShellLink}",*i.r1)i.r0'
${If} $0 = 0
System::Call `$1->0(g "${IID_IPersistFile}",*i.r2)i.r0`
${If} $0 = 0
System::Call `$2->5(wr8,i${STGM_READWRITE})i.r0` ; Load
${If} $0 = 0
System::Call `$1->9(tr9)i.r0` ; SetWorkingDirectory
${If} $0 = 0
System::Call `$2->6(i0,i0)i.r0` ; Save
${EndIf}
${EndIf}
System::Call `$2->2()` ; Release
${EndIf}
System::Call `$1->2()` ; Release
${EndIf}
StrCpy $9 $0
Pop $1
Pop $0
Pop $8
Exch $9
FunctionEnd
Section
CreateShortcut "$temp\test2.lnk" "$sysdir\Calc.exe"
Push "$temp\test2.lnk"
Push "%UserProfile%"
Call Lnk_SetWorkingDirectory
Pop $0
DetailPrint HRESULT=$0 ; 0 = success
SectionEnd
It should be noted that IShellLink::SetWorkingDirectory does not say anything about supporting unexpanded environment variables but they do seem to work.
Related
I have a python app that I'm installing with NSIS. What I cant figure out is how you get an App into the "priority list" that is in the Focus Assist area of windows. The dialog suggests you can add any app - but you can't. Its a really small amount of apps that are available. How can you make an app available to this Focus Assist priority list?
It might have something to do with AUMID. You need to set this when you install your app. When using NSIS these two options don't seem to fix this issue
https://github.com/safing/nsis-shortcut-properties
https://nsis.sourceforge.io/WinShell_plug-in
I'm not sure if setting the ToastActivatorCLSID is going to help but here is some NSIS code to do that. I noticed that OneDrive also sets the ID so this function sets both:
!include LogicLib.nsh
!include Win\COM.nsh
!include Win\Propkey.nsh
Function SetLnkToastActivatorCLSIDAndAUMI
System::Store S
Pop $3
Pop $4
Pop $6
!insertmacro ComHlpr_CreateInProcInstance ${CLSID_ShellLink} ${IID_IShellLink} r1 ""
${If} $1 P<> 0
${IUnknown::QueryInterface} $1 '("${IID_IPersistFile}",.r2)'
${IUnknown::Release} $1 ""
${If} $2 P<> 0
${IPersistFile::Load} $2 '(r3,${STGM_READWRITE})i.r0'
${If} $0 >= 0
${IUnknown::QueryInterface} $2 '("${IID_IPropertyStore}",.r1)'
${If} $1 P<> 0
System::Call '*${SYSSTRUCT_PROPERTYKEY}(${PKEY_AppUserModel_ToastActivatorCLSID})p.r3'
System::Call '*(&g16"$4")p.r4'
System::Call '*${SYSSTRUCT_PROPVARIANT}(${VT_CLSID},,p$4)p.r5'
${IPropertyStore::SetValue} $1 '($3,$5)i.r0'
System::Free $4
${If} $6 != ""
System::Call '*$3${SYSSTRUCT_PROPERTYKEY}(${PKEY_AppUserModel_ID})'
System::Call '*(&w${NSIS_MAX_STRLEN}s)p.r4' $6
${V_SetPointer} $5 ${VT_LPWSTR} $4
${IPropertyStore::SetValue} $1 '($3,$5)'
System::Free $4
${EndIf}
${IPropertyStore::Commit} $1 ""
System::Free $3
System::Free $5
${IUnknown::Release} $1 ""
${If} $0 >= 0
${IPersistFile::Save} $2 '(p0,1)'
${EndIf}
${EndIf}
${EndIf}
${IUnknown::Release} $2 ""
${EndIf}
${EndIf}
System::Store L
FunctionEnd
!macro SetLnkToastActivatorCLSIDAndAUMI lnkpath clsid aumi
Push "${aumi}"
Push ${clsid}
Push "${lnkpath}"
Call SetLnkToastActivatorCLSIDAndAUMI
!macroend
Section
!define MYTOASTCLASSGUID {696a4cab-ef17-48d1-8bb5-6ebc36b94d0a} ; Replace this with your own CLSID
CreateShortcut /NoWorkingDir "$SMPrograms\NSIS_toast_test.lnk" "$sysdir\winver.exe"
!insertmacro SetLnkToastActivatorCLSIDAndAUMI "$SMPrograms\NSIS_toast_test.lnk" ${MYTOASTCLASSGUID} "My.AppModel.Id.Goes.Here"
SectionEnd
I'm using WindowsXP, NSIS 2.46 with nsSCM plug-in, and have 4 machines with nearly the same environment(hardware + software) since they come from a same GHOST image but with very limited changes on application layer(no any system settings changed).
I'm using NSIS installer to install my application to them, detail process are:
Stop 'Apache2.2' service.
Copy files to Apache root folder.
CreateShortCut
With script:
CreateShortCut "$SMPROGRAMS\MyApp\Stop.lnk" "$SYSDIR\sc.exe" "stop MyAppService" "C:\WINDOWS\system32\SHELL32.dll" 27 SW_SHOWMINIMIZED
CreateShortCut "$SMPROGRAMS\MyApp\ShowDemo.lnk" "$PROGRAMFILES\MyAppPath\MyAppShowDemoHelper.exe" "-b 102" "C:\WINDOWS\system32\SHELL32.dll" 24 SW_SHOWMINIMIZED
Start 'Apache2.2' service.
With script:
nsSCM::Start /NOUNLOAD "Apache2.2"
Pop $0 ; return error/success
${If} $0 == "success"
MessageBox MB_ICONINFORMATION|MB_OK "Successfully started 'Apache2.2' service"
${Else}
MessageBox MB_ICONSTOP|MB_OK "Failed to start 'Apache2.2' service with result: $0, Contact help desk or start it manually!"
${EndIf}
===========================
Now the problem is the step 4, in two of those machines, it always popup ERROR and always need manually to start the service(with no error), but meantime, the shortcuts are created successfully.
I've checked system logs, Apache logs, but no error logs/message could be found.
I spend one day and tried everything, at last, I found once I remove the Step 3, everything is fine, so any idea Why?
[Edit0]:
For work around, I have to switch Step 3 and 4, at least now it works good.
CreateShortcut should have no impact on plugins so this is rather strange. It would be nice if we could narrow this down to a problem with the plugin API in NSIS or a problem in the plugin itself. Maybe you could try the SimpleSC plugin?
Here is some code that does it manually with the system plugin. It is not pretty but should display a relevant error code if it fails.
Section
!define MyServiceName "Spooler" ;"stisvc"
ExecWait '"$SysDir\cmd.exe" /c net stop "${MyServiceName}"' ; Hack to make sure the service is stopped so we can try starting it again
Sleep 1111
InitPluginsDir
CreateShortcut "$PluginsDir\Test1.lnk" "$ExePath" ; Create some dummy shortcuts to possibly trigger the bug
CreateShortcut "$PluginsDir\Test2.lnk" "$ExePath"
!include LogicLib.nsh
!include Util.nsh
!define ERROR_SERVICE_ALREADY_RUNNING 1056
!define SC_MANAGER_CONNECT 0x0001
!define SERVICE_QUERY_STATUS 0x0004
!define SERVICE_START 0x0010
!define SERVICE_PAUSE_CONTINUE 0x0040
!define SERVICE_CONTROL_CONTINUE 3
!define SERVICE_STOPPED 1
!define SERVICE_START_PENDING 2
!define SERVICE_STOP_PENDING 3
!define SERVICE_RUNNING 4
!define SERVICE_CONTINUE_PENDING 5
!define SERVICE_PAUSE_PENDING 6
!define SERVICE_PAUSED 7
!macro WaitForServiceRunningStatus_
System::Store S
Pop $6
Pop $1
System::Call KERNEL32::GetTickCount()i.r7
System::Call '*(i,i,i,i,i,i,i)i.r2'
loop:
System::Call 'ADVAPI32::QueryServiceStatus(ir1, ir2)i.r3'
System::Call '*$2(i,i.r4)'
${If} $3 <> 0
${If} $4 = ${SERVICE_RUNNING}
DetailPrint 'Service is now running.'
${Else}
Sleep 250
System::Call KERNEL32::GetTickCount()i.r8
${IfThen} $8 < $7 ${|} StrCpy $7 $8 ${|} ; Reset on GetTickCount rollover
IntOp $8 $8 - $7
IntCmpU $8 $6 "" loop
DetailPrint 'Timeout! Service status is $4'
${EndIf}
${EndIf}
System::Free $2
System::Store L
!macroend
!macro WaitForServiceRunningStatus hSC MsTimeout
Push ${hSC}
Push ${MsTimeout}
${CallArtificialFunction} WaitForServiceRunningStatus_
!macroend
System::Call 'ADVAPI32::OpenSCManager(t"", i0, i${SC_MANAGER_CONNECT})i.r0 ?e'
Pop $9
${If} $0 = 0
DetailPrint 'OpenSCManager(t"", i0, i${SC_MANAGER_CONNECT}) failed with error $9'
${Else}
System::Call 'ADVAPI32::OpenService(ir0, t"${MyServiceName}", i${SERVICE_QUERY_STATUS}|${SERVICE_START}|${SERVICE_PAUSE_CONTINUE})i.r1 ?e'
Pop $9
${If} $1 = 0
DetailPrint 'OpenService("${MyServiceName}") failed with error $9'
${Else}
System::Call '*(i,i,i,i,i,i,i)i.r2'
System::Call 'ADVAPI32::QueryServiceStatus(ir1, ir2)i.r3 ?e'
Pop $9
${If} $3 = 0
DetailPrint 'QueryServiceStatus failed with error $9'
StrCpy $4 0 ; We failed, set to unused code so we stop processing
${Else}
System::Call '*$2(i.r3,i.r4,i.r5,i,i,i,i)'
IntFmt $3 "%#x" $3
IntFmt $4 "%#x" $4
IntFmt $5 "%#x" $5
DetailPrint 'QueryServiceStatus: Type=$3, CurrentState=$4 ControlsAccepted=$5'
${EndIf}
${If} $4 = ${SERVICE_PAUSE_PENDING}
${OrIf} $4 = ${SERVICE_PAUSED}
System::Call 'ADVAPI32::ControlService(ir1, i${SERVICE_CONTROL_CONTINUE}, ir2)i.r3 ?e'
Pop $9
${If} $3 = 0
DetailPrint 'ControlService(SERVICE_CONTROL_CONTINUE) failed with error $9'
${Else}
DetailPrint 'Resuming "${MyServiceName}"...'
!insertmacro WaitForServiceRunningStatus $1 5000
${EndIf}
${ElseIf} $4 = ${SERVICE_CONTINUE_PENDING}
!insertmacro WaitForServiceRunningStatus $1 5000
${ElseIf} $4 >= ${SERVICE_STOPPED}
${If} $4 = ${SERVICE_RUNNING}
DetailPrint "Service already running"
${Else}
System::Call 'ADVAPI32::StartService(ir1, i0, i0)i.r3 ?e'
Pop $9
${If} $3 = 0
DetailPrint 'StartService failed with error $9'
${Else}
DetailPrint 'Starting "${MyServiceName}"...'
!insertmacro WaitForServiceRunningStatus $1 9000
${EndIf}
${EndIf}
${EndIf}
System::Free $2
System::Call 'ADVAPI32::CloseServiceHandle(i.r1)'
${EndIf}
System::Call 'ADVAPI32::CloseServiceHandle(i.r0)'
${EndIf}
SectionEnd
I run a certain process via ExecShell from my NSIS installer. That process takes sometime to start (5 - 40 seconds maybe), during which I want the NSIS window to remain visible.
The problem is that while the process itself starts almost instantly, it is sometime before a user would see anything, thus I want the NSIS installer window to remain visible UNTIL the started process's main window (or any window for that matter is shown).
What I need to know thus is how do I get processid from ExecShell (can't use Exec or ExecWait for other reasons), and then how do I use that process id to see if the window has been shown (I know I can do it via a simple sleep, check, goto loop, so basically I am trying to figure out the check part of it)?
So, how exactly can I know if the process I spawned using ShellExec has shown a GUI. This needs to work in Windows XP SP3 and up.
Thank you.
RequestExecutionLevel user
Page InstFiles
!include LogicLib.nsh
!include WinMessages.nsh ; For SW_*
Var IsSlowFakeApp ; We can also pretend to be a silly little app that is slow to start up, this is just so the example code has no external dependencies
Function .onInit
StrCpy $0 $CMDLINE 1 -1
${If} $0 == "?"
StrCpy $IsSlowFakeApp 1
Sleep 3333
${EndIf}
FunctionEnd
Function StartMyAppAndWaitForWindow
StrCpy $0 "$ExePath" ; Application
StrCpy $1 "?" ; Parameters
!define SEE_MASK_NOCLOSEPROCESS 0x40
DetailPrint 'Starting "$0" $1'
System::Store S
System::Call '*(i60,i${SEE_MASK_NOCLOSEPROCESS},i$hwndparent,i0,tr0,tr1,i0,i${SW_SHOW},i,i,i,i,i,i,i)i.r0'
System::Call 'SHELL32::ShellExecuteEx(ir0)i.r1'
${If} $1 <> 0
System::Call '*$0(i,i,i,i,i,i,i,i,i,i,i,i,i,i,i.r1)'
System::Call 'USER32::WaitForInputIdle(ir1,2000)i'
System::Call 'KERNEL32::GetProcessId(ir1)i.r2' ; MSDN says this function is XP.SP1+
StrCpy $3 $2 ; Not found a window yet, keep looping
; Call EnumWindows until we find a window matching our target process id in $2
System::Get '(i.r5, i) iss'
Pop $R0
callEnumWindows:
System::Call 'USER32::EnumWindows(k R0, i) i.s'
loopEnumWindows:
Pop $4
StrCmp $4 "callback1" 0 doneEnumWindows
System::Call 'USER32::GetWindowThreadProcessId(ir5,*i0r4)'
${If} $4 = $2
System::Call 'USER32::IsWindowVisible(ir5)i.r4'
${IfThen} $4 <> 0 ${|} StrCpy $3 0 ${|} ; Found a visible Window
${EndIf}
Push $3 ; EnumWindows callback's return value
System::Call "$R0"
Goto loopEnumWindows
doneEnumWindows:
${If} $3 <> 0
Sleep 1000
Goto callEnumWindows
${EndIf}
System::Free $R0
; Hide installer while app runs
/*HideWindow
System::Call 'KERNEL32::WaitForSingleObject(ir1,i-1)'
BringToFront*/
System::Call 'KERNEL32::CloseHandle(ir1)'
${EndIf}
System::Free $0
System::Store L
FunctionEnd
Section
${If} $IsSlowFakeApp <> 0
SetCtlColors $HWNDPARENT 0xffffff 0xdd0000
FindWindow $0 "#32770" "" $HWNDPARENT
SetCtlColors $0 0xffffff 0xdd0000
DetailPrint "This is a fake slow app and it will close soon..."
Sleep 5555
Quit
${Else}
Call StartMyAppAndWaitForWindow
${EndIf}
SectionEnd
What I basically want is TeamViewer-like first page for my NSIS installer with the following options:
3 radio buttons for: Run Only; Install for current user; Install for all users (requires restart with admin rights).
A label for license like in TeamViewer (i.e. no actual EULA page, only a link to it in the footer).
A button that can change text, i.e. Accept and Run or Accept and Install.
I cannot figure out how to do it easily in terms of UI and in terms of control flow.
Also I need the ability to restart the installer if user decides to install program for all users (i.e. I guess there should be a detectable command line switch, so that if present installer will automatically assume 3rd install type).
A screenshot of a sample UI as requested:
A sample NSIS template would be greatly appreciated.
Thanks.
...
RequestExecutionLevel user
!include LogicLib.nsh
!include nsDialogs.nsh
!include FileFunc.nsh
!include MUI2.nsh
Var mode
Var modeRadioRun
Var modeRadioInstCU
Var modeRadioInstLM
Function OnRadioChange
GetDlgItem $1 $hwndparent 1 ; Find Install/Next button
${NSD_GetState} $modeRadioRun $0
${If} $0 = ${BST_CHECKED}
${NSD_SetText} $1 "Accept && Run"
${Else}
${NSD_SetText} $1 "Accept && Install"
${EndIf}
FunctionEnd
Function ModePageCreate
!insertmacro MUI_HEADER_TEXT "Welcome to blah" "blah blah"
${GetParameters} $0
ClearErrors
${GetOptions} "$0" "/ELEVATEDINSTALL" $0
${IfNot} ${Errors}
UserInfo::GetAccountType
Pop $0
${If} $0 == "Admin"
StrCpy $mode 1
Abort ; Skip page and start installing
${Else}
MessageBox mb_iconstop "Admin rights required!"
${EndIf}
${EndIf}
nsDialogs::Create 1018
Pop $0
${NSD_CreateRadioButton} 30u 20u 50% 12u "Run"
Pop $modeRadioRun
${NSD_OnClick} $modeRadioRun OnRadioChange
${NSD_CreateRadioButton} 30u 40u 50% 12u "Install for current user"
Pop $modeRadioInstCU
${NSD_OnClick} $modeRadioInstCU OnRadioChange
${NSD_CreateRadioButton} 30u 60u 50% 12u "Install for all users"
Pop $modeRadioInstLM
${NSD_OnClick} $modeRadioInstLM OnRadioChange
${NSD_CreateLink} 20u -14u 50% 12u "License"
Pop $0
${NSD_OnClick} $0 ShowLicense
${NSD_Check} $modeRadioRun
call OnRadioChange ; Trigger button change
nsDialogs::Show
FunctionEnd
Function ModePageLeave
${NSD_GetState} $modeRadioRun $0
${NSD_GetState} $modeRadioInstCU $1
${If} $0 = ${BST_CHECKED}
InitPluginsDir
SetOutPath $pluginsdir
File "myapp.exe"
ExecWait '"$pluginsdir\myapp.exe"'
SetOutPath $temp ; Don't lock $pluginsdir
Quit
${ElseIf} $1 = ${BST_CHECKED}
StrCpy $mode 0
${Else}
StrCpy $mode 1
UserInfo::GetAccountType
Pop $0
${If} $0 != "Admin"
ExecShell "runas" '"$exepath"' "/ELEVATEDINSTALL"
Quit
${EndIf}
${EndIf}
FunctionEnd
Function ShowLicense
ExecShell "" "http://example.com/license"
FunctionEnd
Page Custom ModePageCreate ModePageLeave
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES
!insertmacro MUI_LANGUAGE "English"
Section
${If} $mode > 0
SetShellVarContext all
StrCpy $InstDir "$ProgramFiles\MyApp"
${Else}
SetShellVarContext current
StrCpy $InstDir "$LocalAppData\Programs\MyApp"
${EndIf}
SetOutPath $InstDir
File myapp.exe
CreateShortcut "$SMPrograms\MyApp.lnk" "$InstDir\myapp.exe"
WriteUninstaller "$InstDir\Uninst.exe"
WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\MyAppGuid" "UninstallString" '"$InstDir\Uninst.exe"'
WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\MyAppGuid" "DisplayName" "MyApp blah blah"
SectionEnd
Section Uninstall
; Todo: Remove files and registry entries (You should write to a .ini in $InstDir so you know if it was a per user or machine install)
RMDir "$InstDir"
SectionEnd
You might want to edit the base UI to make the install button larger with Resource Hacker (on one of the files in NSIS\Contrib\UIs) and in the script use ChangeUI to apply.
My goal is to know if you use any check or code snipp to determine if ther is some dependecy when passing NSIS command line params to copiled setup using Silent installation ( /S param ).
The NSIS sample: http://nsis.sourceforge.net/Get_command_line_parameter_by_name
For example, If I have three params: Setup.exe /S param1="" param2="" param3=""
How to check the following secanrio:
${if} <Param1 is passed to Setup.exe>
<Param2 must ALSO be passed to Setup.exe>
${else}
<Error message notifiing that Param1 is present, but dependent Param2 param is missing in CMD parameters>
Thank you!
I really hope you will share at least code snipp ... if not whole functional code.
outfile test.exe
requestexecutionlevel user
silentinstall silent ;always force silent in this sample
!include LogicLib.nsh
!include FileFunc.nsh
Function StripOptPrefix
Exch $0
Push $1
StrCpy $1 $0 1
${If} $1 == "="
${OrIf} $1 == ":"
StrCpy $0 $0 "" 1
${EndIf}
Pop $1
Exch $0
FunctionEnd
!macro StripOptPrefix var
Push ${var}
call StripOptPrefix
Pop ${var}
!macroend
Section
${GetParameters} $0
${If} $0 == ""
;No parameters, lets run the tests
ExecWait '"$exepath" /param1=foo'
ExecWait '"$exepath" /param1=foo /param2=bar'
${Else}
${GetOptions} $0 "/param1" $1
${If} ${Errors}
# /param 1 not used, do nothing?
${Else}
${GetOptions} $0 "/param2" $2
${If} ${Errors}
MessageBox mb_iconstop "Missing /param2, required by /param1"
Quit
${Else}
!insertmacro StripOptPrefix $1
!insertmacro StripOptPrefix $2
MessageBox mb_ok "1=$1$\n2=$2"
${EndIf}
${EndIf}
${EndIf}
SectionEnd