1

I have a custom class derived from CWnd that I would like to post a message to from a worker thread. I am using the PostMessage function to achieve this. The first argument to PostMessage is the HWND type handle to my class, and the next is the message I would like handled. For the first parameter, I generate the handle to my class using GetSafeHwnd() function, and for the second parameter, I use WM_USER+3. Also, I declare a message map inside my class header file, and add an entry for the message handler inside the BEGIN_MESSAGE_MAP and END_MESSAGE_MAP block. However, my handler is not getting called. I have also checked the return value of PostMessage function, it is 1, that means success.

Here is my code :

Inside MyClass.h

class CMyClass : CWnd
{
....
.... 
public:
void InitHandle();

protected:
afx_msg LRESULT OnMessageReceived(WPARAM wParam, LPARAM lParam);
DECLARE_MESSAGE_MAP()
} 

Inside MyClass.cpp

#define WM_MY_MESSAGE WM_USER+3

HWND handleToMyClassWindow;

BEGIN_MESSAGE_MAP(CMyClass, CWnd)
    ON_MESSAGE(WM_MY_MESSAGE, OnMessageReceived)
END_MESSAGE_MAP()

LRESULT CMyClass::OnMessageReceived(WPARAM wParam, LPARAM lParam)
{ .... }

void CMyClass::InitHandle()
{ 
    handleToMyClassWindow = GetSafeHwnd();
}

Inside Worker thread

UINT WorkerThreadFunction(LPVOID pParam )
{ 
....
PostMessage(handleToMyClassWindow, WM_MY_MESSAGE, NULL, NULL);
....
}

My question is, what are the possible reasons for the OnMessageReceived handler to not be called.

P.S.

I take care that the calling object calls the InitHandle() function.

I tried the same technique with the View class (derived from CView) of my program, and it works there, but fails here.

user2654449
  • 119
  • 7
  • 1
    Don't use `WM_USER`, use `WM_APP`. The `WM_USER` range is also used by some of the standard controls - you may have registered the same message twice. – Mark Ransom Jun 18 '14 at 22:37
  • @MarkRansom, I tried WM_APP, it still did not work. Also, my code works if I am adding the handler to the View class of my program, but it does not work if adding to my custom class. – user2654449 Jun 18 '14 at 22:57
  • Here's another interesting thing : From the class Explorer, I right clicked on CMyClass, and opened up the properties box. I do not see my message in the list of message for CMyClass. – user2654449 Jun 18 '14 at 23:08
  • Also, does my class need to have a message loop? I tried the same kind of code inside the View class of my program, and it worked, the message got posted. My View class does not have a message loop (at least not to my knowledge). But then I wonder, how does a message gets processed without a message loop? – user2654449 Jun 18 '14 at 23:10
  • Trying to understand MFC without knowing the Windows API is futile. You should probably read [Prerequisites for learning MFC programming](http://stackoverflow.com/a/18191454/1889329). – IInspectable Jun 20 '14 at 00:47

1 Answers1

1

You cannot post to a window if it has not been created. GetSafeHwnd() will return NULL if you have not actually created a window using your class.

ScottMcP-MVP
  • 10,337
  • 2
  • 15
  • 15
  • I checked, indeed that is what is happening. – user2654449 Jun 18 '14 at 23:44
  • Could you please give me hints on how to create a window for CMyClass? I have no need for GUI with respect to CMyClass, so I would like the window to remain invisible. – user2654449 Jun 18 '14 at 23:50
  • 1
    Call its CreateWindow. – ScottMcP-MVP Jun 19 '14 at 01:07
  • The Class does not have a CreateWindow function. I do see a Create function in CWnd. I tried the following, and it did not work. I get run time exception at the line. Create(L"CMyClass", L"dkjfhsfd", WS_CHILD | WS_VISIBLE, CRect(0, 0, 20, 20), this); – user2654449 Jun 19 '14 at 16:03
  • 1
    The first parameter is a window class name, not related to a C++ class name. You can use a built in window class name such as "STATIC" as shown in the MSDN page for CWnd::Create. And you are passing 'this' as its parent window. That can only work if 'this' is a window that already has a valid HWND. – ScottMcP-MVP Jun 19 '14 at 16:29
  • If you don't really need a window, and are coding a message pump anyway - shouldn't you be using PostThreadMessage? http://msdn.microsoft.com/en-us/library/windows/desktop/ms644946(v=vs.85).aspx – Ofek Shilon Jun 23 '14 at 18:51