views:

51

answers:

4

Can anybody please help me showing how to set an environment variable and start a process in ANSI C for Windows? If possible I want to avoid using any windows API for this.

+3  A: 

In pure ANSI C, it is not possible. There is neither of the functions setenv nor putenv, and even the execv* family of functions is missing.

Instead, I suggest that you write a little interface in the way you want (which possibly looks like execve) and is system-dependent. That way, you can change the wrapper easily when you port your program to a non-Windows environment.

Roland Illig
Visual C++ has `_execve` (with a leading underscore), but it's certainly not ANSI C.
Dean Harding
It is not possible to do in pure ANSI C, OK. Can you please tell me how can I do it using Windows API?
Anindya Chatterjee
A: 

You can use CreateProcess function from WInAPI to start a new process

+1  A: 

Assuming portability is your reason for specifying ANSI C, you can do exactly what you want with the POSIX function _execve:

This is a portable function that spawns a new child process and allows you to supply an array of environment settings.

gavinb
+1  A: 

To launch a process using Win32 API use the CreateProcess function as stated by kayrick.

To set an enviorment you can use SetEnvironmentVariable. These are both Win32 API.

You may also want to have a look at GetEnvironmentVariable.

Hope this helps.

bluntkhan