CreateDirRequest - changing path manually - nsis

In my code I use NSD_CreateDirRequest to set path to folder. When I use browse button it work good. But when I change directory manually it doesn't work.
Ex.
I choose this directory using Browse button:
C:\Users\User\Desktop\Folder
And when I change directory manually to:
C:\Users\User\Desktop\Folder2
And press next, it't still old directory: C:\Users\User\Desktop\Folder
How to fix it?
My code:
${NSD_CreateDirRequest} 0 22u 84% 12u "Choose directory"
Pop $Dir
${NSD_SetText} $Dir $DirApp
${NSD_CreateBrowseButton} 85% 22u 15% 12u "Choose"
Pop $BrowseButton
${NSD_OnClick} $BrowseButton OnBrowseForDir
Function OnBrowseForDir
nsDialogs::SelectFolderDialog /NOUNLOAD "Directory"
Pop $0
${If} $0 == error
${Else}
StrCpy $DirApp $0
${NSD_SetText} $Dir $DirApp
${EndIf}
FunctionEnd

You need to read explicitely the value of the directory, for example in a page leaving callback:
Page custom CustomPageName LeaveCallback
# ...
Function LeaveCallback
${NSD_GetText} $Dir $DirApp
FunctionEnd

Related

nsis - remove pinned icon from taskbar on uninstall

I'm working on an app for a company and they requested that if the app is pinned to the taskbar, when uninstalling the app should be unpinned. If I just delete the icon from quicklaunch\user pinned\taskbar then it leaves a blank icon on the taskbar.
I need to somehow actually unpin it. The only thing I've come across is installing winshell plugin (http://nsis.sourceforge.net/WinShell_plug-in) and then calling IStartMenuPinnedList::RemoveFromList (https://msdn.microsoft.com/en-us/library/windows/desktop/bb774817(v=vs.85).aspx)
I'd rather not install a plugin if I don't need to. Does anyone have any suggestions?
NSIS does not have native support for this interface so you have to use a plug-in. If you want to avoid that 3rd-party plug-in (written by me) then you can use the System plug-in instead:
!include "LogicLib.nsh"
!include "Win\COM.nsh" ; NSIS v3+
!macro UnpinShortcut lnkpath
Push $0
Push $1
!insertmacro ComHlpr_CreateInProcInstance ${CLSID_StartMenuPin} ${IID_IStartMenuPinnedList} r0 ""
${If} $0 P<> 0
System::Call 'SHELL32::SHCreateItemFromParsingName(ws, p0, g "${IID_IShellItem}", *p0r1)' "${lnkpath}"
${If} $1 P<> 0
${IStartMenuPinnedList::RemoveFromList} $0 '(r1)'
${IUnknown::Release} $1 ""
${EndIf}
${IUnknown::Release} $0 ""
${EndIf}
Pop $1
Pop $0
!macroend
Section Uninstall
!insertmacro UnpinShortcut "$SMPrograms\MyApp.lnk"
Delete "$SMPrograms\MyApp.lnk"
SectionEnd

NSIS uninstaller privileges according to installer

I have an NSIS installer for an application, which can be run as normal user. But if the user wants to install into the "Program Files" directory, it can still be accomplished by starting the installer with administrator privileges.
Now I have the problem, that the uninstaller is started with user privileges by default, even if the installation took place as administrator. This causes the uninstallation to silently fail. Even worse: It even states that the uninstall process was successful without being able to delete any files.
My question is: Is it possible to create an uninstaller during the installation, which requires (or better: requests itself with) the same privileges as the installation process?
You would have to implement this check yourself. You can check if you are admin in the installer with the UserInfo plugin and then store the result in a .ini, the registry or append the info to the uninstaller.exe:
InstallDir $temp\instdir
Section
UserInfo::GetAccountType
Pop $0
StrCmp $0 "Admin" 0 +2
StrCpy $0 1
IntOp $0 $0 & 1 ; $0 is now 1 if admin or 0 if not
SetOutPath $InstDir
WriteUninstaller "$InstDir\Uninstall.exe"
FileOpen $1 "$InstDir\Uninstall.exe" a
FileSeek $1 0 END
FileWriteByte $1 $0
FileClose $1
SectionEnd
Section Uninstall
FileOpen $1 "$ExePath" r
FileSeek $1 -1 END
FileReadByte $1 $0
FileClose $1
DetailPrint "Installer was admin: $0"
SectionEnd

nsDialogs invalid command

