C++ createprocess没有';t继承父环境
我编写了以下代码来在cygwin上创建一个运行脚本的进程C++ createprocess没有';t继承父环境,c++,shell,cygwin,C++,Shell,Cygwin,我编写了以下代码来在cygwin上创建一个运行脚本的进程 DWORD RunSilent(char* strFunct, char* strstrParams) { STARTUPINFO StartupInfo; PROCESS_INFORMATION ProcessInfo; char Args[4096]; char *pEnvCMD = NULL; char *pDefaultCMD = "CMD.EXE"; ULONG rc; memset(&StartupInfo, 0,
DWORD RunSilent(char* strFunct, char* strstrParams)
{
STARTUPINFO StartupInfo;
PROCESS_INFORMATION ProcessInfo;
char Args[4096];
char *pEnvCMD = NULL;
char *pDefaultCMD = "CMD.EXE";
ULONG rc;
memset(&StartupInfo, 0, sizeof(StartupInfo));
StartupInfo.cb = sizeof(STARTUPINFO);
StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow = SW_HIDE;
Args[0] = 0;
strcat(Args, strFunct);
strcat(Args, " ");
strcat(Args, strstrParams);
if (!CreateProcess( NULL, Args, NULL, NULL, TRUE,
/*CREATE_NEW_CONSOLE, */
0,
NULL,
NULL,
&StartupInfo,
&ProcessInfo))
{
return GetLastError();
}
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
if(!GetExitCodeProcess(ProcessInfo.hProcess, &rc))
rc = 0;
CloseHandle(ProcessInfo.hThread);
CloseHandle(ProcessInfo.hProcess);
return rc;
}
int main(){
RunSilent("c:/cygwin64/bin/bash.exe", " --login -i -c \"/cygdrive/c/FP/V7/myscript \"");
return 0;
}
myscript的内容是:
if test $MY_HOME
then
FORMALHOME_HOME=$MY_HOME
export FORMALHOME_HOME
else
echo "A Home variable is required: missing \$MY_HOME"
exit -1
fi
通过运行我的程序,它总是返回消息“需要一个主变量:缺少$my_Home”
虽然变量$MY_HOME已经设置
似乎创建的进程没有继承父环境
有没有办法解决这个问题?不确定这是否解决了所有问题,但是
bash
的--login
-选项清除了所有环境变量并提供了一个干净的shell。您是否尝试忽略此选项?我已将其删除,但问题仍然存在