August 11, 2022, 05:04:04 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


CreatePipe

Started by Todd Riggins, September 16, 2007, 04:13:26 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Todd Riggins

September 16, 2007, 04:13:26 PM Last Edit: September 16, 2007, 04:15:19 PM by Todd Riggins
Learning and sharing ;)

It's cool how quick one can translate C code to Aurora code...

Here's a pipe example from msdn...

I first searched up createpipe here:
http://msdn2.microsoft.com/en-us/library/aa365152.aspx

then followed the code example here:
http://msdn2.microsoft.com/en-us/library/ms682499.aspx

It isn't really all perty look'n , but it works  :D

Two programs need to be compiled.
Compile msdnpipechild.src to a console type exe first.
Then compile msdnpipemain.src as a console type exe.
I say that cause when you compile and run msdnpipemain, msdnpipemain.exe will
want to create a new process of msdnpipechild.exe.

Ok mr./mrs./miss programmer, study it to see what it does. Referance links provided above for more info. ;)

msdnpipemain.src

#include "windows.inc"
#include "tchar.inc"
#include "stdio.inc"

#define BUFSIZE 4096

HANDLE hChildStdinRd, hChildStdinWr, 
   hChildStdoutRd, hChildStdoutWr,
   hInputFile, hStdout;

global sub main()
{
   SECURITY_ATTRIBUTES saAttr;
   BOOL fSuccess;

// Set the bInheritHandle flag so pipe handles are inherited.

   saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
   saAttr.bInheritHandle = TRUE;
   saAttr.lpSecurityDescriptor = NULL;

// Get the handle to the current STDOUT.

   hStdout = GetStdHandle(STD_OUTPUT_HANDLE);

// Create a pipe for the child process's STDOUT.

   if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
      ErrorExit("Stdout pipe creation failed\n");

// Ensure the read handle to the pipe for STDOUT is not inherited.

   SetHandleInformation( hChildStdoutRd, HANDLE_FLAG_INHERIT, 0);

// Create a pipe for the child process's STDIN.

   if (! CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
      ErrorExit("Stdin pipe creation failed\n");

// Ensure the write handle to the pipe for STDIN is not inherited.

   SetHandleInformation( hChildStdinWr, HANDLE_FLAG_INHERIT, 0);

// Now create the child process.
   
   fSuccess = CreateChildProcess();
   if (! fSuccess)
      ErrorExit("Create process failed with");

// Get a handle to the parent's input file.

  // if (argc == 1)
  //    ErrorExit("Please specify an input file");
string file;
file = FILEREQUEST("Select a file to read",0,1,"","*.*");

   printf( "\nContents of %s:\n\n", file);

   hInputFile = CreateFile(file, GENERIC_READ, 0, NULL,
      OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);

   if (hInputFile == INVALID_HANDLE_VALUE)
      ErrorExit("CreateFile failed");

// Write to pipe that is the standard input for a child process.

   WriteToPipe();

// Read from pipe that is the standard output for child process.

   ReadFromPipe();
writeln("Press Any Key To Close");
while getkey() = "";
   return 0;
}

Sub CreateChildProcess(),BOOL
{
   STRING sCmdline="msdnpipechild";
   PROCESS_INFORMATION piProcInfo;
   STARTUPINFO siStartInfo;
   BOOL bFuncRetn = FALSE;

// Set up members of the PROCESS_INFORMATION structure.

   ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );

// Set up members of the STARTUPINFO structure.

   ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
   siStartInfo.cb = sizeof(STARTUPINFO);
   siStartInfo.hStdError = hChildStdoutWr;
   siStartInfo.hStdOutput = hChildStdoutWr;
   siStartInfo.hStdInput = hChildStdinRd;
   siStartInfo.dwFlags |= STARTF_USESTDHANDLES;

// Create the child process.
   
   bFuncRetn = CreateProcess(NULL,
      sCmdline,     // command line
      NULL,          // process security attributes
      NULL,          // primary thread security attributes
      TRUE,          // handles are inherited
      0,             // creation flags
      NULL,          // use parent's environment
      NULL,          // use parent's current directory
      &siStartInfo,  // STARTUPINFO pointer
      &piProcInfo);  // receives PROCESS_INFORMATION
   
   if (bFuncRetn == 0)
   {
      ErrorExit("CreateProcess failed\n");
   }
   else
   {
      CloseHandle(piProcInfo.hProcess);
      CloseHandle(piProcInfo.hThread);
      return bFuncRetn;
   }
}

