Skip to content
⌘ NSIS Forum Archive

ReadRegStr

4 posts

MysticEmpires2k#

ReadRegStr

I have a littel problem.I want read the install dir from the reg.But i get a "" in the installdir.Waht is wrong.

Function GetMyInstPath
ReadRegStr $0 HKCU "Software\Mysoft" "MyDir"
FunctionEnd


; The default installation directory
InstallDir $0

; The text to prompt the user to enter a directory
DirText "This will install the very simple example1 on your computer. Choose a directory"

; The stuff to install
Section "ThisNameIsIgnoredSoWhyBother?"
; Set output path to the installation directory.
SetOutPath $INSTDIR
; Put file there
File "C:\windows\notepad.exe"
SectionEnd ; end the section

; eof


I'am a beginner and my english is not very god.
kichik#
A function doesn't get called by itself, you must call it from a section or a callback function. Anyway, you don't need to do it yourself, just use InstallDirRegKey.
MysticEmpires2k#
Ok thx.But i have a problem to i have crate this on c++

CRegKey key;
if( key.Open( HKEY_CURRENT_USER, "Software\\mysoft") == ERROR_FILE_NOT_FOUND)
{
fprintf(fp,"InstallDirRegKey HKEY_LOCAL_MACHINE `SOFTWARE\\mysoft` `MyDir`\n");
}
else
{
fprintf(fp,"InstallDirRegKey HKEY_CURRENT_USER `Software\\mysoft1` `MyDir`\n");
}

but i can't srcibt it.
kichik#
If you have two possible install directories, manage them in .onInit.

Function .onInit
  ReadRegStr $INSTDIR HKLM "Software\\mysoft1" "myDir"
  IfErrors 0 done
    ; if we couldn't read the reg key read the second one
    ReadRegStr $INSTDIR HKLM "Software\\mysoft2" "myDir"
    IfErrors 0 done
      ; if we can't read this one either just use default
      StrCpy $INSTDIR "$PROGRAMFILES\MyProg"
  done:
FunctionEnd 
InstallDirRegKey makes the installer read from the specified registry key when the it starts, and if can't read, just return to the default InstallDir.

.onInit is a callback function that the installer calls before it shows any dialogs, so you can initialize stuff in there such as the installation directory.