1

I have a third party java program called kgsgtp.jar which need to communicate with my own C++ (but mainly just C) program. The documentation for the java program states:

=====================

You just need to make sure that stdin for kgsGtp it connected to the engine's output and stdout for kgsGtp is connected to the engine's input. Usually, the easiest way to do this is by forking and execing kgsGtp from within your engine.

=====================

Now I am a reasonably competent programmer and feel that I could probably arrange all that, given just a few more clues. I suspect that if the description was expanded to erm, 10? lines instead of three and a half then I'd have it sorted in no time.

I'm guessing that what the document means by forking, is using WinExec() or CreateProcess() in my program to execute the java program? I'm also guessing that perhaps when I use the right function, then the fact of one program's stdin corresponding to the other's stdout will happen automatically?

Mick
  • 8,284
  • 22
  • 81
  • 173
  • I think that the explanation is for a unix environment. – Maurice Perry Feb 04 '10 at 09:55
  • Dupe of http://stackoverflow.com/questions/2186648/stdin-stdout-communication-between-java-program-and-c-program-under-64-bit-window asked yesterday by same user –  Feb 04 '10 at 10:11
  • @neil. No its not. The java program allows more than one method to start up the communication. The first is failing so now I'm attempting to get the other method to work. – Mick Feb 04 '10 at 10:26

2 Answers2

4

That description is for unixes, where a sequence of pipe(),dup2(), fork()/exec() calls would be use to do this.

Take a look at the code snippet in the answer from denis here: How do I get console output in C++ with a Windows program? , should get you started.

Edit: more complete example is here: http://support.microsoft.com/kb/190351

Community
  • 1
  • 1
Anonym
  • 3,098
  • 4
  • 31
  • 32
  • That looks promising... let me just double check I'm understanding this. In his code, when you reach the comment "//readfile and/or writefile"... does that mean that from now on any printf()'s I do will go to the stdin of teste.exe and vice versa? – Mick Feb 04 '10 at 10:12
  • No, it means reading from the hRead handle reads from stdout/err of teste.exe. The example there doesn't hook up stdin of teste.exe, but it would be similar - create another pipe and set the hStdInput in the STARTUPINFO structure. Added a link to another example . – Anonym Feb 04 '10 at 10:36
0

What you need is equivalent of POSIX dup() on windows may be this

sud03r
  • 19,109
  • 16
  • 77
  • 96