zhangxaochen
zhangxaochen

Reputation: 34017

Environment variable not parsed correctly in when cmd-line being started from the explorer

I set some environment vars using rapidEE.exe:

OPENCV_HOME=D:\opencv249
OPENCV_LIB=%OPENCV_HOME%\build\x86\vc10\lib
OPENCV_BIN=%OPENCV_LIB%\..\bin

and save & restart my win7. Then I open (Shift + right-click, clicking "open cmd window here") two cmd windows separately from my DESKTOP and DRIVE-C, and echo the variables, the var %opencv_bin% is not expanded correctly in the 1st cmd window, WHY?

from drive C:

C:\>echo %opencv_home%
D:\opencv249

C:\>echo %opencv_lib%
D:\opencv249\build\x86\vc10\lib

C:\>echo %opencv_bin%
%OPENCV_LIB%\..\bin

C:\>

from desktop:

d:\Users\zhangxaochen\Desktop>echo %opencv_home%
D:\opencv249

d:\Users\zhangxaochen\Desktop>echo %opencv_lib%
D:\opencv249\build\x86\vc10\lib

d:\Users\zhangxaochen\Desktop>echo %opencv_bin%
D:\opencv249\build\x86\vc10\lib\..\bin

d:\Users\zhangxaochen\Desktop>

all the vars I defined are expandable strings: enter image description here

Upvotes: 0

Views: 216

Answers (1)

MC ND
MC ND

Reputation: 70923

Maybe I'm wrong, if so then sorry, but the real question should be Why I get the variable parsed correctly in one of the windows?

The environment variables read operations from the registry are done in alphabetic order. Order is important.

Once all variables has been read, REG_EXPAND_SZ variables are reprocessed so values depending on other variables can be expanded. But if you have a REG_EXPAND_SZ variable depending on another REG_EXPAND_SZ variable that is alphabetically greater, when the first variable is processed the second still has no value and the first variable will hold the reference (not the value) to the second. And this is what is stored in the environment block of the created process.

So, the normal behaviour is what you see from C, opencv_bin depending on opencv_lib is not expanded as both are REG_EXPAND_SZ and opencv_bin < opencv_lib

Why then you get a different behaviour in another window and the variables replaced? It will depend on how exactly did you start the two windows, the process that is the parent of each of them (that can change and as the environment is inherited from parent to child it is important), and (this is just an assumption) if the data you are posting is correct, it seems you are not using the standard windows command line: How did you get a lowercase drive letter in the prompt?

In any case, the easier way to solve it is changing how the variables get initialized

OPENCV_BIN=%OPENCV_HOME%\build\x86\vc10\bin
OPENCV_HOME=D:\opencv249
OPENCV_LIB=%OPENCV_BIN%\..\lib

And change OPENCV_HOME to REG_SZ

Upvotes: 2

Related Questions