Archive: WriteRegStr @ HKLM on Win 7


WriteRegStr @ HKLM on Win 7
Hello, I am writing an application that needs to write to the HKLM/SOFTWARE section of the registry in Windows 7 and seem to be hitting a brick wall.

I have tried to request execution level user,admin and highest, neither seems to be letting NSIS write to HKLM.... Am I doing something wrong or missing an important step?

Thanks in advance! Here is a truncated version of the script I am trying to put together, you can see the WriteRegStr is just a test entry there for debugging purposes until I find out why it's not writing.



!include "Sections.nsh"
!include "LogicLib.nsh"

var /GLOBAL VER

Name "BSC Suite $VER"
BrandingText "Bayside Computer Shop"
OutFile "bayside-installer.exe"
RequestExecutionLevel admin
CRCCheck off
Icon hdup.ico

Page components
Page directory
Page instfiles

Section "Malware Bytes Anti-Malware" g2o7
SetOutPath "$INSTDIR\Anti-Malware"
File "bin\mbam-setup.exe"
WriteRegStr HKLM "SOFTWARE\My Software" "" $INSTDIR
SectionEnd


Function .onInit
StrCpy $VER 10.4.27
StrCpy $INSTDIR "$DOCUMENTS\tech\User Software"
FunctionEnd


Function .onInstSuccess
MessageBox MB_YESNO|MB_ICONQUESTION "The computer has to reboot before any$\r$\nof the software you selected is$\r$\ninstalled.$\r$\n$\r$\nDo you wish to reboot the system?" IDNO +2
Reboot
FunctionEnd




I am not completely familiar with running NSIS on 7/Vista but I assume it is UAC that is blocking your installer. You can try the UAC plug-in: http://nsis.sourceforge.net/UAC_plug-in

Stu


I currently have UAC disabled on this 7 machine while I test stuff, I read the documentation/ReadMe included with that UAC plugin but I do not see any functions to write to the registry, I loaded it in my script and tried compiling/running the script again and it still does not write to HKLM, but it will write to HKCU when I use the same syntax.


you should be okay, UAC-wise, when requesting admin level; unless the key is set read-only somewhere.

Are you sure that NSIS isn't allowed to write to the registry in that location? How are you verifying this?

Just to throw one possible thing out there... a lot of Win7 machines are going to be 64bit. If you don't use SetRegView to switch to the 64bit registry, NSIS will actually end up writing to the 32bit registry at HKEY_LOCAL_MACHINE\Software\WOW6432node\My Software\. So if you are checking behavior by simply looking for HKEY_LOCAL_MACHINE\Software\My Software\, you're not going to be finding it.


Very nice, I think that was it (SetRegView) ... My entries are showing under the WOW6432node key. I'll look up the SetRegView routine, I'm assuming there is a way to detect if you are running 64 bit and to switch views appropriately. Thank you so much for your help, you saved me a serious headache consindering all our windows 7 machines are 64 bit.


yep - see the included 'x64.nsh' for some LogicLib-enabled macros you can use. in short:


${If} ${RunningX64}
; 64bit things here
${Else}
; 32bit things here
${EndIf}


However... you shouldn't be writing to the 64bit registry unless you - or rather the application you're installing - need(s) it.

I.e. if you are installing a 32bit application, it makes sense to just let your installer write to the 32bit registry as well - especially if the installer writes out information that your application needs, as it will be subject to the same registry redirection.

Similarly, if you are installing a 64bit application, that's when you'd want to use SetRegView to toggle to the 64bit registry.
( also keep in mind that there are separate 64bit handlers for $PROGRAMFILES and the like, and that the Windows\System32\ folder gets redirected; have a search through the forums for details.. not sure there's a wiki entry on all of this yet. )

Thanks a bunch, the reason I need to write to the real HKLM is for RunOnceEx (HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnceEx in order to install applications on the next boot,) so I'm pretty sure it has to be the real 64 bit HKLM that I need to write to. I'll look into x64.nsh and SetRegView when I get off of work, thanks for all the help and information!