3

I am trying to create a pipe in my c program to input data to another program myProgram.exe. All I need to do is to get myProgram running and send "quit" from the main program. The problem is I don't know how I can create a pipe in c.

I got a working Perl example somewhere:

open(myHandle, ".../myProgram.exe");
//... (do something else) ...
printf(myHandle, "quit");
close(myHandle);

Can somebody please help me translate this into c code? Please keep in mind I'm running under Windows so there is no unistd.h available.

Cheers, B

b3d
  • 31
  • 1
  • 3

2 Answers2

4

I've recently posted a simple example how to create pipes for inter-process communication:

See Create Named Pipe C++ Windows

Community
  • 1
  • 1
Lukas Thomsen
  • 3,089
  • 2
  • 17
  • 23
  • Thanks for the example. I have a simple question: to connect a client to the pipe, do I have to necessarily edit the code in the client program (add the line with CreateFile etc.)? I want to feed my client some input, but in my case I have the client program from somewhere else and I really don't want to change anything in the code there. I hope you understand my question. – b3d Nov 04 '14 at 08:01
  • Yes you have to do that. If you don't want to change the other application you can create the application with a new handle to STDOUT/STDIN. Check the documentation of attribute of `STARTUPINFO` structure and function `CreateProcess(..)`. – Lukas Thomsen Nov 04 '14 at 20:56
  • Thanks Lukas. I set the new handlesto STDOUT/STDIN as you suggested. I transmuted this [example](http://msdn.microsoft.com/en-us/library/windows/desktop/ms682499%28v=vs.85%29.aspx). I didn't make any changes in my client program. I managed to get it running and pipe no more than a single command to it. It is not optimal, but for my needs it is enough. – b3d Nov 05 '14 at 08:09
0

Maybe this would help:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa365780(v=vs.85).aspx

There is a lot of information about pipes under windows there.

Also see:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa365781(v=vs.85).aspx

For a list of pipe-related functions in WINAPI