I have a COM component, implemented in C++ with ATL, that uses overlapped socket I/O. Right after the connection is made to the server, it starts an overlapped read on the socket, with code like this:
// Pass pointer to this instance as hEvent parameter, for use by callback
m_recvOverlapped.hEvent = reinterpret_cast<HANDLE>(this);
int rc = ::WSARecv(m_s, &wsabuf, 1, &m_recvNumberOfBytes, &m_recvFlags, &m_recvOverlapped, RecvCallback);
if (rc == SOCKET_ERROR)
{
// If error is WSA_IO_PENDING, then the I/O is still in progress. Otherwise, something bad happened.
int error = ::WSAGetLastError();
if (error != WSA_IO_PENDING)
{
ReceiveError(error);
}
}
And I have a callback function that looks something like this:
void CALLBACK CMySocket::RecvCallback(DWORD dwError, DWORD cbTransferred, LPWSAOVERLAPPED lpOverlapped, DWORD dwFlags)
{
CMySocket* socket = reinterpret_cast<CMySocket*>(lpOverlapped->hEvent);
ATLASSERT(socket != 0);
if (!socket)
return;
socket->ReceiveCompleted(dwError, cbTransferred, lpOverlapped, dwFlags);
}
This COM component works fine in unit tests, when used in a command-line app, and when used in a .NET GUI app (via COM interop). However, when I use this component in an MFC app, the RecvCallback
never gets called when the server sends data to it.
WSARecv()
returns SOCKET_ERROR
, and WSAGetLastError()
returns WSA_IO_PENDING
, as expected for asynchronous overlapped reads.
When I use the SysInternals TcpView app to watch what's happening, it indicates that the client is receiving data. But the callback is never being called.
Sending data to the server through the connected socket works fine.
I am calling CoInitializeEx()
and WSAStartup()
in my MFC app's InitInstance()
method.
Any ideas?