Different PATH environment variable for 32bit and 64bit Windows - is it possible?

14

4

Is it possible to have whole or part of PATH environment variable specific to the type of running process's image (32bit/64bit)? When I run some app from within 64bit cmd.exe I would like to have it pick the 64bit version of OpenSSL library whereas when I run some app from within 32bit cmd.exe I would like to have it pick the 32bit version of OpenSSL library.

FOLLOW UP
where.exe does not find OpenSSL libs when %ProgramFiles% variable is used in the PATH environment variable

Piotr Dobrogost

Posted 2011-02-18T20:06:33.653

Reputation: 4 413

Answers

9

Make %ProgramFiles% to %ProgramFiles(x86)% env variable switching to work for you:

Place folders with x32 and x64 versions of OpenSSL library into appropriate %programfiles% and %ProgramFiles(x86)% directories and in the PATH environment variable, use a reference to these folders via the %programfiles% variable.

This way, when you are running in x32-bit environment, your PATH entry %programfiles%/OpenSSL/ will automatically get resolved to %ProgramFiles(x86)%/OpenSSL/ on a disk.

romka

Posted 2011-02-18T20:06:33.653

Reputation: 136

1Well I have some trouble getting it to work. echo %programfiles% shows different path depending on the type of cmd.exe it's run from but where ssleay32.dll in both types of cmd.exe (32bit and 64bit) can't find this dll and displays INFO: Could not find files for the given pattern(s). Any ideas? – Piotr Dobrogost – 2011-02-19T10:13:33.607

This might help: Altough this might help: http://stackoverflow.com/questions/906310/installing-registering-win32-openssl-libraries-distributed-with-my-app

– Darokthar – 2011-02-19T14:57:32.717

1if one of the dlls is a 32-bit one, on 64-bit machine it should go into C:\windows\syswow64 folder – romka – 2011-02-20T13:21:59.057

This does not work for me. When I include %ProgramFiles% in the PATH variable definition, it does not get expanded at all, so my exe doesn't find its dlls. – Carlos A. Ibarra – 2014-05-27T23:03:32.167

7

