Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.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
C++ 无法将命令行传递给CreateProcess创建的新exe_C++_Winapi_Createprocess - Fatal编程技术网

C++ 无法将命令行传递给CreateProcess创建的新exe

C++ 无法将命令行传递给CreateProcess创建的新exe,c++,winapi,createprocess,C++,Winapi,Createprocess,我现在尝试从C++编写的应用程序中调用.exe。 我试图调用的exe是一个Kermit程序(k95.exe)——一个文件传输应用程序 不使用C++,我可以在KEMIT程序中键入命令,它工作。< /P> 但是,现在我正在尝试使用CreateProcess()在我的应用程序中调用这个“Kermit”程序 在这个阶段,我能够成功地调用“Kermit”程序。 Kermit应用程序能够成功启动 现在,我们希望通过我的C++应用程序将“取Connect。txt”键入控制台,我们不知道如何继续。 我知道我们

我现在尝试从C++编写的应用程序中调用.exe。 我试图调用的exe是一个Kermit程序(k95.exe)——一个文件传输应用程序

不使用C++,我可以在KEMIT程序中键入命令,它工作。< /P> 但是,现在我正在尝试使用

CreateProcess()
在我的应用程序中调用这个“Kermit”程序

在这个阶段,我能够成功地调用“Kermit”程序。 Kermit应用程序能够成功启动

现在,我们希望通过我的C++应用程序将“取Connect。txt”键入控制台,我们不知道如何继续。

我知道我们可以传入命令
CreateProcess()
,类似于传递函数参数,但我不打算立即关闭这个Kermit程序。 在这两者之间,我可能仍然希望将其用于其他操作,如下载或上载文件

我们无法执行上述所有操作,因为
CreateProcess()
不返回窗口句柄


笔记
  • 上面使用的“Take”是k95.exe的命令之一

  • 以下是我的CreateProcess函数:

            bool LaunchKermitExe( const char path, char cmdLine)
            {
             STARTUPINFO         si;
             SECURITY_ATTRIBUTES saProcess, saThread;
             PROCESS_INFORMATION piProcess;
             bool bSuccess;
             DWORD lasterr;
    
    
             // setup STARTUPINFO struct
             ZeroMemory(&si, sizeof(si));
             si.cb = sizeof(si);
    
             // make new process handle inheritable
             saProcess.nLength = sizeof(saProcess);
             saProcess.lpSecurityDescriptor = NULL;
             saProcess.bInheritHandle = TRUE;
    
             // make the new thread handle not inheritable
             saThread.nLength = sizeof(saThread);
             saThread.lpSecurityDescriptor = NULL;
             saThread.bInheritHandle = FALSE;
    
             bSuccess = CreateProcess(path, cmdLine, NULL, NULL,TRUE,
                     0, NULL, NULL, &si, &piProcess);
    
             lasterr = GetLastError();
    
             // now close handles to detach the process
              CloseHandle(piProcess.hThread);
              CloseHandle(piProcess.hProcess);
    
             return bSuccess;
            }   
    

  • 因此,您需要做的是写入该进程的stdin。我不熟悉windows CreateProcess api,所以您必须自己做一些检查。显示了几个将文件和内容读入被调用进程的stdin的示例。做这类事情没有简单的方法,所以准备好学习很多关于管道的知识吧

    bool LaunchKermitExe( const char path, char cmdLine)
    
    函数参数应为
    const char*path
    char*cmdLine
    。将命令行参数添加到应用程序名称,如下所示:

    STARTUPINFOA  si;
    PROCESS_INFORMATION pi;
    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    char *cmd = strdup("c:\\windows\\notepad.exe \"c:\\path\\filename.txt\"");
    CreateProcessA(0, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
    free(cmd);
    CloseHandle(pi.hProcess);
    

    Kermit应用程序显然有自己的用于用户输入的命令处理器。创建进程时,您必须重定向其STDIN,以便根据需要向其写入自己的数据。MSDN中有一节对此进行了详细描述: