Wow - I’ve really gone through the wringer with the Overlapped structure lately. Using the Overlapped structure in C# is anything but intuitive. The postings here helped me along and gave me some helpful tips:
http://bytes.com/groups/net-c/556537-using-pipes-c-overlapped-i-o
Before I read those posts, I was trying to use the NativeOverlapped structured by just calling ‘new’ on it. The results were highly misleading. All of the response values came back as successful (or as IO Pending - as they should with Overlapped calls). But the ‘bytesRead’ parameter was returning 0!!! What was even more odd was that I was still getting valid data - even with a ‘bytesRead’ result of 0! I couldn’t figure it out. To further add to the oddness, it totally worked on Vista with no problems (aside from bytes read being 0). It wasn’t until I was getting a mysterious ExecutionEngineException on XP that I dug into it further and changed my logic to mirror the method used in the postings. That worked like a charm.
Further playing around led me to find another interesting result. I didn’t actually need to do all of the complicated “pack” logic in the postings above. I actually just needed to pass the NativeOverlapped object around as a reference parameter and the ExecutionEngineException went away. I’m guessing this has to do with the fact that since the event handle was getting set/reset internal in the OS, the OS or .NET layer did not like the object being a non-reference object with stuff inside of it changing. In any case, both the “pack”, unsafe pointer approach, as well as the reference approach solved the issue.