Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3281884
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:51:56+00:00 2026-05-17T19:51:56+00:00

I have a COM component, implemented in C++ with ATL, that uses overlapped socket

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:51:57+00:00Added an answer on May 17, 2026 at 7:51 pm

    OK, I found the answer by searching for other Stack Overflow questions regarding WSARecv.

    From Len Holgate’s answer to Win32 Overlapped I/O – Completion routines or WaitForMultipleObjects?:

    . . . you can pass a completion routine which is called when completion occurs. This is known as ‘alertable I/O’ and requires that the thread that issued the WSARecv() call is in an ‘alertable’ state for the completion routine to be called. Threads can put themselves in an alertable state in several ways (calling SleepEx() or the various EX versions of the Wait functions, etc). . . .

    It turns out that if I start a timer in the MFC app that periodically calls SleepEx(0, TRUE), then the receive callback gets called. So the problem is that the main MFC thread that calls my COM object’s method that calls WSARecv() never goes into an alertable state on its own.

    So, I’ll probably need to change my COM object’s implementation so that it uses I/O completion ports rather than callbacks, or starts its own thread that calls WSARecv() and keeps itself alertable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a COM object implemented with C++/ATL that includes a method which will
I really need help... I have implemented a COM component (i.e A.dll) with IDL,
I have a .NET 2.0 project that needs to use a COM component (specifically
https://i.stack.imgur.com/2KxdM.png I have this segmented control that I made, however I don't know how
I have a VB6 COM component which I need to call from my .Net
I have an old C++ COM component which has to stay in Visual Studio
I have a .NET component exposed as a CCW (Com Callable Wrapper) and being
I have a COM object written using the MS ATL library. I have declared
I have an OLE COM object that trying to write a wrapper for, I
I have some COM component which I call from some c# dll. I also

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.