Thread: Portable pipes?

  1. #1
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459

    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?
    Last edited by zacs7; 03-13-2008 at 12:55 AM.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pipes sometimes fail
    By EVOEx in forum Linux Programming
    Replies: 2
    Last Post: 05-02-2009, 01:47 PM
  2. Pipes
    By Martin Kovac in forum C Programming
    Replies: 1
    Last Post: 03-31-2009, 03:09 AM
  3. Pipes, inheritence and some newbie questions
    By grasshopa in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2006, 07:00 AM
  4. which Portable mp3 player?
    By Raihana in forum A Brief History of Cprogramming.com
    Replies: 27
    Last Post: 01-09-2004, 07:58 AM
  5. Services and Pipes
    By nickname_changed in forum Windows Programming
    Replies: 0
    Last Post: 07-16-2003, 06:46 AM