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
  1. Home
  2. General Programming
  3. C#
  4. C# and Named pipes

C# and Named pipes

Scheduled Pinned Locked Moved C#
csharpquestion
2 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.
  • S Offline
    S Offline
    Spiros
    wrote on last edited by
    #1

    Hi everybody Does anyone know if there is a way to implement named pipes using C#? Thank you very much!! Spiros Prantalos Miami the place to be!!

    H 1 Reply Last reply
    0
    • S Spiros

      Hi everybody Does anyone know if there is a way to implement named pipes using C#? Thank you very much!! Spiros Prantalos Miami the place to be!!

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      First, let me just say that .NET Remoting is the recommended way for inter-process communication - even over disparate networks like the Internet. See the System.Runtime.Remoting namespace for more information, or read the MS Press book, ".NET Remoting" (it's pretty good for both beginners and advanced developers alike). Having said that, you have to P/Invoke the CreateNamedPipe function and create a SECURITY_ATTRIBUTES structure like so:

      [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
      public static extern IntPtr CreateNamedPipe(string name, int openMode, int pipeMode,
      int maxInstances, int outBufferSize, int inBufferSize,
      int defaultTimeout, ref SECURITY_ATTRIBUTES);
       
      [StructLayout(LayoutLind.Sequential)]
      public struct SECURITY_ATTRIBUTES
      {
      public int length;
      public IntPtr securityDescriptor;
      public bool inheritHandle;
      }

      If you don't want to worry about the SECURITY_ATTRIBUTES struct in the CreateNamedPipe method, you could probably change the param to a generic object and pass null when calling the method. See the SDK documentation for CreateNamedPipe for details about what else to pass. Now, that returns you a handle (IntPtr in .NET). You can use that in a call to the constructor for FileStream:

      IntPtr handle = CreateNamedPipe(@"\\.\pipe\Name123", ...);
      Stream s = new FileStream(handle, FileAccess.ReadWrite);

      Now you've got a Stream to work with using managed code.

      -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

      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