lpEnvironment [in] Pointer to an environment block for the new process. If this parameter is NULL, the new process uses the environment of the calling process. An environment block consists of a null-terminated block of null-terminated strings. Each string is in the form: name=value 如果不懂英文,自己去GOOGLE翻译,你结贴率太低了。
msdn是这样说的。// Set a value for the child process to inherit.
An environment block consists of a null-terminated block of null-terminated strings. Each string is in the form: name=value
如果不懂英文,自己去GOOGLE翻译,你结贴率太低了。
if (! SetEnvironmentVariable(TEXT("myvar"), TEXT("newvalue")))
{
printf("SetEnvironmentVariable failed (%d)\n", GetLastError());
return FALSE;
}
// Create a child process.
fSuccess = CreateProcess(NULL, TEXT("childenv"), NULL, NULL, TRUE, 0,
NULL, // inherit parent's environment
NULL, &si, &pi);
STARTUPINFO si;
DWORD dw;
PROCESS_INFORMATION pi;
ZeroMemory(&si,sizeof(STARTUPINFO));
ZeroMemory(&pi,sizeof(PROCESS_INFORMATION));
TCHAR szEnviron[MAX_PATH];
_stprintf(szEnviron,_T("TEMP=%SystemRoot%\TEMP"));
if(!CreateProcess(_T("c:\\Environ1.exe"),NULL,NULL,NULL,FALSE,CREATE_NEW_CONSOLE|CREATE_UNICODE_ENVIRONMENT,szEnviron,NULL,&si,&pi))
//MessageBox(NULL,_T("创建进程失败!"),_T("消息"),MB_OK);
dw=GetLastError();那个子进程Environ1中,获取环境变量的代码是:
TCHAR szEnviron[1024];
GetEnvironmentVariable(_T("TEMP"),szEnviron,1024);
MessageBox(NULL,szEnviron,_T("消息"),MB_OK);
可是子进程一启动之后,就弹出一个错误对话框,说是:
"应用程序正常初始化(0xc0150004)失败。请单击"确定",终止应用程序。"
连第一行代码都没执行。