The answer (checked as right) provided by romka is simple and elegant, but does unfortunately not work (at least on Windows 7 and Windows 8 64 bits, I didn't push my test further).

The problem comes from the fact that the system %PATH% variable does not always expand other env variable : it works with %SYSTEMDRIVE% for example, but unfortunately not for %PROGRAMFILES%. Wikipedia suggests that this behavior comes from the level of indirection (%SYSTEMDRIVE% does not refer to a third env variable).

The only solution I found is to use the File System Redirector magic and the directories System32/SysWoW64, as suggested in the comments.

To avoid the direct deployment of DLLs in the Windows directory, which is usually hard to maintain, one can deploy instead a softlink to a custom directory (works on Windows Vista and later versions of Windows) :

By the way, sorry for not commenting directly on the relevant posts : currently not enough reputation on my account to do this.

Baptiste Chardon

Posted 2011-02-18T20:06:33.653

Reputation: 71

5

Yes it is absolutely possible. Simply write a three .bat files. The first one should look like this:

@echo off
if "%1" == "" goto x86
if not "%2" == "" goto usage

if /i %1 == x86 goto x86
if /i %1 == ia64 goto ia64
goto usage

:x86
if not exist "%~dp0bin\x86.bat" goto missing
call "%~dp0bin\x86.bat"
goto :eof

:ia64
if not exist "%~dp0bin\ia64.bat" goto missing
call "%~dp0bin\ia64.bat"
goto :eof

:usage
echo Error in script usage. The correct usage is:
echo %0 [option]
echo where [option] is: x86 ^| ia64
echo:
echo For example:
echo %0 x86
goto :eof

:missing
echo The specified configuration type is missing. The tools for the
echo configuration might not be installed.
goto :eof

The second and the third .bat file are basically the same, except they differ in their name. The first will be called x86.bat the second ia64.bat and they are placed in a folder called bin which is above the the first bat file. You will have this:

PATH\first.bat
PATH\bin\x86.bat
PATH\bin\ia64.bat

The content of the second and third .bat file should look like this:

@set PATH=THE PATH YOU WANT

You could create a link to first .bat file which will have the following settings:

Target: %comspec% /k "PATH\first.bat" OPTION | Where OPTION is x86 or ia64

Start in: PATH | Where PATH is the PATH to your first.bat

The script is the simplified script Microsoft uses to start the right command line for their Visual Studio environment. You could simply expand this scripts to N environments. By adding more .bat files for different environments and by editing the first.bat with more options and goto statements. I hope it is self explaining.

And i hope Microsoft does not sue me for using their script.

EDIT:

Ah i think i misunderstood you a bit. For the 32bit cmd line the link should be created as:

Target: %windir%\SysWoW64\cmd.exe "PATH\first.bat" x86

EDIT2:

Try something like:

if "%ProgramFiles%" == "%ProgramFiles(x86)%" goto x64_PATH
if "%ProgramFiles%" == "%ProgramW6432%" goto x86_PATH

:x64_PATH
@set PATH=YOUR 64 bit PATH
SOME_PATH\your64BitApp.exe
goto :eof

:x86_PATH
@set PATH=YOUR 32bit PATH
SOME_PATH\your32BitApp.exe
goto :eof

Darokthar

Posted 2011-02-18T20:06:33.653

Reputation: 1 361

1You might want to correct that, just for clarity - odds are, they are not using Intel 64 bit technology (ia64 - Itanium CPUs) but rather AMD64 bit technology, commonly referred to as x64. – Multiverse IT – 2011-02-18T21:59:27.680

Thanks for your answer. The idea is nice. However I was looking for some system level solution like the one used to modify %ProgramFiles% variable. (Quote: The %ProgramFiles% itself depends on whether the process requesting the environment variable is itself 32-bit or 64-bit (this is caused by Windows-on-Windows 64-bit redirection). http://en.wikipedia.org/wiki/Environment_variable#Discrete_value_variables)

– Piotr Dobrogost – 2011-02-18T22:01:52.480

1

I wanted just to summarise the answer I obtained by following the links provided in the answer from Baptiste Chardon. By using the mklink command line tool to create a directory symbolic link in C:\Windows\system32 and in C:\Windows\SysWOW64, each having the same name (though different targets), you can then just add the one in C:\Windows\system32 to the Path environment variable. For example:

C:\> mklink /D C:\Windows\SysWOW64\my_XXbit_dlls C:\dlls\x86
symbolic link created for C:\Windows\SysWOW64\my_XXbit_dlls <<===>> C:\dlls\x86
C:\> mklink /D C:\Windows\System32\my_XXbit_dlls C:\dlls\x64
symbolic link created for C:\Windows\System32\my_XXbit_dlls <<===>> C:\dlls\x64

user74094

Posted 2011-02-18T20:06:33.653

Reputation: 262

0

I have had this problem and the answer is as follows:

The path for your system variable on the 64 bit machines is c:\progra~2. You need to have a spaceless path for your environmental variable, otherwise the system won't read further than C:\programs.

On our 32 bit machines the environment variable companyprograms is c:\program files and on the 64 bit ones its c:\progra~2. We then set our shortcuts for users to %companyprograms%\...

You can do it through group policy or by script.

JRubinstein

Posted 2011-02-18T20:06:33.653

Reputation: 21

-1

As romka indicated in the followup, the simple answer is the SysWOW64 directory.

Fortunately the installers from Shining Light productions take care of this for you. Just run the 32bit and 64bit installers and select to copy the .DLLs into the Window "System" directory and the proper directory is chosen for the .DLLs (i.e. the 64bit .DLLs go into System32 and the 32bit .DLLs go into SysWOW64.

Once I had done this my 32bit apps find the 32bit .DLLs and my 64bit apps find the 64bit .DLLs.

etinthelab

Posted 2011-02-18T20:06:33.653

Reputation: 1