Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Named pipes: C# server, C++ client

Named pipes: C# server, C++ client

Scheduled Pinned Locked Moved C#
csharpc++sysadminquestion
14 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Richard Andrew x64R Richard Andrew x64

    You might want to try Sysinternals' Process Explorer. It can enumerate every kernel object (that includes pipes) that exists inside a process. This way you could be sure the pipe name is being created the way you intend.

    The difficult we do right away... ...the impossible takes slightly longer.

    C Offline
    C Offline
    Crazy Joe Devola
    wrote on last edited by
    #5

    thank you. I have Process Explorer but I don't know how to see list of objects for a process. I used the search option to search for "mypipe". It shows that the server has a handle to \Device\NamedPipe\pipe\mypipe . I tried to check the client to open "\\Device\\NamedPipe\\pipe\\mypipe" but that didn't help (I didn't expect it to work anyway).

    C 1 Reply Last reply
    0
    • C Crazy Joe Devola

      thank you. I have Process Explorer but I don't know how to see list of objects for a process. I used the search option to search for "mypipe". It shows that the server has a handle to \Device\NamedPipe\pipe\mypipe . I tried to check the client to open "\\Device\\NamedPipe\\pipe\\mypipe" but that didn't help (I didn't expect it to work anyway).

      C Offline
      C Offline
      Crazy Joe Devola
      wrote on last edited by
      #6

      This is how the client opens the pipe:

      hPipe=CreateFile("\\\\.\\pipe\\mypipe",
      GENERIC_WRITE,//GENERIC_READ | GENERIC_WRITE,
      0,
      NULL,
      OPEN_EXISTING,
      0,
      NULL) ;

      if ( hPipe!=INVALID_HANDLE_VALUE)
      {
      return 0; //Success!!!
      }

      err = GetLastError();
      if(err!=ERROR_PIPE_BUSY)
      {
      printf("Could not open pipe, GetLastError=%u\n", err);
      return -1 ;
      }

      Create file returns an invalid handle. GetLastError() returns 2.

      Richard Andrew x64R 1 Reply Last reply
      0
      • C Crazy Joe Devola

        This is how the client opens the pipe:

        hPipe=CreateFile("\\\\.\\pipe\\mypipe",
        GENERIC_WRITE,//GENERIC_READ | GENERIC_WRITE,
        0,
        NULL,
        OPEN_EXISTING,
        0,
        NULL) ;

        if ( hPipe!=INVALID_HANDLE_VALUE)
        {
        return 0; //Success!!!
        }

        err = GetLastError();
        if(err!=ERROR_PIPE_BUSY)
        {
        printf("Could not open pipe, GetLastError=%u\n", err);
        return -1 ;
        }

        Create file returns an invalid handle. GetLastError() returns 2.

        Richard Andrew x64R Offline
        Richard Andrew x64R Offline
        Richard Andrew x64
        wrote on last edited by
        #7

        You do know that backslashes need to be escaped in C++, right? It should be:

        hPipe=CreateFile("\\\\.\\pipe\\mypipe",
        GENERIC_WRITE,//GENERIC_READ | GENERIC_WRITE,
        0,
        NULL,
        OPEN_EXISTING,
        0,
        NULL) ;

        The difficult we do right away... ...the impossible takes slightly longer.

        C 1 Reply Last reply
        0
        • Richard Andrew x64R Richard Andrew x64

          You do know that backslashes need to be escaped in C++, right? It should be:

          hPipe=CreateFile("\\\\.\\pipe\\mypipe",
          GENERIC_WRITE,//GENERIC_READ | GENERIC_WRITE,
          0,
          NULL,
          OPEN_EXISTING,
          0,
          NULL) ;

          The difficult we do right away... ...the impossible takes slightly longer.

          C Offline
          C Offline
          Crazy Joe Devola
          wrote on last edited by
          #8

          Yes i do :-) for some reason the codeproject shows just 1 backslash. I do have it like this:

          "\\\\.\\pipe\\mypipe"

          Richard Andrew x64R 1 Reply Last reply
          0
          • C Crazy Joe Devola

            Yes i do :-) for some reason the codeproject shows just 1 backslash. I do have it like this:

            "\\\\.\\pipe\\mypipe"

            Richard Andrew x64R Offline
            Richard Andrew x64R Offline
            Richard Andrew x64
            wrote on last edited by
            #9

            Let's see the code that creates the pipes in both projects.

            The difficult we do right away... ...the impossible takes slightly longer.

            C 1 Reply Last reply
            0
            • Richard Andrew x64R Richard Andrew x64

              Let's see the code that creates the pipes in both projects.

              The difficult we do right away... ...the impossible takes slightly longer.

              C Offline
              C Offline
              Crazy Joe Devola
              wrote on last edited by
              #10

              Server: C# NamedPipeServerStream pipeServer = new NamedPipeServerStream("\\\\.\\pipe\\mypipe", PipeDirection.In, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous); // Wait for a connection pipeServer.BeginWaitForConnection(new AsyncCallback(WaitForConnectionCallBack), pipeServer); Client: C++

              	hPipe=CreateFile("\\\\\\\\\\\\\\\\.\\\\\\\\pipe\\\\\\\\mypipe", 
              	                 GENERIC\_READ | GENERIC\_WRITE,
              	                 0,
              	                 NULL,
              	                 OPEN\_EXISTING,
              	                 0,
              	                 NULL) ;
              	if ( hPipe!=INVALID\_HANDLE\_VALUE) 
              	{
              		return 0; //Success!!!
              	}
              
              	err = GetLastError();
              	if(err!=ERROR\_PIPE\_BUSY)
              	{
              		printf("Could not open pipe, GetLastError=%u\\n", err);
              		return -1 ;
              	}
              
              	printf("Could not open pipe, GetLastError=%u. WaitNamedPipe.\\n", err);
              	if(! WaitNamedPipe(pipe\_name,2000))
              	{
              		printf("Could not open pipe\\n");
              		return -1 ;
              	}
              

              again: C++ client with C++ server - works. C# client with C# server - works. C++ client with C# server - does not works. Thank you.

              Richard Andrew x64R 1 Reply Last reply
              0
              • C Crazy Joe Devola

                Server: C# NamedPipeServerStream pipeServer = new NamedPipeServerStream("\\\\.\\pipe\\mypipe", PipeDirection.In, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous); // Wait for a connection pipeServer.BeginWaitForConnection(new AsyncCallback(WaitForConnectionCallBack), pipeServer); Client: C++

                	hPipe=CreateFile("\\\\\\\\\\\\\\\\.\\\\\\\\pipe\\\\\\\\mypipe", 
                	                 GENERIC\_READ | GENERIC\_WRITE,
                	                 0,
                	                 NULL,
                	                 OPEN\_EXISTING,
                	                 0,
                	                 NULL) ;
                	if ( hPipe!=INVALID\_HANDLE\_VALUE) 
                	{
                		return 0; //Success!!!
                	}
                
                	err = GetLastError();
                	if(err!=ERROR\_PIPE\_BUSY)
                	{
                		printf("Could not open pipe, GetLastError=%u\\n", err);
                		return -1 ;
                	}
                
                	printf("Could not open pipe, GetLastError=%u. WaitNamedPipe.\\n", err);
                	if(! WaitNamedPipe(pipe\_name,2000))
                	{
                		printf("Could not open pipe\\n");
                		return -1 ;
                	}
                

                again: C++ client with C++ server - works. C# client with C# server - works. C++ client with C# server - does not works. Thank you.

                Richard Andrew x64R Offline
                Richard Andrew x64R Offline
                Richard Andrew x64
                wrote on last edited by
                #11

                This is just a guess, but could it be that since you have specified both GENERIC_READ and GENERIC_WRITE for the client, then you need to open the server as "InOut"?

                The difficult we do right away... ...the impossible takes slightly longer.

                C 1 Reply Last reply
                0
                • Richard Andrew x64R Richard Andrew x64

                  This is just a guess, but could it be that since you have specified both GENERIC_READ and GENERIC_WRITE for the client, then you need to open the server as "InOut"?

                  The difficult we do right away... ...the impossible takes slightly longer.

                  C Offline
                  C Offline
                  Crazy Joe Devola
                  wrote on last edited by
                  #12

                  I thought of that. I tried changing the server side to InOut - it did not help. i also tried changing the client to GENERIC_WRITE without GENERIC_READ - it didn't work either. Thank you for all the advise, tips and time you spent trying to help - much appreciated.

                  1 Reply Last reply
                  0
                  • C Crazy Joe Devola

                    I wrote 2 pairs of named pipe client/server programs: 1st pair in C# (.NET 4) 2nd pair in C++ (un-managed) All 4 test programs use the same pipe name \\\\.\pipe\mypipe The C# pair work fine with each other - I send a message from the client and it is received by the server. The C++ pair work also fine with each other. But... when I try to run the C# client with the C++ server, or the C++ client with the C# server - then it doesn't work. The client is unable to connect to the server. Is there something preventing the C++ client from working with the .NET server? Should it work? Thank you.

                    C Offline
                    C Offline
                    Crazy Joe Devola
                    wrote on last edited by
                    #13

                    I think the problem was found.... On stackoverflow, someone posted this answer: C#'s NamedPipeClientStream, NamedPipeServerStream automatically append "\.\\pipe\" to the name I changed the C# code to open the pipe just as "mypipe" and now the connection is successful.

                    Richard Andrew x64R 1 Reply Last reply
                    0
                    • C Crazy Joe Devola

                      I think the problem was found.... On stackoverflow, someone posted this answer: C#'s NamedPipeClientStream, NamedPipeServerStream automatically append "\.\\pipe\" to the name I changed the C# code to open the pipe just as "mypipe" and now the connection is successful.

                      Richard Andrew x64R Offline
                      Richard Andrew x64R Offline
                      Richard Andrew x64
                      wrote on last edited by
                      #14

                      Glad you got it sorted. :)

                      The difficult we do right away... ...the impossible takes slightly longer.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups