Home > Enterprise >  Create new process, then send it a command later, on Windows 10
Create new process, then send it a command later, on Windows 10

Time:04-08

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

CodePudding user response:

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.

  • Related