Skip to content
⌘ NSIS Forum Archive

Registry check for an existing program

7 posts

moho9#

Registry check for an existing program

First, I apologize in advance for my ignorance.

I've spent hours researching my issue on the web and trial-and-erroring, but have been unable to get this to work. I'm not a programmer, and never have so much as written a .bat file. It's been like trying to learn quantum physics from textbook written in Chinese (I don't speak Chinese).

Anyway, I'm trying to check for an existing installation of my "program" (video game mod), so I can abort the install if it's already present (regardless of version). I've tried a couple variations of the function below, but I can't get it to work. I keep getting an error on the "ReadRegStr" line, but according to my installer (partially generated in HM NIS Edit), what I've defined here is what it's supposed to be written to the registry. I've also read there can be an issue with 64-bit Windows, but have no idea if that is related to my issue.

;Check for Previous Installation of Program
Function .onInit
ClearErrors
ReadRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayName" "$(^Name)"
StrCmp $2 ""
MessageBox MB_OK "This program is already installed. Please uninstall before trying again."
Abort
FunctionEnd
Taking into consideration the following parameters that HM NIS Edit Wizard generated for me:

!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
!define PRODUCT_UNINST_ROOT_KEY "HKLM"

Section -Post
WriteUninstaller "$INSTDIR\uninst.exe"
WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayName" "$(^Name)"
WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "UninstallString" "$INSTDIR\uninst.exe"
WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "${PRODUCT_VERSION}"
SectionEnd
Note, I've been trying to follow certain examples set on these pages, trying to keep the script as simple as possible:

Are you a developer who uses NSIS to distribute your application? Are you a Winamp plug-in developer who wants to use NSIS to distribute your plug-in? Have suggestions for other people like you? This is the place.


If anyone is willing to take the time to help, I would very much appreciate it.

Thanks much.
Afrow UK#
Function .onInit
ClearErrors
ReadRegStr $R0 ${PRODUCT_UNINST_ROOT_KEY} `${PRODUCT_UNINST_KEY}` DisplayName
${IfNot} ${Errors}
MessageBox MB_OK|MB_ICONEXCLAMATION `This program is already installed. Please uninstall before trying again.`
Abort
${EndIf}
FunctionEnd
You were not using ReadRegStr correctly (its first argument must be a variable which receives the value).

Stu
Anders#
When you get a compiler error it generally means you used the wrong syntax. When this happens you should read about the function you are having trouble with in the manual...
moho9#
Originally Posted by Afrow UK View Post
Function .onInit
ClearErrors
ReadRegStr $R0 ${PRODUCT_UNINST_ROOT_KEY} `${PRODUCT_UNINST_KEY}` DisplayName
${IfNot} ${Errors}
MessageBox MB_OK|MB_ICONEXCLAMATION `This program is already installed. Please uninstall before trying again.`
Abort
${EndIf}
FunctionEnd
You were not using ReadRegStr correctly (its first argument must be a variable which receives the value).

Stu
Stu, thank you so much for providing the code like this! Not to mention explaining what the problem was. This worked perfectly. Hopefully this will help other people like me who are also using some pre-generated HM NIS script :]

Originally Posted by Anders View Post
When you get a compiler error it generally means you used the wrong syntax. When this happens you should read about the function you are having trouble with in the manual...
Yeah, I knew something was wrong in regards to that, I just couldn't figure out how to fix it. I've read most, if not all, of the manual at this point (not to mention lots of random posts in various places, the wiki, etc). It's just that the concept of variables and "arguments" (these especially) is very new to me -- and difficult to understand. I've figured several other things out on my own, but much of it I'm not getting.

At any rate, I'm incredibly grateful that there are people around this forum willing to donate their time and share their knowledge. Thank you, again.
JasonFriday13#
Originally Posted by moho9 View Post
It's just that the concept of variables and "arguments" (these especially) is very new to me -- and difficult to understand.
Basically, variables are places to store the data. NSIS has inbuilt variables, some are $0 to $9, and $INSTDIR. These can be changed.

'Defines' are different. They start with '${' and end with '}', like ${NSISDIR} (the directory where NSIS is installed). These cannot be changed.

Arguments are extra bits of information given to a function. If you used 'Name' in your script, the argument is the string after it: Name "my program" (one argument). Some have no arguments, like ClearErrors. Others have several arguments, and sometimes they are optional too, like StrCmp (the final argument is optional).

So you could have: StrCmp $0 "foo string" +2 (which skips the next line if the strings are the same, 3 arguments), or: StrCmp $0 "another foo string" 0 +5 (which doesn't skip lines if the strings are the same, and skips 5 lines if they are not the same, 4 arguments). These are 'in' arguments.

There are functions with an 'out' argument, the most familiar one would be StrCpy. This copies a string into a variable.
Example: StrCpy $0 "foo string". So if you used that and DetailPrint "$0" in a section, it would display "foo string".

Using LogicLib is a good way of making your code easier to read (what Afrow UK did).

Hope this helps.
moho9#
@JasonFriday --

It does help, actually. It's still confusing, but what you have here makes more sense than the NSIS manual, for example, lol.

Appreciate you taking the time to explain things in simpler terms and provide examples. Very useful 🙂

BTW, I was wondering if it might be useful to post my script on the wiki? As a complete novice, it's been difficult to start with what the HM NIS wizard gave me and then work off of that to include several other things. It took 2-3 weeks of research and hundreds of trials and errors -- especially since HM NIS won't run the script on my x64 Win7. I had to actually launch the .exe every time to test it. Are any NSIS users allowed to post on there? Is permission required?
JasonFriday13#
You can register an account on the wiki (my wiki page is in my signature below), but really it's for tools and other mods to nsis. Anyway, there's plenty of example scripts in the NSIS\Examples directory explaining the basics. I recently made a test script that attempts to use nearly all the functions nsis has to offer, it's over 1200 lines long. I used it to test the Linux build of makensis that I was working on at the time (that's basically finished now).