Setting a new environment variable for the Windows child process

 

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Target platform: none, just for learning and fun

Header file: Standard and Windows

Additional library: Windows Platform SDK

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: non-CLR or unmanaged. Need to add netapi32.lib (netapi32.dll) to the project. Click the Project menu->Select the your_project_name Properties... sub menu->Expand the Configuration Properties folder on the left pane->Expand the Linker subfolder->Select the Input subfolder->Select the Additional Dependencies field on the right pane->Click the ... at the end of the field->Type in 'netapi32.lib' in the empty pane->Click the OK button->Click the OK button second time to close the project Properties dialog.

To do: Setting new environment variable for the child process

To show: The various Windows process and thread manipulation with changing the environment variable for child process

 

 

 

 

// For WinXp

#define _WIN32_WINNT 0x0501

#include <windows.h>

#include <stdio.h>

#define BUFSIZE 4029

void MyErrorExit(LPTSTR lpszMessage)

{

fprintf(stderr, "%S\n", lpszMessage);

// Exit peacefully...

ExitProcess(0);

}

 

int wmain(int argc, WCHAR **argv)

{

LPTSTR lpszOldValue = NULL;

TCHAR tchBuf[BUFSIZE] = {0};

BOOL fSuccess;

STARTUPINFO siStartInfo = {0};

PROCESS_INFORMATION piProcInfo = {0};

 

// Fill up the allocated memory with 0's

ZeroMemory(&siStartInfo, sizeof(siStartInfo));

siStartInfo.cb = sizeof(siStartInfo);

ZeroMemory(&piProcInfo, sizeof(piProcInfo));

 

// lpszOldValue gets current value of "MYTESTVAR", or NULL if "MYTESTVAR"

// environment variable does not exist. Set "MYTESTVAR" to new value ("mynewstringvalue"),

// create child process, then use SetEnvironmentVariable() to restore

// original value of "MYTESTVAR". If lpszOldValue is NULL, the "MYTESTVAR" variable will be deleted.

lpszOldValue = ((GetEnvironmentVariable(L"MYTESTVAR", tchBuf, BUFSIZE) > 0) ? tchBuf : NULL);

 

// Set a new value for the child process to inherit, "mynewstringvalue".

if (!SetEnvironmentVariable(L"MYTESTVAR", L"mynewstringvalue"))

MyErrorExit(L"SetEnvironmentVariable() for child process failed.");

else

wprintf(L"SetEnvironmentVariable() for child process is OK.\n");

 

// Create a child process.

fSuccess = CreateProcess(L"c:\\windows\\system32\\mem.exe", NULL, NULL, NULL, TRUE, 0,

NULL, // inherit parent's environment

NULL, &siStartInfo, &piProcInfo);

 

if (!fSuccess)

MyErrorExit(L"CreateProcess(), child failed.");

else

wprintf(L"CreateProcess(), child is OK.\n");

 

// Checking...

wprintf(L"\n");

system("set");

wprintf(L"\n");

 

// Restore the parent's environment.

if (!SetEnvironmentVariable(L"MYTESTVAR", lpszOldValue))

MyErrorExit(L"SetEnvironmentVariable(), restore the parent's environment variables failed.");

else

wprintf(L"CreateProcess(), restore the parent's environment variables is OK.\n");

return 0;

}

 

Output example:

 

SetEnvironmentVariable() for child process is OK.

CreateProcess(), child is OK.

ALLUSERSPROFILE=C:\Documents and Settings\All Users

APPDATA=C:\Documents and Settings\mike spoon\Application Data

CLASSPATH=.;C:\Program Files\NetBeans 6.0\ide8\modules\ext;C:\Program Files\NetBeans 6.5.1\java2\modules\ext\toplink;C:\Program Files\Java\jre6\lib\ext\QTJava.zip

CommonProgramFiles=C:\Program Files\Common Files

COMPUTERNAME=NAZURI

ComSpec=C:\WINDOWS\system32\cmd.exe

FP_NO_HOST_CHECK=NO

HOMEDRIVE=C:

HOMEPATH=\Documents and Settings\mike spoon

JAVA_DOCPATH=C:\Program Files\Java\jdk1.6.0_03\bin

JAVA_HOME=C:\Program Files\Java\jdk1.6.0_03

LOGONSERVER=\\NAZURI

MYTESTVAR=mynewstringvalue

NUMBER_OF_PROCESSORS=2

OS=Windows_NT

Path=C:\Perl\site\bin;C:\Perl\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Common Files\Roxio Shared\DLLShared\;C:\Program Files\Common Files\Roxio Shared\DLLShared\;C:\Program Files\Common Files\Roxio Shared\9.0\DLLShared\;c:\Program Files\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\MySQL\MySQL Server 5.0\bin;C:\Program Files\Support Tools\;C:\Program Files\Java\jdk1.6.0_03\bin;C:\Program Files\NetBeans 6.8\java3\ant\bin;C:\Program Files\Java\jdk1.6.0_03\lib;C:\PHP;C:\Snort

