createprocess不会继承父env

时间:2014-03-06 15:14:10

标签: 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, 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

通过运行我的程序,它总是返回消息“需要一个Home变量:缺少$ MY_HOME” 虽然变量$ MY_HOME已经设置

似乎创建的进程不继承父环境

有什么想法解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

不确定这是否解决了您的所有问题,但--login - bash - 选项清除了所有环境变量并提供了一个干净的shell。你试过省略这个选项吗?