Solved

Changing system drive and environment

Posted on 1998-05-12
7
182 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP.net build a IF/Then Walkthrough Guide 1 210
Grammars for C C++ and java 1 122
IdTCPClient1->Disconnect(); not working 3 67
Find Visual Studio Tools 2 103
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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.

773 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