Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 3

Qt-interest Archive, September 1999
Help for popen()-like function under Windows


Message 1 in thread

Hello, 
 	I am currently porting a UNIX program to WINDOWS.
Most difficulty time I have is to find the popen()-like function under
WINDOWS. Any help and hints would be greatly appreciated.

Thanks in advance
Tianlin Wang


Message 2 in thread

Have  look at the Doxygen source code - I remember seeing a #define popen
_popen or something like that...

Michael


> -----Original Message-----
> From: owner-qt-interest@troll.no [mailto:owner-qt-interest@troll.no]On
> Behalf Of Tianlin Wang
> Sent: Monday, September 20, 1999 1:27 AM
> To: qt-interest@troll.no
> Subject: Help for popen()-like function under Windows
>
>
> Hello,
>  	I am currently porting a UNIX program to WINDOWS.
> Most difficulty time I have is to find the popen()-like function under
> WINDOWS. Any help and hints would be greatly appreciated.
>
> Thanks in advance
> Tianlin Wang
>
> --
> List archive and information: http://www.troll.no/qt-interest/
>


Message 3 in thread


> Hello, 
>  	I am currently porting a UNIX program to WINDOWS.
> Most difficulty time I have is to find the popen()-like function under
> WINDOWS. Any help and hints would be greatly appreciated.
> 
> Thanks in advance
> Tianlin Wang

This is what I use instead of popen(): (Sorry for the comments in german ;-))
It is not an **EXACT** replacement for popen() but it is OK for me.

Kurt.

/*------------------------------------------------------------------------------
  Globals fuer die Routinen pt_popen() / pt_pclose()
------------------------------------------------------------------------------*/
#if defined(WIN32) && defined(WITH_QT)
#include <windows.h>
HANDLE my_pipein[2], my_pipeout[2], my_pipeerr[2];
char   my_popenmode = ' ';

static int
my_pipe(HANDLE *readwrite)
{
  SECURITY_ATTRIBUTES sa;

  sa.nLength = sizeof(sa);          /* Laenge in Byte */
  sa.bInheritHandle = 1;            /* Descriptoren sollen vererbbar sein */
  sa.lpSecurityDescriptor = NULL;

  if (! CreatePipe (&readwrite[0],&readwrite[1],&sa,1 << 13))
  {
    errno = EMFILE;
    return -1;
  }

  return 0;
}

#endif /* WIN32 && WITH_QT */

