-
Portable pipes?
Hello,
I'm working on an application that is essential a front-end to a command line program if you will. Basically my program (A) uses another program (B) to perform a few tasks. A launches B when it needs to do the tasks, passing data via command line arguments and an anonymous pipe (B's stdin) -- reading back info over another pipe (B's stdout). However I'm aiming to have this program portable, at least Windows and Linux (I'm using Gtk for my UI).
So my question is, is there any portable pipe API? Or should I wrap Linux pipes and Windows anonymous pipes? If so, it'd be pretty hard to ensure they had the same behaviour. I ask because I don't want to reinvent the wheel, I've already looked into it but came up dry.
My wrap idea sortof goes like:
Code:
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
#ifdef LINUX
/* includes */
#endif
#include <stdio.h>
#include <stdlib.h>
int apipe_connect(struct apipe_t * p, const char * cmd)
{
#ifdef WIN32
/* error */
if(CreatePipe(&(p->hRead), &(p->hWrite), NULL, 40) == 0)
return 1;
#endif
return 0;
}
int apipe_disconnect(struct apipe_t * p)
{
#ifdef WIN32
CloseHandle(p->hRead);
CloseHandle(p->hWrite);
#endif
#ifdef LINUX
/* linux ... */
#endif
return 0;
}
Would that be the 'best' way to get my API working on Linux/Windows? (ie use of the pre-processor)... Or would it be better to put the 'WIN32' stuff in one file, ditto for the 'LINUX' stuff and include the files accordingly? Then I'd have to sync my API if it changed?
-
There's a Boost.Process somewhere - in the vault, or the sandbox, or something. The library is not complete, but it provides cross-platform support for launching processes and communicating with them.
Alternatively, look into portable runtime layers, like the APR (Apache Portable Runtime, lowest layer of the Apache server) or the NSPR (Netscape Portable Runtime, lowest layer of Firefox).
Some links:
http://apr.apache.org/
http://www.mozilla.org/projects/nspr/
http://www.netbsd.org/~jmmv/process/
http://julipedia.blogspot.com/2006/0...ls-posted.html
http://www.netbsd.org/~jmmv/process/
For developing my own cross-platform stuff, I prefer separate files. It's much cleaner, IMO. You probably won't have so many implementations and API changes that the additional changes are a relevant burden.
-
Wow thanks a lot, I never thought of using a portable runtime.
Although I'm considering having the best of both worlds, wrap my API around the native API and use glib to do the rest... or actually glib to do it all (I'm relying on libgtk so I might as well use glib). Thanks for the suggestions, apr interests me a lot.