Problem in ReadFile for Pipes

  • Thread starter Mustafa Ahmad Malik
  • Start date
M

Mustafa Ahmad Malik

Hello,
I have wrapped the Win32 pipes function in C#. I have created named pipe
with PIPE_WAIT in pipemode parameter like this

pipehandle = CreateNamedPipe(_pipeName,
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
PIPE_UNLIMITED_INSTANCES,
4096,
4096,
NMPWAIT_WAIT_FOREVER,
pipeSecurityDescriptor);

This mean that pipe should be blocking and any read or write operation on
pipe should be blocked until there is something to be read/written. But when
i call ReadFile function it returns immediately with false and
GetLastError() function return ERROR_PIPE_LISTENING. My understanding is, it
should not happen because of PIPE_WAIT in CreateNamedPipe(). Here is the
call to ReadFile :

byte[] intBuffer = new byte[4];
byte[] _numRead = new byte[4];
bool rVal = ReadFile(pipehandle, intBuffer, 4, numRead, 0);

Is it a bug of .NET or there is something that i'm missing. Should i have to
set blocking mode through some other API explicitly. Also, here is the
declaration of wrapped functions:

[DllImport("kernel32.dll")]
public static extern int CreateNamedPipe(String lpName, // pipe name
uint dwOpenMode, // pipe open mode
uint dwPipeMode, // pipe-specific modes
uint nMaxInstances, // maximum number of instances
uint nOutBufferSize, // output buffer size
uint nInBufferSize, // input buffer size
uint nDefaultTimeOut, // time-out interval
IntPtr pipeSecurityDescriptor // SD);

[DllImport("kernel32.dll")]
public static extern bool ReadFile(int hFile, // handle to file
byte[] lpBuffer, // data buffer
uint nNumberOfBytesToRead, // number of bytes to read
byte[] lpNumberOfBytesRead, // number of bytes read
uint lpOverlapped // overlapped buffer);

Thanks
Mustafa Ahmad Malik.
 

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