【问题标题】:Unable to pass command line to new exe created by CreateProcess无法将命令行传递给 CreateProcess 创建的新 exe
【发布时间】:2016-11-18 10:50:44
【问题描述】:

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

在不使用 C++ 的情况下,我可以在 Kermit 程序中输入命令并且它可以工作。

但是,现在我尝试使用CreateProcess() 在我的应用程序中调用这个“Kermit”程序。

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

现在,我们想通过我的 C++ 应用程序将“Take connect.txt”键入控制台,但我们不知道如何继续。

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

我们无法做到以上所有,因为CreateProcess() 不返回窗口句柄。


注意事项

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

  2. 我的 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;
            }   
    

【问题讨论】:

    标签: c++ winapi createprocess


    【解决方案1】:

    所以你需要做的是写入该进程的标准输入。我不熟悉 windows CreateProcess api,所以你必须自己看一下。 This example on 展示了几个将文件和内容读入被调用进程的标准输入的示例。做这种事情没有简单的方法,所以准备学习很多关于管道的知识!

    【讨论】:

      【解决方案2】:

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

      Creating a Child Process with Redirected Input and Output

      【讨论】:

        猜你喜欢
        • 2020-02-18
        • 2010-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多