Search code examples
windows-installerinstallshieldinstallscriptinstallscript-msi

Abort the Install Shield installation if specific key exists in registry


During setup.exe installation if a particular key exists in registry i want to abort the installation.To achieve this,i am calling install-script function using custom action in install shield project. Install Script code is :

function MyFunction(hMSI)
    // To Do:  Declare local variables.
    STRING szKey;
    NUMBER nRootKey;

begin
// Set the root key to HKEY_LOCAL_MACHINE.
    nRootKey = HKEY_LOCAL_MACHINE;

    if (RegDBSetDefaultRoot (nRootKey) < 0) then
        MessageBox ("First call to RegDBSetDefaultRoot failed.", SEVERE);
    else
        MessageBox ("Root key successfully set to HKEY_LOCAL_MACHINE.",
                   INFORMATION);
    endif;

szKey = "SOFTWARE\\Test";

if (RegDBKeyExist (szKey)< 0) then
MessageBox ("Test is not present", SEVERE);
abort;
endif;

if (RegDBKeyExist (szKey)= 1) then
MessageBox ("Test is present", SEVERE);
abort;
endif;

    // To Do:  Write script that will be executed when MyFunction is called.

end;

Every time i am getting message "Test is not present" even though key "HKEY_LOCAL_MACHINE\SOFTWARE\Test" is present in registry.

I think i did some what wrong in script or missing something. Please help on this.

Referred link: http://helpnet.installshield.com/installshield19helplib/Subsystems/installshield19langref/helplibrary/LangrefRegDBKeyExist_example.htm#Langref_appendixD_3271668955_1023535

https://community.flexerasoftware.com/showthread.php?139026-Check-if-a-registry-key-exists


Solution

  • It's not 100% confirmed in your question, but I would give strong odds that:

    1. you are testing on a 64-bit installation of Windows,
    2. you are creating and visually verifying the registry key using C:\Windows\System32\Regedt32.exe or equivalent, and
    3. you aren't aware that the InstallScript engine runs as a 32-bit process, or aren't aware of the implications.

    If that's all true, the problem is that you have created the key HKEY_LOCAL_MACHINE\SOFTWARE\Test, but your code is checking HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Test instead, due to the registry redirector. To fix this, you should do one of the following:

    • Test a key in the 32-bit view (for example, launch C:\Windows\SysWow64\regedt32.exe); this will let you use the name HKEY_LOCAL_MACHINE\SOFTWARE\Test to mean the redirected location.
    • Change your code to request a 64-bit view of the registry by locally altering REGDB_OPTIONS to include REGDB_OPTION_WOW64_64KEY. While that flag is specified, InstallScript registry functions will not be redirected. Don't forget to set REGDB_OPTIONS back to its prior value.
    • If your project is MSI-based, consider using system searches and type 19 "error" custom actions instead

    Note that if this key is created by software outside of your control, you will need to ensure that you are checking for the right location. That will influence whether the first or second bullet is a better choice for your situation. (If the key is fully under your control, you should also think about whether it's using the right location, and change it if it is not.)