[python-win32] overlaped namedpipe client with CreateFile instead of CallNamedPipe

Mark Hammond mhammond at skippinet.com.au
Sun Jun 29 03:23:29 CEST 2008


> In M.Hammonds "python programming on win32" chapter 18 there is a pipe
> service and a client [1] which use CallNamedPipe. I changed the service
> to
> accept messages bigger than its buffer [2]. Does anybody know how to
> rewrite
> the client with CreateFile method so it can accept bigger that buffer
> messages too?
> My attempt [3] which mimics
> http://msdn.microsoft.com/en-us/library/aa365592(VS.85).aspx fails with
> following message:
> pywintypes.error: (121, 'WaitNamedPipe', 'The semaphore timeout period
> has expired.')
...
> [2]--------------------------------------------------------------
> hr, data = win32file.ReadFile(pipeHandle, 256)
> 	if (win32api.GetLastError() == winerror.ERROR_MORE_DATA):
> 		t,t,remains = win32pipe.PeekNamedPipe(pipeHandle,0)
> 		hr,d2 = win32file.ReadFile(pipeHandle, remains)
> 		data = data + d2

A loop like the following works for me:

        hr = winerror.ERROR_MORE_DATA
        got = []
        while hr == winerror.ERROR_MORE_DATA:
            hr, data = ReadFile(hPipe, 256)
            got.append(data)
        data = ''.join(got)

I can also make code like the MSDN example work:

            hr, data = ReadFile(hPipe, 256)
            if hr == winerror.ERROR_MORE_DATA:
                t,t,remains = PeekNamedPipe(hPipe,0)
                print "remains is", remains
                hr,d2 = ReadFile(hPipe, remains)
                data = data + d2

But that will cause the second ReadFile to read the rest of the message
regardless of the size.  It should be fairly easy to modify that to a loop,
but I'm not sure what PeekNamedPipe offers in this case (ie, just doing
blocking reads should be fine in this case, as you generally can't start
processing until the entire message is received anyway)

If you keep having problems making it work, please attach your complete
modified test code so we can run it and check it out.

Cheers,

Mark



More information about the python-win32 mailing list