I have a problem with my NSIS script in which i try to a custom page where the user insert a ClientId through the use of a textbox.
The problem I get so far is that my compiler gives my this error:
Invalid command: nsDialogs::Create Error in script "C:*Directory*\Installer.nsi" on line 35 -- aborting creation
process
I have included both the nsDialogs.nsh and LogicLib.nsh in my script.
!include nsDialogs.nsh
!include LogicLib.nsh
Var Dialog
Var Label
Var Text
Name "Installer"
OutFile "Installer.exe"
InstallDir $PROGRAMFILES\MyProject
RequestExecutionLevel admin
Page directory
Page custom nsDialogsPage nsDialogsPageLeave
Page instfiles
UninstPage uninstConfirm
UninstPage instfiles
Section Installer
SetOutPath $INSTDIR
File /r "*Release Folder*"
WriteUninstaller "uninstall.exe"
Call InstallService
SectionEnd
Function nsDialogsPage
nsDialogs::Create 1018
Pop $Dialog
${If} $Dialog == error
Abort
${EndIf}
${NSD_CreateLabel} 0 0 100% 12u "Hello, welcome to nsDialogs!"
Pop $Label
${NSD_CreateText} 0 13u 100% -13u "Type something here..."
Pop $Text
nsDialogs::Show
FunctionEnd
I get the exact same error when I try to compile the example scripts found in Examples/NSIS.
The include files do exists, and is in the same folder as the compiler is reading them from. Is there a library file needed which is not included in the installtion of NSIS?
Thanks
It sounds to me like the plugin is missing. Make sure you have a nsDialogs.dll in the plugin directory...

NSIS AccessControl Plugin disable access to directory for all except Admins and System

I just read bunch of answers about setting permissions to directory by NSIS AccessControl plugin, but all those only show basic usage copy pasted from plugin site... It only shows cases ALL or NOTHING... but how do I disable ALL permissions to directory for everyone except System and Administrators?
AccessControl::DisableFileInheritance "$temp\test.tmp"
Pop $0
DetailPrint $0
AccessControl::ClearOnFile "$temp\test.tmp" "(S-1-5-18)" "FullAccess"
Pop $0
DetailPrint $0
AccessControl::SetOnFile "$temp\test.tmp" "(S-1-5-32-544)" "FullAccess"
Pop $0
DetailPrint $0
This might not be enough, you should probably also use SetFileOwner and maybe SetFileGroup...

Set value of InstallDir in a function, or set auto populate value somehow?

