Re: A question on reading mailslot

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: cyberninja (cyberninja_at_discussions.microsoft.com)
Date: 06/11/04


Date: Fri, 11 Jun 2004 09:48:01 -0700


"Gary Chanson" wrote:

>
> "cyberninja" <cyberninja@discussions.microsoft.com> wrote in message
> news:50722032-A643-4B1E-8B9A-6E80C3AE112E@microsoft.com...
> > When reading a stream from a mailslot, the following Win32 API is used:
> >
> > BOOL ReadFile(HANDLE hFile,
> > LPVOID lpBuffer,
> > DWORD nNumberOfBytesToRead,
> > LPDWORD lpNumberOfBytesRead,
> > LPOVERLAPPED lpOverlapped
> > );
> >
> > However, there are some difference between calling ReadFile for a disk
> file and for a mailslot stream. If hFile is a handle for the a mailslot, if
> the param of "nNumberOfBytesToRead" is less than the total message size (you
> can retrieve it from GetMailslotInfo()), nothing will be read and
> *lpNumberOfBytesRead will be 0.
> >
> > This situation raise a problem: if *lpNumberOfBytesRead is less than
> nNumberOfBytesToRead, what can we do except return an error? As you know, in
> file stream, you still get something and *lpNumberOfBytesRead will not be 0,
> so you can continue reading the handle in a loop until all data are read. In
> mailslot stream, if you do so, in the next loop you still get 0 in
> *lpNumberOfBytesRead.
> >
> > Any suggestion for handling this situation.
>
> Retry with a progressively larger buffer until either you can read the
> message or can't allocate a big enough buffer. Of course, that assumes that
> you check first for the presence of the message before trying to read it.
>
> --
> -GJC [MS Windows SDK MVP]
> -Software Consultant (Embedded systems and Real Time Controls)
> - http://www.mvps.org/ArcaneIncantations/consulting.htm
> -gchanson@mvps.org
>
>

Actually the difficult situation is: when the reading buffer size is large enough, is it possible for ReadFile() to output a *lpNumberOfBytesRead, which is less than nNumberOfBytesToRead? If it is possible, how should I do? Trying progressively larger buffer will not work because nNumberOfBytesToRead and lpBuffer is large enough.

Thanks,

cyberninja