\bin;C:\Program Files\TortoiseSVN\bin;C:\Program Files\Microsoft Network Monitor 3\;C:\Program Files\QuickTime\QTSystem\;C:\Program Files\Microsoft Visual Studio 9.0\;c:\Program Files\Microsoft Visual Studio 9.0\VC\bin

PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH

PHP_PEAR_BIN_DIR=C:\PHP

PHP_PEAR_DATA_DIR=C:\PHP\data

PHP_PEAR_DOC_DIR=C:\PHP\docs

PHP_PEAR_INSTALL_DIR=C:\PHP\pear

PHP_PEAR_PHP_BIN=.\php.exe

PHP_PEAR_SYSCONF_DIR=C:\PHP

PHP_PEAR_TEST_DIR=C:\PHP\tests

PROCESSOR_ARCHITECTURE=x86

PROCESSOR_IDENTIFIER=x86 Family 6 Model 15 Stepping 13, GenuineIntel

PROCESSOR_LEVEL=6

PROCESSOR_REVISION=0f0d

ProgramFiles=C:\Program Files

PROMPT=$P$G

QTJAVA=C:\Program Files\Java\jre6\lib\ext\QTJava.zip

RoxioCentral=C:\Program Files\Common Files\Roxio Shared\9.0\Roxio Central33\

SESSIONNAME=Console

SystemDrive=C:

SystemRoot=C:\WINDOWS

TEMP=C:\DOCUME~1\MIKESP~1\LOCALS~1\Temp

TMP=C:\DOCUME~1\MIKESP~1\LOCALS~1\Temp

USERDOMAIN=NAZURI

USERNAME=mike spoon

USERPROFILE=C:\Documents and Settings\mike spoon

VBOX_INSTALL_PATH=C:\Program Files\Oracle\VirtualBox\

VisualStudioDir=C:\Documents and Settings\mike spoon\My Documents\Visual Studio 2008

VS90COMNTOOLS=c:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools\

WecVersionForRosebud.CFC=2

windir=C:\WINDOWS

_ACP_ATLPROV=c:\Program Files\Microsoft Visual Studio 9.0\VC\Bin\ATLProv.dll

_ACP_INCLUDE=c:\Program Files\Microsoft Visual Studio 9.0\VC\include;c:\Program Files\Microsoft Visual Studio 9.0\VC\atlmfc\include;C:\Program Files\Microsoft SDKs\Windows\v6.0A\\include;C:\Program Files\Microsoft SDKs\Windows\v6.0A\include

_ACP_LIB=c:\Program Files\Microsoft Visual Studio 9.0\VC\lib;c:\Program Files\Microsoft Visual Studio 9.0\VC\atlmfc\lib;c:\Program

Files\Microsoft Visual Studio 9.0\VC\atlmfc\lib\i386;C:\Program Files\Microsoft SDKs\Windows\v6.0A\\lib;C:\Program Files\Microsoft SDKs\Windows\v6.0A\lib;C:\Program Files\Microsoft Visual Studio 9.0\;C:\Program Files\Microsoft Visual Studio 9.0\lib

_ACP_PATH=c:\Program Files\Microsoft Visual Studio 9.0\VC\bin;C:\Program Files\Microsoft SDKs\Windows\v6.0A\\bin;C:\Program Files\

Microsoft Visual Studio 9.0\Common7\Tools\bin;C:\Program Files\Microsoft Visual Studio 9.0\Common7\tools;C:\Program Files\Microsoft Visual Studio 9.0\Common7\ide;C:\Program Files\HTML Help Workshop;C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin;c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727;C:\Program Files\Microsoft Visual Studio 9.0\;C:\WINDOWS\SysWow64;;C:\Perl\site\bin;C:\Perl\bin;

C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Common Files\Roxio Shared\DLLShared\;C:\Program Files\Com

mon Files\Roxio Shared\DLLShared\;C:\Program Files\Common Files\Roxio Shared\9.0\DLLShared\;c:\Program Files\Microsoft SQL Server\

90\Tools\binn\;C:\Program Files\MySQL\MySQL Server 5.0\bin;C:\Program Files\Support Tools\;C:\Program Files\Java\jdk1.6.0_03\bin;C

:\Program Files\NetBeans 6.8\java3\ant\bin;C:\Program Files\Java\jdk1.6.0_03\lib;C:\PHP;C:\Snort\bin;C:\Program Files\TortoiseSVN\

bin;C:\Program Files\Microsoft Network Monitor 3\;C:\Program Files\QuickTime\QTSystem\

 

CreateProcess(), restore the parent's environment variables is OK.

Press any key to continue . . .

655360 bytes total conventional memory

655360 bytes available to MS-DOS

627616 largest executable program size

1048576 bytes total contiguous extended memory

0 bytes available contiguous extended memory

941056 bytes available XMS memory

MS-DOS resident in High Memory Area

 

 

C and C++ Programming Resources | C & C++ Code Example Index