Using Environment variable in registry for command

22,719

Solution 1

You should remember to make the registry value a REG_EXPAND_SZ instead of a REG_SZ. Normally, only REG_EXPAND_SZ values are expanded.

Solution 2

Don't forget that %ProgramFiles(x86)% will expand with spaces in the name so the path(s) needs to be enclosed in quotes. It might work like: "%ProgramFiles(x86)%\Feliratozo\Feliratozo.exe" "%1" Also, Frank Thomas is correct about %ProgramFiles(x86)% not working on 32bit Windows.

Variables are expanded based on the parent process though. %ProgramFiles% will expand to C:\Program Files (x86) on a 64bit installation if you use the 32bit console or regedit (Located in SysWOW64).

A simple solution:

Install.cmd:

IF EXIST %WinDIR%\SysWOW64\regedit.exe (
   %WinDIR%\SysWOW64\regedit.exe /s Install.reg
) ELSE (
   %WinDIR%\regedit.exe /s Install.reg
)

Install.reg:

Windows Registry Editor Version 5.00

[...\shell\myThing\command]
@="\"%ProgramFiles%\\Feliratozo\\Feliratozo.exe\" \"%1\""
Share:
22,719

Related videos on Youtube

Martin Fejes
Author by

Martin Fejes

I'm a Java and PHP developer. I mainly work on backend.

Updated on September 18, 2022

Comments

  • Martin Fejes
    Martin Fejes over 1 year

    I'm making a program which requires arguments from context menu (click a file and invoke my program with the file name as parameter).

    I'm trying to add a registry key to ...\shell\myThing\command. What I'd like to is the following:

    C:\Program Files (x86)\Feliratozo\Feliratozo.exe %1
    

    I'd like to use %ProgramFiles(x86)% environment variable, because I've read somewhere that it works on x86 and x64 Windows as well. (Changing to "normal" Program Files on x86.) The problem comes when I try to set:

    %ProgramFiles(x86)%\Feliratozo\Feliratozo.exe %1
    

    This way when I try to use the said context menu item, it gives the following error:

    Windows cannot access the specified device, path, or file. You may not have the appropriate permissions to access the item.
    

    The permission problem doesn't seem to be correct because when using absolute path it works.

    What can I do now?

    • Werner Henze
      Werner Henze about 11 years
      Did you remember to Change the registry Setting to be a REG_EXPAND_SZ instead of a REG_SZ!?
    • Frank Thomas
      Frank Thomas about 11 years
      By the way, part of your premise is faulty. on a 32bit PC, you cannot evaluate the env var %ProgramFiles(x86)%. you will recieve the message 'The system cannot find the path specified.'. Personally from a pathing perspective, its better to use regular old '%ProgramFiles%' since it will always resolve to a folder regardless of the bittedness of the host.
    • Martin Fejes
      Martin Fejes about 11 years
      Thank you both. Werner Henze, if you make an answer, I can accept it, this way I can't even give a Vote Up for your comment.
    • Werner Henze
      Werner Henze about 11 years
      @Martin Fejes: Made it an answer. Please only accept it if you tested it and it worked. I am not sure if Explorer is expanding environment variables in these registry keys at all.
  • Ian Kemp
    Ian Kemp almost 6 years
    If the value happens to be the (Default) key, you will find you cannot change its type via the Registry Editor GUI. To get around this, create the key manually: copy/save its current value, from an Admin PowerShell window/command prompt run reg.exe add <path-to-value> /ve /t REG_EXPAND_SZ /d "<value>". Voila, your (Default) key is now a REG_EXPAND_SZ!
  • smac89
    smac89 about 5 years
    @IanKemp don't forget to quote the "<path-to-value>" and remember the angle brackets are not part of the value