Environment Vars - using
-
Where can I find info on the usage of Environment Variables? What I want to do: 1) use my own environment vars in links and shortcuts (e.g. Target and Start In fields in a shortcut's property dialog 2) use my own environment vars in batch files 3) set my own environment vars programmatically (from VC++) If you have the answer, I wouldn't mind getting quick input on these specific issues too. What I did: I) under "My Computer | Properties | Advanced | Environment Variables", I created a user variable My_ROOT with C:\src_latest as the value. II) Restarted my computer III) at the DOS prompt, I tried to do simple things like cd My_ROOT, cd $My_ROOT, and in a shortcut I want altered, I tried to use My_ROOT\bin\CoolProg.exe (Target field). No success to any of III) above. Thanks a bunch! Johnny
-
Where can I find info on the usage of Environment Variables? What I want to do: 1) use my own environment vars in links and shortcuts (e.g. Target and Start In fields in a shortcut's property dialog 2) use my own environment vars in batch files 3) set my own environment vars programmatically (from VC++) If you have the answer, I wouldn't mind getting quick input on these specific issues too. What I did: I) under "My Computer | Properties | Advanced | Environment Variables", I created a user variable My_ROOT with C:\src_latest as the value. II) Restarted my computer III) at the DOS prompt, I tried to do simple things like cd My_ROOT, cd $My_ROOT, and in a shortcut I want altered, I tried to use My_ROOT\bin\CoolProg.exe (Target field). No success to any of III) above. Thanks a bunch! Johnny
john john mackey wrote: III) at the DOS prompt, I tried to do simple things like cd My_ROOT, cd $My_ROOT, and in a shortcut I want altered, I tried to use My_ROOT\bin\CoolProg.exe (Target field). Use % instead of $ Try cd %My_ROOT% [EDIT] I just tried cd %TEMP% [/EDIT] John
-
Where can I find info on the usage of Environment Variables? What I want to do: 1) use my own environment vars in links and shortcuts (e.g. Target and Start In fields in a shortcut's property dialog 2) use my own environment vars in batch files 3) set my own environment vars programmatically (from VC++) If you have the answer, I wouldn't mind getting quick input on these specific issues too. What I did: I) under "My Computer | Properties | Advanced | Environment Variables", I created a user variable My_ROOT with C:\src_latest as the value. II) Restarted my computer III) at the DOS prompt, I tried to do simple things like cd My_ROOT, cd $My_ROOT, and in a shortcut I want altered, I tried to use My_ROOT\bin\CoolProg.exe (Target field). No success to any of III) above. Thanks a bunch! Johnny
Hi, > 1) use my own environment vars in links and shortcuts > (e.g. Target and Start In fields in a shortcut's property dialog Surround the variable name with percent symbols; e.g.
%PROJECTS%\SomeTool.exe %LAUNCH_ARGS%
Also, if the variable represents a path and it may have a space in it I'd recommend changing the above to:
"%PROJECTS%\SomeTool.exe" %LAUNCH_ARGS%
> 2) use my own environment vars in batch files Same again. > 3) set my own environment vars programmatically (from VC++) You can use the SetEnvironmentVariable API but this function only sets the variable for the current process (and any child processes that you may launch that inherit the current environment). To set persistent environment variables you have to save them in the registry on NT/2K/XP and in AUTOEXEC.BAT on 95/98/Me. The rest of these notes are for NT/2K/XP only. There are three types of environment variables: user, system and volatile. The system variables are loaded first and are the same for every user on the system. They are stored as values of this registry key:
[HKLM\System\CurrentControlSet\Control\Session Manager\Environment]
If the value is a straight string the registry value type is REG_SZ, but if the value contains another environment variable name (in percent symbols) the value type is REG_EXPAND_SZ. Next come the user environment variables. These are per user (duh!) and are stored in each user's registry hive under:
[HKCU\Environment]
These follow the same rules as the system variables; they can also refer to a system variable name (in percent symbols). The final category is volatile variables; these are also per-user but are not saved between logon sessions; the system stores things such as the current logon server and domain name here. They are stored in:
[HKCU\Volatile Environment]
All three types are joined to create the final environment block your process is started with. BTW, if you do create a tool to modify the environment and you want a running process (such as Explorer) to pick up the changes, you have to broadcast a WM_SETTINGSCHANGE message to notify them. A snippet of C++ does this:
const DWORD dwTimeout= 3000;
DWORD dwResult= 0;
SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE,
0, (LPARAM)_T("Environment"), SMTO_NORMAL,
dwTimeout, &dwResult);Hope this helps. Regards, John Bates.
-
Where can I find info on the usage of Environment Variables? What I want to do: 1) use my own environment vars in links and shortcuts (e.g. Target and Start In fields in a shortcut's property dialog 2) use my own environment vars in batch files 3) set my own environment vars programmatically (from VC++) If you have the answer, I wouldn't mind getting quick input on these specific issues too. What I did: I) under "My Computer | Properties | Advanced | Environment Variables", I created a user variable My_ROOT with C:\src_latest as the value. II) Restarted my computer III) at the DOS prompt, I tried to do simple things like cd My_ROOT, cd $My_ROOT, and in a shortcut I want altered, I tried to use My_ROOT\bin\CoolProg.exe (Target field). No success to any of III) above. Thanks a bunch! Johnny
Thanks all. I got my shortcuts running with %My_ROOT%. I now must fix my CoolProg.exe code so that when I supply a configuration file as an argument, and that configuration file itself uses the My_ROOT variable within, the environment variable is correctly interpreted as the full path (value). Johnny
-
Thanks all. I got my shortcuts running with %My_ROOT%. I now must fix my CoolProg.exe code so that when I supply a configuration file as an argument, and that configuration file itself uses the My_ROOT variable within, the environment variable is correctly interpreted as the full path (value). Johnny
ExpandEnvironmentStrings()
will do that for you --Mike-- "So where does that leave us? Well, it leaves us right back where we started, only more confused than before." -- Matt Gullett Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber -
ExpandEnvironmentStrings()
will do that for you --Mike-- "So where does that leave us? Well, it leaves us right back where we started, only more confused than before." -- Matt Gullett Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabberThanks, I didn't know about the ExpandEnvironmentStrings() and therefore wrote my own string parser/environment variable expander (using the GetEnv() function and a while loop) I think I will use the canned ExpandEnvironmentStrings() function. HOWEVER, I am noticing a problem with the CWinApp::ParseCommandLine() Maybe it's just me, but when I do the following %My_ROOT%\bin\myapp.exe %My_ROOT%\config\myconfigs.prj The argument "myconfigs.prj" doesn't get its pathname resolved -- it passes down to the lower underlyings of CWinApp::ParseCommandLine() as the literal string %My_ROOT%\config\myconfigs.prj and tries to open that file, which fails. Any more guidance?? Thanks - Happy Independence Day! Johnny
-
Thanks, I didn't know about the ExpandEnvironmentStrings() and therefore wrote my own string parser/environment variable expander (using the GetEnv() function and a while loop) I think I will use the canned ExpandEnvironmentStrings() function. HOWEVER, I am noticing a problem with the CWinApp::ParseCommandLine() Maybe it's just me, but when I do the following %My_ROOT%\bin\myapp.exe %My_ROOT%\config\myconfigs.prj The argument "myconfigs.prj" doesn't get its pathname resolved -- it passes down to the lower underlyings of CWinApp::ParseCommandLine() as the literal string %My_ROOT%\config\myconfigs.prj and tries to open that file, which fails. Any more guidance?? Thanks - Happy Independence Day! Johnny
Try something like this (inside your InitInstance() function):
// Parse command line for standard shell commands, DDE, file open CCommandLineInfo cmdInfo; ParseCommandLine(cmdInfo); \_TCHAR szExpanded\[MAX\_PATH+1\] = {0}; ExpandEnvironmentStrings(cmdInfo.m\_strFileName, szExpanded, MAX\_PATH);
Then, instead of
cmdInfo.m_strFileName
, useszExpanded
; I prefer to wear gloves when using it, but that's merely a matter of personal hygiene [Roger Wright on VB] Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. [Rich Cook]