1

I don't want to create another bicycle and searched lightweight (not boost, poco, ace or another library) crossplatform (win/lin) wrapper under pipe/popen for C++. Any suggestions?

Torsten
  • 21,726
  • 5
  • 24
  • 31

1 Answers1

3

Maybe something like

#ifndef _WIN32
inline int _pipe(int fildes[2], unsigned psize, int textmode) {
   return pipe(fildes);
}
inline FILE* _popen(const char* command, const char* type) {
   return popen(command, type);
}
inline void _pclose(FILE* file) { 
   pclose(file); 
}
#endif

MSDN says _popen, _pclose and _pipe are provided in the CRT. So we just forward the underscored functions to the real POSIX functions outside of Windows (you could do the reverse, but notice that _pipe takes 2 extra arguments which are meaningful in Windows).

                      ___________
                     /           \ 
                    |  Warning !  |
                     \___________/
                           |
 -------- __@      __@     | __@       __@      __~@
 ----- _`\<,_    _`\<,_    _`\<,_     _`\<,_    _`\<,_
 ---- (*)/ (*)  (*)/ (*)  (*)/ (*)  (*)/ (*)  (*)/ (*)
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1. These functions should only be used in console programs. It won't work in Windows GUI program. See the note in the MSDN page of _popen for more info, and see also What is the equivalent to Posix popen() in the Win32 API?.

  2. You do know that popen executes a command in shell right? The syntax between a UNIX shell and the Windows command prompt is vastly different, and these can't be tackled with some cross-platform implementation of pope/pclose.

  3. There might be some subtle difference between the Windows functions and the real POSIX functions not handled with these simple wrappers.

  4. You can't use them in WinRT.

(The bicycles are copied from http://www.chris.com/ascii/index.php?art=transportation%2Fbicycles.)

Community
  • 1
  • 1
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • Yeah, it's a simplest C way. I found some modern C++ implementation with redirecting output features, error handling, etc. I want to create easy to use mechanism to run any statement in separate process (because statement can crush application) and analyze output and return code. Before doing it i want to looking around. – Torsten Sep 13 '12 at 10:00
  • 2
    @Torsten: What implementation would that be? Can you link to it? – Ingwie Phoenix Oct 24 '14 at 04:44
  • @suluke Sorry, I get confuse :p. – Stargateur Feb 24 '17 at 17:37
  • 2
    @Stargateur another inexplicable edit-reject for me. I should probably stop trying to improve upon answers entirely. Maybe @kennytm still wants to edit their answer to have `_pclose` return the `int` value from `pclose` so one can check the return value with it? – suluke Feb 24 '17 at 20:00