/*------------------------------------------------------------------------------
  Ersatz fuer die Routine 'popen()' unter WIN32.
  ACHTUNG: Wenn 'cmd' den String '2>&1' enthaelt, wird der Standarderror File-
  Handle auf den Standardoutputfilehandle umgebogen.
------------------------------------------------------------------------------*/
FILE *
#ifdef __STDC__
pt_popen(const char *cmd, const char *mode)
#else
pt_popen(cmd, mode) char *cmd, *mode;
#endif
{
#if defined(WIN32) && defined(WITH_QT)
  FILE *fptr = (FILE *)0;
  PROCESS_INFORMATION piProcInfo;
  STARTUPINFO siStartInfo;
  int success, umlenkung;

  my_pipein[0]   = INVALID_HANDLE_VALUE;
  my_pipein[1]   = INVALID_HANDLE_VALUE;
  my_pipeout[0]  = INVALID_HANDLE_VALUE;
  my_pipeout[1]  = INVALID_HANDLE_VALUE;
  my_pipeerr[0]  = INVALID_HANDLE_VALUE;
  my_pipeerr[1]  = INVALID_HANDLE_VALUE;

  if (!mode || !*mode)
    goto finito;

  my_popenmode = *mode;
  if (my_popenmode != 'r' && my_popenmode != 'w')
    goto finito;

  /*
   * Soll der stderr auf stdin umgelenkt werden ? */
  umlenkung = pt_strinstr("2>&1",(char *)cmd) != 0;

  /*
   * Erzeuge die Pipes... */
  if (my_pipe(my_pipein)  == -1 ||
      my_pipe(my_pipeout) == -1)
    goto finito;
  if (!umlenkung && my_pipe(my_pipeerr) == -1)
    goto finito;

  /*
   * Erzeuge jetzt den Sohnprozess */
  ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
  siStartInfo.cb           = sizeof(STARTUPINFO);
  siStartInfo.hStdInput    = my_pipein[0];
  siStartInfo.hStdOutput   = my_pipeout[1];
  if (umlenkung)
    siStartInfo.hStdError  = my_pipeout[1];
  else
    siStartInfo.hStdError  = my_pipeerr[1];
  siStartInfo.dwFlags    = STARTF_USESTDHANDLES;

  success = CreateProcess(NULL,
     (LPTSTR)cmd,       // command line 
     NULL,              // process security attributes 
     NULL,              // primary thread security attributes 
     TRUE,              // handles are inherited 
     DETACHED_PROCESS,  // creation flags: Ohne Fenster (?)
     NULL,              // use parent's environment 
     NULL,              // use parent's current directory 
     &siStartInfo,      // STARTUPINFO pointer 
     &piProcInfo);      // receives PROCESS_INFORMATION 

  if (!success)
    goto finito;

  /*
   * Diese Handles gehoeren dem Sohnprozess */
  CloseHandle(my_pipein[0]);  my_pipein[0]  = INVALID_HANDLE_VALUE;
  CloseHandle(my_pipeout[1]); my_pipeout[1] = INVALID_HANDLE_VALUE;
  CloseHandle(my_pipeerr[1]); my_pipeerr[1] = INVALID_HANDLE_VALUE;

  if (my_popenmode == 'r')
    fptr = _fdopen(_open_osfhandle((long)my_pipeout[0],_O_BINARY),"r");
  else
    fptr = _fdopen(_open_osfhandle((long)my_pipein[1],_O_BINARY),"w");

finito:
  if (!fptr)
  {
    if (my_pipein[0]  != INVALID_HANDLE_VALUE)
      CloseHandle(my_pipein[0]);
    if (my_pipein[1]  != INVALID_HANDLE_VALUE)
      CloseHandle(my_pipein[1]);
    if (my_pipeout[0] != INVALID_HANDLE_VALUE)
      CloseHandle(my_pipeout[0]);
    if (my_pipeout[1] != INVALID_HANDLE_VALUE)
      CloseHandle(my_pipeout[1]);
    if (my_pipeerr[0] != INVALID_HANDLE_VALUE)
      CloseHandle(my_pipeerr[0]);
    if (my_pipeerr[1] != INVALID_HANDLE_VALUE)
      CloseHandle(my_pipeerr[1]);
  }
  return fptr;

#else  /* !WIN32 || !WITH_QT */
  return popen(cmd,mode);
#endif /* !WIN32 || !WITH_QT */
}

/*------------------------------------------------------------------------------
  Ersatz fuer die Routine 'pclose()' unter WIN32
------------------------------------------------------------------------------*/
int
#ifdef __STDC__
pt_pclose(FILE *fle)
#else
pt_pclose(fle) FILE *fle;
#endif
{
#if defined(WIN32) && defined(WITH_QT)
  if (fle)
  {
    (void)fclose(fle);

    CloseHandle(my_pipeerr[0]);
    if (my_popenmode == 'r')
      CloseHandle(my_pipein[1]);
    else
     CloseHandle(my_pipeout[0]);
    return 0;
  }
  return -1;
#else  /* !WIN32 || !WITH_QT */
  return pclose(fle);
#endif /* !WIN32 || !WITH_QT */
}


---------------------------------------------------
Tel.:   (49)7150/393394    Parity Software GmbH
Fax.:   (49)7150/393351    Stuttgarter Strasse 42/3
E-Mail: kk@parity-soft.de  D-71701 Schwieberdingen
Web:    www.parity-soft.de
---------------------------------------------------