Named pipe class

J

John J. Hughes II

Ok I am trying to make a named pipe class. I can open the server no problem
but when I open the client using CreateFile (which works) the thread then
locks on the SetNamedPipeHandleState. Basically I call the function and it
never returns. I am using the basic example in the online help. I would
expect either an error or success, not go to neverland. All the defines are
based on the SDK include file copied over.

Regards,
John

private string PipeName = @"\\.\pipe\myPipe";
private IntPtr hPort;

public PipeState Open()
{

UInt32 dwDesiredAccess = Win32Pipe.GENERIC_READ |
Win32Pipe.GENERIC_WRITE;
UInt32 dwSharedMode = 0;
UInt32 dwCreationDisposition = Win32Pipe.OPEN_EXISTING;
UInt32 dwFlags = 0;

this.hPort = Win32Pipe.CreateFile(this.PipeName,
dwDesiredAccess,
dwSharedMode,
IntPtr.Zero,
dwCreationDisposition,
dwFlags,
IntPtr.Zero);

if(this.hPort == (IntPtr)Win32Pipe.INVALID_HANDLE_VALUE)
{
int err = Marshal.GetLastWin32Error();
if(err == Win32Pipe.ERROR_PIPE_BUSY)
return PipeState.Busy;

Debug.WriteLine("Error opening client:" + err.ToString());
return PipeState.Error;
}


this.pipeState = PipeState.Opened;
Debug.WriteLine("Clent opened");

UInt32 type = Win32Pipe.PIPE_READMODE_MESSAGE | Win32Pipe.PIPE_WAIT;
bool retVal = Win32Pipe.SetNamedPipeHandleState(this.hPort, type, 0, 0);
if(!retVal)
{
Debug.WriteLine("Message Set Error:" +
Marshal.GetLastWin32Error().ToString());
return PipeState.Error;
}

return PipeState.Opened;
}


public class Win32Pipe
{
...
[DllImport("kernel32.dll")]
internal static extern bool SetNamedPipeHandleState(
IntPtr hNamedPipe,
UInt32 lpMode,
UInt32 lpMaxCollectionCount,
UInt32 lpCollectDataTimeout);

[DllImport("kernel32.dll")]
internal static extern IntPtr CreateFile(
string lpFileName,
UInt32 dwDesiredAccess,
UInt32 dwShareMode,
IntPtr lpSecurityAttributes,
UInt32 dwCreationDisposition,
UInt32 dwFlagsAndAttributes,
IntPtr hTemplateFile );

}
 
M

Mattias Sjögren

John,
[DllImport("kernel32.dll")]
internal static extern bool SetNamedPipeHandleState(
IntPtr hNamedPipe,
UInt32 lpMode,
UInt32 lpMaxCollectionCount,
UInt32 lpCollectDataTimeout);

The last three parameters should be ref UInt32, or possibly IntPtr
passed by value if you want to pass a NULL pointer.



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top