Solved

Changing system drive and environment

Posted on 1998-05-12
7
186 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

732 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