none
Invalid pipe handle in C# RRS feed

  • Question

  • hi everybody , i created an application with C , i created a pipe , after i create a child process and  passed pipe handles into a child process which written C# . My problem is using (PipeStream pipeClient =
                    new AnonymousPipeClientStream(PipeDirection.In,handle)) threw an exception "Invalid pipe handle" , i tried many times different ways but i didnt find solution , what is problem at my code :)

    #include <stdlib.h>
    #include <stdio.h>
    #include <io.h>
    #include <fcntl.h>
    #include <process.h>
    #include <math.h>
    
    enum PIPES { READ, WRITE }; /* Constants 0 and 1 for READ and WRITE */
    #define NUMPROBLEM 8
    
    int main( int argc, char *argv[] )
    {
    
       int fdpipe[2];
       char hstr[20];
       int pid, problem, c;
       int termstat;
    
       
          setvbuf( stdout, NULL, _IONBF, 0 );
    
          /* Open a set of pipes */
          if( _pipe( fdpipe, 256, O_BINARY ) == -1 )
              exit( 1 );
    
    
       
         itoa( fdpipe[READ], hstr, 10 );
          if( ( pid = spawnl( P_NOWAIT, "Csharp.exe", "Csharp.exe", 
                hstr, NULL ) ) == -1 )
              printf( "Spawn failed" );
    
         
          for( problem = 1000; problem <= NUMPROBLEM * 1000; problem += 1000)
          {
    
             printf( "Son, what is the square root of %d?\n", problem );
             write( fdpipe[WRITE], (char *)&problem, sizeof( int ) );
    
          }
    
          /* Wait until spawned program is done processing. */
          _cwait( &termstat, pid, WAIT_CHILD );
          if( termstat & 0x0 )
             printf( "Child failed\n" );
    
    
          close( fdpipe[READ] );
          close( fdpipe[WRITE] );
    }

    C# ( child process)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.IO.Pipes;
    using Microsoft.Win32.SafeHandles;
    
    namespace AnonymPipe1
    {
        class Program
        {
            public static void ReadPipe(string handle)
            {
    
                File.AppendAllText("c:\\log.txt", "ReadPipe : " + handle + "\n");
    
                try
                {
                    using (PipeStream pipeClient =
                    new AnonymousPipeClientStream(PipeDirection.In,handle)) // ERROR :) i cant use my main pipe handle in c#
                    {
                        
                        
                    }
                }
                catch (Exception ex)
                {
                    File.AppendAllText("c:\\log.txt", ex.Message);
                }
    
                tw.Close();
            }
    
            public static void Main(string[] args)
            {
                ReadPipe(args[0]);
            }
        }
    }


    • Edited by warchieft Thursday, September 27, 2012 2:17 PM edit
    Thursday, September 27, 2012 2:13 PM

Answers