Changing system drive and environment

I am trying to write a Windows console C++ program to run under windows NT Server.  This program sole job is to check the console process and change the system drive and directory in the console to a pre-determined setting.
Is there a way to change the program can change the drive and directory  and exit out to the new drive and directory.
I have tried chdir and chdrive. Apparantly these functions change the working directory. When the program exits, the drive and directory is  not changed.

I am trying to do the same with setting new environment variables.

Thanks.

rianAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

alexoCommented:
I don't think it's possible.
NT keeps a "current directory" for each PROCESS.  Your program runs as a separate process and changes its own "current directory".  When it exits, the change is gone.
You'll have to use a batch file.
alexoCommented:
A batch file runs in the context of CMD.EXE, it does not spawn another process so it can change the current directory.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rianAuthor Commented:
I wonder how the windows operating system does it. They must be calling some low level code. Is the same true for changing enviroment values.
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

alexoCommented:
There is a mastr environment bock, backed by the registry, which contains the settings that all processes inherit on startup.  You can change its settings but it won't affect the currently running processes.  On the other hand, you can change the *local copy* of the environment of your process using SetEnvironmentVariable() but that will only affect your process and the "child" processes you create.

Hmmm...
You know what?  You can inject a DLL into all processes in your system using a systemwide hook and make it change the environment.  See SetWindowHookEx() function.  Although I suggest you get Richter's "Advanced Windows" and read it before you mess with hooks (I can give you some help if you're desperate :-) but a slight bug can, and probably will, mess up your system to the point of rebooting).
alexoCommented:
I seem to remember answering a similar question before...
Yup!  http://www.experts-exchange.com/Q.10033351 (currently worth 7 points).

Relevant part reproduced below:

The environment in NT is created from a composition of two registry entries.

One holds the "global" environment:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

The other holds the "per user" environment:
HKEY_CURRENT_USER\Environment

Most of the entries in the environment that a process gets are taken from one of those keys.  However, certain values (like "path") are a composite of their values in both keys.

An application may send an WM_SETTINGCHANGE message (a.k.a WM_WININICHANGE) when it changes entries in the registry but, if I understand the docs correctly, it is not done automatically.

Also see KnowledgeBase article Q104011
rianAuthor Commented:
can u give me a starting point on hooks.
alexoCommented:
* Get Richter's "advanced windows" book.  Read it.  Read it again.

* Check the online help on SetWindowsHookEx() function.

* See previous discussions on EE (featuring yours truly)
  http://www.experts-exchange.com/Q.10053295 (still free)
  http://www.experts-exchange.com/Q.10053225 (still free)
  http://www.experts-exchange.com/Q.10051916 (10 points)
  http://www.experts-exchange.com/Q.10048548 (15 points)

* Praise me for being such a helpful person (This is mandatory, your code will not compile without it).
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.