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.
如何设置环境变量并在ANSI C for Windows中启动进程?如果可能,我想避免使用任何Windows API。
4 个解决方案
#1
1
Assuming portability is your reason for specifying ANSI C, you can do exactly what you want with the POSIX function _execve
:
假设可移植性是您指定ANSI C的原因,您可以使用POSIX函数_execve完成您想要的操作:
- MSDN CRT Reference: _execve
MSDN CRT参考:_execve
This is a portable function that spawns a new child process and allows you to supply an array of environment settings.
这是一个可移植的函数,它生成一个新的子进程,并允许您提供一系列环境设置。
#2
3
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.
在纯ANSI C中,它是不可能的。既没有setenv函数也没有putenv函数,甚至execv *函数族都缺失了。
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.
相反,我建议你以你想要的方式编写一个小接口(可能看起来像execve),并且是系统相关的。这样,当您将程序移植到非Windows环境时,可以轻松更改包装器。
#3
0
You can use CreateProcess function from WInAPI to start a new process
您可以使用WInAPI中的CreateProcess函数来启动新进程
#4
0
To launch a process using Win32 API use the CreateProcess function as stated by kayrick.
要使用Win32 API启动进程,请使用kayrick所述的CreateProcess函数。
To set an enviorment you can use SetEnvironmentVariable. These are both Win32 API.
要设置环境,可以使用SetEnvironmentVariable。这些都是Win32 API。
You may also want to have a look at GetEnvironmentVariable.
您可能还想看看GetEnvironmentVariable。
Hope this helps.
希望这可以帮助。
#1
1
Assuming portability is your reason for specifying ANSI C, you can do exactly what you want with the POSIX function _execve
:
假设可移植性是您指定ANSI C的原因,您可以使用POSIX函数_execve完成您想要的操作:
- MSDN CRT Reference: _execve
MSDN CRT参考:_execve
This is a portable function that spawns a new child process and allows you to supply an array of environment settings.
这是一个可移植的函数,它生成一个新的子进程,并允许您提供一系列环境设置。
#2
3
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.
在纯ANSI C中,它是不可能的。既没有setenv函数也没有putenv函数,甚至execv *函数族都缺失了。
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.
相反,我建议你以你想要的方式编写一个小接口(可能看起来像execve),并且是系统相关的。这样,当您将程序移植到非Windows环境时,可以轻松更改包装器。
#3
0
You can use CreateProcess function from WInAPI to start a new process
您可以使用WInAPI中的CreateProcess函数来启动新进程
#4
0
To launch a process using Win32 API use the CreateProcess function as stated by kayrick.
要使用Win32 API启动进程,请使用kayrick所述的CreateProcess函数。
To set an enviorment you can use SetEnvironmentVariable. These are both Win32 API.
要设置环境,可以使用SetEnvironmentVariable。这些都是Win32 API。
You may also want to have a look at GetEnvironmentVariable.
您可能还想看看GetEnvironmentVariable。
Hope this helps.
希望这可以帮助。