Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to send a Message to COleControl with m_hWnd = NULL?

Status
Not open for further replies.

xanblax

Programmer
Mar 19, 2014
1
ES
thread194-107864

In a previously closed thread a Guest user asked a an interesting question I had to face myself:

-----------------
hi all!
I'm developing a multithreaded MFC activex control and want to send messages
to my COleControl object from one of the threads.
My idea was to use WM_USER messages, but SendMessage requires the
destination window's handle and my m_hWnd is NULL...
has anybody a solution for this?
ciao
holger
-----------------

Well, I had this same problem and I found the solution:
The main MFC class from which an OCX derives is COleControl.
COleControl is a subclass of CWnd, so one could think that it has a proper m_hWnd.
But it does not! As Holger sais, m_hWnd is NULL. And if m_hWnd is NULL, SendMessage fails.
But if you add this snippet to your COleControl derived class, it gets a proper m_hWnd and everything is solved!

Code:
// Create a Notification Sink (to be enable message handling)
m_hWnd = NULL;
if (!CreateEx(0, AfxRegisterWndClass(0), _T("My OCX Notification Sink"), WS_OVERLAPPED, 0, 0, 0, 0, NULL, NULL))
{
	AfxThrowResourceException();
}

Next time you read m_hWnd, you will see it has a correct value. And, therefore, SendMessage & PostMessage will work.

I hope this helps!

Ricardo Vázquez.
Madrid, Spain.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top