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
Solved

Changing system drive and environment

Posted on 1998-05-12
7
183 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Title # Comments Views Activity
how to understand recursion 12 229
Grammars for C C++ and java 1 131
Exception thrown at 0x00007FFD5BC81F28 7 49
How do i run a c++ file? 15 46
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

856 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