Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从c++;使用CreateProcess_Python_C++_C_Mfc - Fatal编程技术网

Python 从c++;使用CreateProcess

Python 从c++;使用CreateProcess,python,c++,c,mfc,Python,C++,C,Mfc,我想使用CreateProcess()在MFC按钮上运行python脚本文件,以便使用管道实时捕获进程的输出 CString sCmd = "Cmd.Exe"; CString sParms = " /c python.exe C:\\WinApp\\Debug\\printx.py"; BOOL fRet = ExecAndProcessOutput(sCmd, sParms); BOOL ExecAndProcessOutput(LPCSTR szCmd, LPCSTR szParms)

我想使用CreateProcess()在MFC按钮上运行python脚本文件,以便使用管道实时捕获进程的输出

CString sCmd = "Cmd.Exe";
CString sParms = " /c python.exe C:\\WinApp\\Debug\\printx.py";
BOOL fRet = ExecAndProcessOutput(sCmd, sParms);


BOOL ExecAndProcessOutput(LPCSTR szCmd, LPCSTR szParms)
{
    SECURITY_ATTRIBUTES rSA = { 0 };
    rSA.nLength = sizeof(SECURITY_ATTRIBUTES);
    rSA.bInheritHandle = TRUE;

    HANDLE hReadPipe, hWritePipe;
    CreatePipe(&hReadPipe, &hWritePipe, &rSA, 25000);

    PROCESS_INFORMATION rPI = { 0 };
    STARTUPINFO         rSI = { 0 };
    rSI.cb = sizeof(rSI);
    rSI.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
    rSI.wShowWindow = SW_SHOWNORMAL;// SW_HIDE;  // or SW_SHOWNORMAL or SW_MINIMIZE
    rSI.hStdOutput = hWritePipe;
    rSI.hStdError = hWritePipe;

    CString sCmd; 
    sCmd.Format("\"%s\" %s", (LPCSTR)szCmd, (LPCSTR)szParms);

    BOOL fRet = CreateProcess(NULL, (LPSTR)(LPCSTR)sCmd, NULL,NULL, TRUE, 0, 0, 0, &rSI, &rPI);
    if (!fRet) 
    {
        return(FALSE);
    }
}
总之,将参数传递给Createprocess的第二个参数是:

"Cmd.Exe"  /c python.exe C:\WinApp\Debug\printx.py
由于某些原因,当我运行这个代码createprocess时,我没有得到任何错误作为返回参数。但是名为-printx.py的python文件未运行

感谢您在这方面的帮助


谢谢,为什么不直接运行
python.exe
,而不是通过
cmd.exe
?谢谢您的建议。尝试如下:CString sCmd=“python.Exe”;CString sParms=“/c printx.py”;没有工作。为什么不直接运行
python.exe
,而不是通过
cmd.exe
?谢谢您的建议。尝试如下:CString sCmd=“python.Exe”;CString sParms=“/c printx.py”;没用。