tags:

views:

33

answers:

1

I'm having trouble using pipes in a larger application and so I created a minimal test application to investigate the problem.

I'm creating a pipe:

Dim sa As SECURITY_ATTRIBUTES
Dim R As Long
sa.nLength = Len(sa)
sa.bInheritHandle = 1
R = CreatePipe(hRead, hWrite, sa, 0) //hRead declared globally
Debug.Print "CreatePipe: " & R

and then I'm reading from it:

Const BufSize As Long = 1024 
Dim Buffer(BufSize) As Byte
Dim lBytesRead As Long
Dim R As Long
R = ReadFile(hRead, Buffer(0), BufSize, lBytesRead, 0)
Debug.Print "ReadFile: " & R
Debug.Print Err.LastDllError

Now as far as I understand the ReadFile call should block, because nobody has written any data into the pipe.

The problem: That only happens iff I put the code right after the CreatePipe code. As soon as I put it into a separate function, it fails with the last error being ERROR_INVALID_HANDLE. (I confirmed that the value of hRead does not change)

I have absolutely no idea what's causing this kind of behaviour.

A: 

I found the solution myself. It was a rather stupid beginner's mistake, but I found some users having the same problem and since the error symptoms where not at all pointing in the right direction, I'll post my solution.

First I did further refactorings to dumb down the code even more. After making each and every variable global, I finally got a "invalid calling convention" error on the ReadFile call.

To make a long story short, the import declaration and the actual call of ReadFile were wrong for the last parameter (the OVERLAPPED parameter)

This is what I did:

Declare Function ReadFile ..., lpOverlapped As Any) As Long
Call ReadFile(..., 0)

Correct would be any one of these:

Declare Function ReadFile ..., lpOverlapped As Any) As Long
Call ReadFile(..., ByVal 0)

Declare Function ReadFile ..., ByVal lpOverlapped As Long) As Long
Call ReadFile(..., 0)

Declare Function ReadFile ..., lpOverlapped As OVERLAPPED) As Long
Call ReadFile(..., myVariableOfTypeOverlapped)
DR
@DR: I made a quick edit to make your answer clearer. "Beziehungsweise" doesn't translate into English very well: "and" is better than "resp." in this case. ;-)
Mike Spross