I am writting an application which talks ( reads and rwrites ) from a serial port. I am using overlapped file I/o

JoeBarryTwinstar
JoeBarryTwinstar used Ask the Experts™
on
I am writting an application which talks ( reads and rwrites ) from a serial port. I am using overlapped file I/o asynchronously. I keep getting an invalid handle message from my WaitForMultipleObjects code and cannot figure out why. Here is the offending code:

            DWORD result = ::WaitForMultipleObjects(2, waiters, FALSE, INFINITE);
            switch(result)
               { /* wait */
                case WAIT_OBJECT_0:  // shutdown
                              {
                    ::CancelIo(parms->hCom);
                    shutdown = ERROR_SUCCESS;  // clean shutdown
                    running = FALSE;
                    continue;
                              }
                case WAIT_OBJECT_0 + 1: // I/O complete
                              {
                    ok = ::GetOverlappedResult(parms->hCom, &ovl, &bytesRead, TRUE);  
                    if(!ok)
                       { /* GetOverlappedResult failed */
                        DWORD err = ::GetLastError();
                        running = FALSE;
                        continue;
                       } /* GetOverlappedResult failed */
                    break;
                              }
                default:
                    { /* trouble */
                     shutdown = ::GetLastError();

/////// This GetLastError returns 6, which means invalid handle. Is it the CEvent handle, or my serial port handle?

I am stumped.

Thanks
                     ASSERT(FALSE); // failure
                     running = FALSE;
                     continue;
                    } /* trouble */
               } /* wait */  
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
jkr
Top Expert 2012

Commented:
How is 'waiters' declared and how are you filling it?

Author

Commented:
It has 2 handles

I is 0x0000 and is named shutdown
the other looks like a valid handle and came from CreateEvent in an overlapped i/o structure
jkr
Top Expert 2012

Commented:
Err, what about some more - details. How is 'waiters' in

DWORD result = ::WaitForMultipleObjects(2, waiters, FALSE, INFINITE);

declared and how are you filling it?
Author of the Year 2009
Commented:
The point is that your "waiters" variable needs to be a pointer to an array of handles.  A common way to do this is:

      HANDLE ahWaiters[2];
      ahWaiters[0]= CreateEvent(...);
      ahWaiters[1]= CreateEvent(...);

     ::WaitForMultipleObjects(2, ahWaiters, ...);
or
     ::WaitForMultipleObjects(2, &ahWaiters[0], ...);

Also, It seems to me that setting any of the times in the HANDLE array to 0 will cause the error that you are experiencing.  Why does the one named "shutdown" have a value of 0?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial