Unable to pass command line to new exe created by CreateProcess

时间:2016-11-18 10:50:44

标签: c++ winapi createprocess

I am now trying to call an .exe from my application written in C++ The exe that I am trying to call is a Kermit program (k95.exe) - a file transfer app.

Without using C++, I am able type in command at the Kermit program and it works.

Able to key in "Take connect.txt" command However, now I am trying use CreateProcess() to call this 'Kermit' program in my application.

At this stage, I am able to call up 'Kermit' program successfully. The Kermit application able to launch up successfully.

Now, we want to key in the "Take connect.txt" into the console through my C++ application and we got no idea how to proceed.

I know that we able to pass in command CreateProcess(), some sort like passing function parameters but I do not intend to close this Kermit program immediately. In between, I may still want to use it for other operation such as Download or Upload file.

We can't do all above because CreateProcess() does not return a window handle.


Notes

  1. "Take" used above is one of the command for k95.exe

  2. Here's my CreateProcess function located:

            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;
            }   
    

3 个答案:

答案 0 :(得分:0)

所以你需要做的是写入该进程的标准输入。我不熟悉Windows CreateProcess api,所以你必须自己做一些看法。 This example on显示了几个读取文件和填充到被调用进程的标准输入中的示例。没有简单的方法来做这种事情,所以准备学习很多关于管道的事情!

答案 1 :(得分:0)

YLabel panel = new YLabel();
for(Component component : panel.getComponents()) {
    System.out.println("component "+component.getClass().getCanonicalName());
    //Change the properties here for your components
}    

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

char* cmdLine

答案 2 :(得分:0)

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

Creating a Child Process with Redirected Input and Output