Solved

Changing system drive and environment

Posted on 1998-05-12
7
180 Views
Last Modified: 2010-04-10
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.

0
Comment
Question by:rian
  • 5
  • 2
7 Comments
 
LVL 11

Expert Comment

by:alexo
ID: 1176090
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.
0
 
LVL 11

Accepted Solution

by:
alexo earned 100 total points
ID: 1176091
A batch file runs in the context of CMD.EXE, it does not spawn another process so it can change the current directory.
0
 

Author Comment

by:rian
ID: 1176092
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.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 11

Expert Comment

by:alexo
ID: 1176093
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).
0
 
LVL 11

Expert Comment

by:alexo
ID: 1176094
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
0
 

Author Comment

by:rian
ID: 1176095
can u give me a starting point on hooks.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1176096
* 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).
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now