C# and Named pipes
-
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!!
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 theCreateNamedPipe
function and create aSECURITY_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 theCreateNamedPipe
method, you could probably change the param to a generic object and passnull
when calling the method. See the SDK documentation forCreateNamedPipe
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 forFileStream
: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-----