Question

In my batch file, I am doing a simple command to get the VirtualBox install path.

REG QUERY HKEY_LOCAL_MACHINE\SOFTWARE\Oracle\VirtualBox /v InstallDir

It works wonders when I execute the batch file from the command line, but no value is given if I launch that same batch file from Inno Setup.

After a long while of testing, I noticed it was searching in the 32 bit section of the registry only when the batch file was launched from Inno Setup.

To test that theory, I looked at a 32 bit Registry located there:

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\NuGet\Repository

In my batch file, I enter the following command (notice that I don't specify the Wow6432Node, which should be wrong)

REG QUERY HKEY_LOCAL_MACHINE\SOFTWARE\NuGet\Repository\

Indeed, when running the batch file from the command line, it does not find the registry key. However, when that same batch file is launched from Inno Setup, it finds \NuGet\repository just fine.

The question now is. How do I get the 64 bit registry key (Virtual Box) with my batch file so that it works from both the command line and Inno Setup?

Was it helpful?

Solution

This is what I ended up doing since I did not have any more time to put on it. It works for all cases, except when a Virtual Box 64 bit is installed in a folder that is not the default one and that the batch file is called in a 32 bit environment.

set vb_path=""

for /f "tokens=1-2*" %%A in ('REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Oracle\VirtualBox" /v InstallDir') do (
  set vb_path="%%C\VBoxManage.exe"
)

IF %vb_path%=="" (
  REM Virtual Box not found. Search in 32 bit virtual box from 64 bit cmd
  for /f "tokens=1-2*" %%A in ('REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Oracle\VirtualBox" /v InstallDir') do (
    set vb_path="%%C\VBoxManage.exe"
  )
)

IF %vb_path%=="" (
  REM Still not found. Must be 32 bit env. with 64 bit Virtual Box
  REM Search in default installation folders.
  IF EXIST "%ProgramFiles(x86)%\Oracle\VirtualBox\VBoxManage.exe" (
    set vb_path="%ProgramFiles(x86)%\Oracle\VirtualBox\VBoxManage.exe"
  ) ELSE (
    IF EXIST "%ProgramW6432%\Oracle\VirtualBox\VBoxManage.exe" (
      set vb_path="%ProgramW6432%\Oracle\VirtualBox\VBoxManage.exe"
    )
  )
)

IF %vb_path%=="" (
  ECHO VirtualBox folder not found in registry or default installation path!
  EXIT /b 1
)

OTHER TIPS

You can call something like

REG QUERY HKLM\SOFTWARE /reg:32

or

REG QUERY HKLM\SOFTWARE /reg:64

to explicit target the registry for 32bit or 64bit.

This is not mentioned when calling REG /?, but I found it here: https://support.microsoft.com/en-us/help/948698/you-cannot-use-the-reg-exe-utility-to-access-64-bit-registry-keys-on-a
I am using this on Windows 7 Pro 64bit.

If I wanna know using batch files, Windows version x86 or x64, I use simple script (not tested at Inno Setup)

@echo off
rem DISCOVERY WINDOWS VERSION x86 or x64

reg query HKLM\Hardware\Description\System\CentralProcessor\0 | find /i "x86" > nul 2>&1
if %errorlevel% equ 1 goto x64

rem DISPLAY OS NAME AND VERSION 

:x86
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
SET CURRENTKEY=HKLM\Software\Microsoft\Windows NT\CurrentVersion
FOR /F "TOKENS=2,*" %%A IN ('reg query "%CURRENTKEY%" 2^>NUL^|FIND /I "ProductName"') DO ECHO                       %%B 32 Bits found...
echo.
ping -n 5 localhost >nul 
cls
goto systemx86

:x64  
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
SET CURRENTKEY=HKLM\Software\Microsoft\Windows NT\CurrentVersion
FOR /F "TOKENS=2,*" %%A IN ('reg query "%CURRENTKEY%" 2^>NUL^|FIND /I "ProductName"') DO ECHO                       %%B 64 Bits found...
echo.
ping -n 5 localhost >nul 
cls
goto systemx64

rem YOUR SCRIPTS BELOW

:systemx86
echo Start your commands for x86! by Iremar Domingos
pause

:systemx64
echo Start your commands for x64! by Iremar Domingos
pause
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top