Sub WriteToPipe()
{
   DWORD dwRead, dwWritten;
   CHAR chBuf[BUFSIZE];

// Read from a file and write its contents to a pipe.

   int untilbreak=0;
   do
   {
     if (! ReadFile(hInputFile, chBuf, BUFSIZE, &dwRead, NULL) ||
    dwRead == 0)
{
untilbreak=1;
}
else
{
if (! WriteFile(hChildStdinWr, chBuf, dwRead,
&dwWritten, NULL))
{
untilbreak=1;
}
}
   } until untilbreak=1;

// Close the pipe handle so the child process stops reading.

   if (! CloseHandle(hChildStdinWr))
      ErrorExit("Close pipe failed\n");
}

Sub ReadFromPipe()
{
   DWORD dwRead, dwWritten;
   CHAR chBuf[BUFSIZE];

// Close the write end of the pipe before reading from the
// read end of the pipe.

   if (!CloseHandle(hChildStdoutWr))
      ErrorExit("Closing handle failed");

// Read output from the child process, and write to parent's STDOUT.
   int untilbreak=0;
   do
   {
      if( !ReadFile( hChildStdoutRd, chBuf, BUFSIZE, &dwRead,
         NULL) || dwRead == 0)
{
untilbreak=1;
}
else
{
      if (! WriteFile(hStdout, chBuf, dwRead, &dwWritten, NULL))
{
untilbreak=1;
}
}
   }  until untilbreak=1;
}

Sub ErrorExit (LPSTR lpszMessage)
{
string txt;
sprintf(txt,"%s\n",lpszMessage);
messagebox(null,txt,"Error",MB_OK);

   ExitProcess(0);
}


msdnpipechild.src

#include "windows.inc"
#define BUFSIZE 4096

global sub main()
{
   CHAR chBuf[BUFSIZE];
   DWORD dwRead, dwWritten;
   HANDLE hStdin, hStdout;
   BOOL fSuccess;

   hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
   hStdin = GetStdHandle(STD_INPUT_HANDLE);
   if ((hStdout == INVALID_HANDLE_VALUE) ||
      (hStdin == INVALID_HANDLE_VALUE))
      ExitProcess(1);
 
   int untilbreak=0;
   do
   {
   // Read from standard input.
      fSuccess = ReadFile(hStdin, chBuf, BUFSIZE, &dwRead, NULL);
      if (!fSuccess || dwRead == 0)
  {
         untilbreak=1;
  }
  else
  {

// Write to standard output.
fSuccess = WriteFile(hStdout, chBuf, dwRead, &dwWritten, NULL);
if (!fSuccess)
untilbreak=1;
  }
   } until untilbreak=1;
}
Brought to you buy: http://www.exodev.com

ExMember001

hehe great, and thanx for sharing !
will look at it this evening when my head will be clear  :D

pistol350

September 16, 2007, 04:58:59 PM #2 Last Edit: September 16, 2007, 05:10:22 PM by pistol350
Thanks Todd for the sample code .
I don't know yet exactly what Pipes are but i guess a few MSDNing and a few GOOGLEing will help  ::)

Hi Krypt,
My own head is rather clear in the early morning,
I assume it is a question of geographic location  ::)
I'm just kidding  :D
Regards,

Peter B.

ExMember001

Pistol350,
you pipe a command line application when you want to get the output from the application console
example you launch a zip cmd line app, the app after process say "zip successful", with the pipe you can read this on the fly from the main program
like when Aurora ide call the parser and linker, when an error is found the error is send to the ide by a pipe ;)


pistol350

Thank you Krypt.
I am starting to get it.

Regards,

Peter B.