I'm creating an installer using NSIS. This installer actually installs two programs in two different directories in the same installer. I am doing this using the modern user interface (MUI) pages and simply calling MUI_PAGE_DIRECTORY twice specifying different starting parameteres, and capturing the directory in the LEAVE macro. What I'm wondering is, can I somehow call InstallDir in a function, or set the auto directory populate value in a function? Or possibly even call a function after the browse button has been returned from?
The reason I want to do this is so when the user clicks the browse button in either of the two directory pages, after they select a directory, the name of the finnal directory specifed in InstallDir will be appended.
For example:
InstallDir value for program 1: c:\client
InstallDir value for program 2: c:\program files\server
user clicks browse on program 1 and chooses c:\temp the resulting path is c:\temp\client
user clicks browse on program 2 and chooses c:\whatever the resulting path is c:\whatever\server
For reference here are the code snipits of what I have that works, but does not deal with the auto append browse button behaviour:
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE ClientDirectoryLeave
!insertmacro MUI_PAGE_DIRECTORY
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE ServerDirectoryLeave
!insertmacro MUI_PAGE_DIRECTORY
; Setup the page display for the client install page
Function ShowPageClient
!insertmacro MUI_HEADER_TEXT "Client" "Client"
!insertmacro MUI_INNERDIALOG_TEXT 1006 "Client"
; setup intal directory
Push $0
StrCpy $0 $PROGRAMFILES 2 #
; CLIENT_FOLDER_NAME is defined as a folder, but this would basicaly
; result in C:\Client as the first 2 characters of $PROGRAMFILES
; is the hard drive with program files installed on it
StrCpy $INSTDIR "$0\${CLIENT_FOLDER_NAME}"
Pop $0
; set the inital value of the directory text box
!insertmacro MUI_INNERDIALOG_TEXT 1019 $INSTDIR
; find and disable the directory selection box
; We do not want users to type in this box
FindWindow $R0 "#32770" "" $HWNDPARENT
GetDlgItem $R1 $R0 1019 ;Text Box
EnableWindow $R1 0
FunctionEnd
; Setup the page display for the server install location page
Function ShowPageServer
!insertmacro MUI_HEADER_TEXT "Server" "Server"
!insertmacro MUI_INNERDIALOG_TEXT 1006 "Server"
; setup intal directory
; SERVER_FOLDER_NAME is defined as a folder, but this would basicaly
; result in C:\Program Files\Server
StrCpy $INSTDIR "$PROGRAMFILES\${SERVER_FOLDER_NAME}"
; set the inital value of the directory text box
!insertmacro MUI_INNERDIALOG_TEXT 1019 $INSTDIR
; find and disable the directory selection box
; We do not want users to type in this box
FindWindow $R0 "#32770" "" $HWNDPARENT
GetDlgItem $R1 $R0 1019 ;Text Box
EnableWindow $R1 0
FunctionEnd
Note: I can make the browse button work for one of the directory pages, but then when I'm on the second page, the auto populate actual auto populates incorrectly
The appended folder name is constant and set at compile time, there is a bug report related to this.
My advice is to abandon the append feature and let the user have full control over the two destinations:
Name "NSIS Test"
InstallDir ""
!include MUI.nsh
Var DirClient
Var DirServer
Function .onInit
;Set default destinations
StrCpy $DirClient "$ProgramFiles\$(^Name)\Client"
StrCpy $DirServer "$ProgramFiles\$(^Name)\Server"
FunctionEnd
!macro ConfigureMyDirPage type var
!define MUI_DIRECTORYPAGE_VARIABLE ${var}
!define MUI_PAGE_HEADER_SUBTEXT "Choose the folder in which to install $(^NameDA) ${type}"
!define MUI_DIRECTORYPAGE_TEXT_TOP "Setup will install $(^NameDA) ${type} in the following folder. To install in a different folder, click Browse and select another folder. $_CLICK"
!define MUI_DIRECTORYPAGE_TEXT_DESTINATION "${type} $(^DirSubText)"
!macroend
!insertmacro ConfigureMyDirPage "Client" $DirClient
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro ConfigureMyDirPage "Server" $DirServer
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE "English"
Section
DetailPrint DirClient=$DirClient
DetailPrint DirServer=$DirServer
SectionEnd
Okay I finally figured it out. Basically there is a function that is called to "verify" the path after browse button is clicked. I tied into this function an appended the directory manual if needed. To do this I created a new variable and set it in a function called when the page is displays as follows:
; Client Directory
!define MUI_PAGE_CUSTOMFUNCTION_SHOW ShowPageClient
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE ClientDirectoryLeave
!insertmacro MUI_PAGE_DIRECTORY
; Setup the page display for the client install page
Function ShowPageClient
; setup intal directory
Push $0
StrCpy $0 $PROGRAMFILES 2 #
StrCpy $INSTDIR "$0\${CLIENT_FOLDER_NAME}"
Pop $0
!insertmacro MUI_INNERDIALOG_TEXT 1019 $INSTDIR
FindWindow $R0 "#32770" "" $HWNDPARENT
GetDlgItem $R1 $R0 1019 ;Text Box
EnableWindow $R1 0
; Setup the client or server variable to indicate that
; we're in the client install part to signal the .onVerifyInstDir function
; to put the correct directory
StrCpy $CLIENT_OR_SERVER "client"
FunctionEnd
The function that is called after browse is .onVerifyInstDir so that is where I check the CLIENT_OR_SERVER variable and set the path appropriately
; This function is special and is called any time a
; path is validated on returning from the browse button
; need to append the correct directory if it does not already exists
; in the install directory path
Function .onVerifyInstDir
; save the current $0 register, as it is used in this function
Push $0
${If} $CLIENT_OR_SERVER == "client"
; in the client stage so directory must contain CLIENT_FOLDER_NAME
${StrContains} $0 "${CLIENT_FOLDER_NAME}" "$INSTDIR"
${If} $0 == ""
; the install dir does not contain the folder so append it
StrCpy $INSTDIR "$INSTDIR\${CLIENT_FOLDER_NAME}"
${EndIf}
${Else}
; in the server stage so directory must contain SERVER_FOLDER_NAME
${StrContains} $0 "${SERVER_FOLDER_NAME}" "$INSTDIR"
${If} $0 == ""
; the install dir does not contain the folder so append it
StrCpy $INSTDIR "$INSTDIR\${SERVER_FOLDER_NAME}"
${EndIf}
${EndIf}
; pop the saved register value
Pop $0
FunctionEnd
Couple notes:
the StrContains function I used was found here:
http://nsis.sourceforge.net/StrContains
Further reference to the .onVerifyInstDir function can be found here:
http://nsis.sourceforge.net/Docs/Chapter4.html#4.7.2.1.10

Resources