0

I can create a process just fine and send a command to it on start up .

My question:

  • is let's say a new process is created
  • e.g. a new cmd window
  • and it has a pid of 007.

How do i send another command - for example DIR - to do it later on?

I know how to concatenate and send multiple commands during process creation ,but i want to know how to send command after the process is created e.g. after i see my new cmd console with pid 007 on screen.

I read that I could use the Windows WriteFile function, but i don't know .

STARTUPINFO siStartupInfo;
PROCESS_INFORMATION piProcessInfo;
    
memset(&siStartupInfo, 0, sizeof(siStartupInfo));
memset(&piProcessInfo, 0, sizeof(piProcessInfo));
    
siStartupInfo.cb = sizeof(siStartupInfo);
siStartupInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_FORCEOFFFEEDBACK | STARTF_USESTDHANDLES;
siStartupInfo.wShowWindow = SW_HIDE;
    
if (CreateProcess(MyApplication, "", 0, 0, FALSE, 0, 0, 0, &siStartupInfo, &piProcessInfo) == FALSE)  
    // How do i send lets say command  dir to my process  here.
    return 0;
}
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
  • 3
    How to "send a command" will differ depending on the application you want to run. For example of a command-line environment, this is usually done by using *pipes*, where one pipe is connected to the standard input of the application you run. Everything you write to that pipe will be considered input for the application (as long as it reads from its standard input). – Some programmer dude Apr 07 '22 at 13:51
  • 1
    [Creating a Child Process with Redirected Input and Output](https://learn.microsoft.com/en-us/windows/win32/procthread/creating-a-child-process-with-redirected-input-and-output) – Remy Lebeau Apr 07 '22 at 14:46

1 Answers1

3

Generally, Inter-Process communication is something that both processes must define in order to work. You can't just send "commands" to another application if the other application is not designed to receive it (or easily one program would ruin the other).

Probably you tried cmd.exe /c dir which instantly executes "dir" on cmd.exe but this works because cmd.exe has the /c parameter, not because you can do that generally. Since cmd can execute batch files, you may try this or this or this.

Generally, after both applications agree on what to send, the next step is how to transfer data. In Windows, this can be done with File Mapping, Sockets, Pipes etc.

Michael Chourdakis
  • 10,345
  • 3
  • 42
  • 78