[SOLVED] How do basic applications communicate with each other?

Started by
11 comments, last by deadimp 17 years, 9 months ago
In VC _get_osfhandle will return you the Windows handle corresponding to the C-runtime file descriptor (the "int" file id as you put it). Dev-C++ probably has a different way, you'll need to read the docs.

All of this stuff is completely non-standard in the eyes of C/C++. It's OS- and compiler-specific by it's very nature. Thus as you see every combination of OS and compiler does it a little bit differently.
-Mike
Advertisement
Well, I found "int _open_osfhandle(int*,int)" that does the reverse of that. Thanks! (Also located it in MinGW "io.h")
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
Well, here's the working example I made (heavily based off of the example, made in MinGW):
main.cpp > main.exe
#include <windows.h>#include <stdio.h>#include <conio.h>#include <fcntl.h>#include <io.h>//RECUERDA: _open_osfhandlestruct { struct IO {  HANDLE read,write; } in, out;} child;struct PACKET { int x, y, z;};int main() { printf("Create handles...\n"); SECURITY_ATTRIBUTES att; memset(&att,0,sizeof(att)); att.nLength=sizeof(att); att.bInheritHandle=true; CreatePipe(&child.in.read,&child.in.write,&att,0); //In-Read,Write SetHandleInformation(child.in.write,HANDLE_FLAG_INHERIT,0); //In-Write CreatePipe(&child.out.read,&child.out.write,&att,0); //Out-Read,Write SetHandleInformation(child.out.read,HANDLE_FLAG_INHERIT,0); //Out-Read printf("Create child process...\n"); STARTUPINFO start; memset(&start,0,sizeof(start)); start.cb=sizeof(start); start.hStdError=start.hStdOutput=child.out.write; start.hStdInput=child.in.read; start.dwFlags=STARTF_USESTDHANDLES; PROCESS_INFORMATION proc; bool out; if (CreateProcess(NULL,"test",NULL,NULL,true,0,NULL,NULL,&start,&proc)) {  CloseHandle(proc.hProcess); CloseHandle(proc.hThread); } else {  printf("Error");  getch(); exit(0); } //Write to: PACKET data={0,25,34}; DWORD ret; if (!WriteFile(child.in.write,&data,sizeof(data),&ret,NULL)) printf("Wot?\n"); CloseHandle(child.in.write); //Read from: CloseHandle(child.out.write); #define BUFFER_LEN 4096 char buffer[BUFFER_LEN]; ret=1; while (ret>0) {  ReadFile(child.out.read,buffer,BUFFER_LEN,&ret,NULL);  fwrite(buffer,ret,1,stdout); } printf("\nDone processing"); getch(); return 0;}

test.cpp > test.exe
#include <windows.h>#include <conio.h>#include <stdio.h>struct PACKET { int x, y, z;}; int main() { PACKET data; printf("Processing data: "); fread(&data,sizeof(data),1,stdin); printf("x: %d, y: %d, z: %d\n",data.x,data.y,data.z);}


NOTE: Yes, I know I'm using binary I/O.

[Edited by - deadimp on July 6, 2006 5:48:13 PM]
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire

This topic is closed to new replies.

Advertisement