Article View: borland.public.cppbuilder.nativeapi
Article #3141Named pipe WriteFile problem
From: T. Meyer
Date: Fri, 18 Aug 2006 12:35
Date: Fri, 18 Aug 2006 12:35
104 lines
4286 bytes
4286 bytes
Hello,
I use a named pipe to write something to my pipe server.
I want to write just one byte into the pipe so I use following code:
#define BUFSIZE 512
...
char chBuf[BUFSIZE] = "HELLO";
bool fSuccess = false;
DWORD dwBytesWritten = 0, dwTotalBytesWritten = 0, dwWait;
HANDLE PipeWriteEvt = CreateEvent(
NULL, // default security attribute
TRUE, // manual-reset event
FALSE, // initial state = signaled
NULL); // unnamed event object
OL.Internal = 0;
OL.InternalHigh = 0;
OL.Offset = 0;
OL.OffsetHigh = 0;
OL.hEvent = PipeWriteEvt;
fSuccess = WriteFile(
hPipe, // pipe handle
chBuf, // buffer to write
1, // Write 1 byte only
&dwBytesWritten, // number of bytes written
&OL); // NULL if not overlapped
if I look on the pipe server I see that the whole buffer was send (HELLO)
even if I set nNumberOfBytesToWrite to 1.
I do further checkings after I use WriteFile(...). So I check if the no of
bytes written equals the length of word to send (here HELLO = 5 bytes to
send).
If not I repeat calling WriteFile(...) until they are equal.
But on my server I get now five times the whole word HELLO instead of H, E,
L, L, O, even if I send only one byte each time.
Seems that the WriteFile function doesnt see that I set bytesToSend to 1?
If I look into dwBytesWritten after WriteFile(...) it says 1, but on the
server I get all 5 bytes!?!
What is wrong with my code?
Here the code what I do after 1st time calling WriteFile:
dwTotalBytesWritten = dwBytesWritten;
if ((!fSuccess) || (dwTotalBytesWritten != MsgLength)) // MsgLenght is
calculates, it equals 5 here
{
// ReadFile not completed, so wait until PipeEvt signaled
DWORD dwErr;
dwErr = GetLastError();
if ((dwErr != ERROR_IO_PENDING) && (dwErr != ERROR_SUCCESS))
{
ShowError(dwErr);
}
else
{
while((!fSuccess) || (dwTotalBytesWritten != MsgLength))
{
Application->ProcessMessages();
// Wait until PipeReadEvt singnals that some or all
// of bytes are available
dwWait = WaitForSingleObject(
PipeWriteEvt, // handle of event objects
1000); // wait 1 sec.
switch (dwWait)
{
case WAIT_TIMEOUT: TimeNeeded += 1;
break;
case WAIT_OBJECT_0: ResetEvent(PipeWriteEvt);
fSuccess = WriteFile(
hPipe, // pipe handle
chBuf, // buffer to write
1, , // write 1 byte only
&dwBytesWritten, // number of bytes
written
&OL); // NULL if not
overlapped
dwTotalBytesWritten += dwBytesWritten;
break;
}
Application->ProcessMessages();
if (TimeNeeded >= TimeOut)
{
ret = false;
break;
}
}
}
}
...
CloseHandle(PipeWriteEvt);
CloseHandle(hPipe);
...
Thanks for any help!
Technical Details
Message-ID:
<44e5b421$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,dd77ae57da4f6d50,start X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "T. Meyer" <timon@mustek.de> Newsgroups: borland.public.cppbuilder.nativeapi Subject: Named pipe WriteFile problem Date: Fri, 18 Aug 2006 14:35:39 +0200 Lines: 106 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2670 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670 X-RFC2646: Format=Flowed; Original NNTP-Posting-Host: 80.132.166.197 Message-ID: <44e5b421$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1155904545 80.132.166.197 (18 Aug 2006 05:35:45 -0700) Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail