🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #3282

Re: NamedPipe problem

#3282
From: Remy Lebeau (TeamB)
Date: Thu, 17 Aug 2006 16:59
70 lines
3486 bytes
"T. Meyer" <timon@mustek.de> wrote in message
news:44e47a8e$1@newsgroups.borland.com...

> Why writing to the server wokrs but reading (over same pipe)
> causes: 'The handle is invalid!'?

You did not specify FILE_FLAG_OVERLAPPED when calling CreateFile().

> I use overlapped IO to ensure my program can do
> further things if pipe reading not finsihed at once.
> But I dont know the WHEN the reading is complete.

The is what the TEvent is for.  It is signaled when an overlapped operation
is finished.  You can then call GetOverlappedResult() to get the result of
the operation.

> if ReadFile returns back I catch the error code to get information
> what happend. If error code is ERROR_IO_PENDING the I use
> WaitForMultipleObjects and GetOverlappedResult but then I do
> not know what is the trigger to say ReadFile has finsihed the reading?

Yes, you do, because you are already waiting for the reading to finish.
When WaitForMultiObjects() returns WAIT_OBJECT_0, the event has been
signaled, and then you are calling GetOverlappedResult() to get the result
of the reading.

Why are you using WaitForMultipleObjects() for a single object?  Use
WaitForSingleObject() instead.  Better yet, TEvent has its own WaitFor()
method that you can use.

>       exit(-1);

DO NOT use exit() in a VCL application!  It does not allow the VCL to clean
up after itself on shutdown.  Use Application->Terminate() instead, or
alternatively just Close() the MainForm.

You are also leaking all of the objects and handles that you created.
Always free your objects, and close your handles.

>   // Send a message to the pipe server.
>   fSuccess = WriteFile(

Why are you trying to use Overlapped I/O for reading but not for writing?
Writing has just as much a chance of blocking as reading does.  In fact, if
you want to use Overalpped I/O for reading, then you have to specify
FILE_FLAG_OVERLAPPED in CreateFile(), which in turn will require that you
use Overlapped I/O for WriteFile() as well.  WriteFile() will fail if you
don't pass it an OVERLAPPED structure for a file handle that uses Overlapped
I/O.

> When is the ReadFile completed to set ReadCompleted to true to leave this
loop?

When the event has become signaled, causing WaitFor...() to return
WAIT_OBJECT_0.

> void __fastcall TForm1::ShowError(DWORD Err)

You don't need to call FormatMessage() yourself.  The VCL has its own
SysErrorMessage() function that does that for you.

    void __fastcall TForm1::ShowError(DWORD Err)
    {
        Application->MessageBox(SysErrorMessage(Err).c_str(),
"GetLastError", MB_OK | MB_ICONINFORMATION);
    }


Gambit
Technical Details
Message-ID: <44e4a098$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <44e47a8e$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,facc6570cb7e9402
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <44e47a8e$1@newsgroups.borland.com>
Subject: Re: NamedPipe problem
Date: Thu, 17 Aug 2006 09:59:04 -0700
Lines: 72
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 68.69.17.41
Message-ID: <44e4a098$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1155834008 68.69.17.41 (17 Aug 2006 10:00:08 -0700)
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail