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.

5
  • 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. Jun 18, 2014 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. Jun 18, 2014 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. Jun 18, 2014 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? Jun 18, 2014 at 23:10
  • Trying to understand MFC without knowing the Windows API is futile. You should probably read Prerequisites for learning MFC programming. Jun 20, 2014 at 0:47

1 Answer 1

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.

6
  • I checked, indeed that is what is happening. Jun 18, 2014 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. Jun 18, 2014 at 23:50
  • 1
    Call its CreateWindow. Jun 19, 2014 at 1: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); Jun 19, 2014 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. Jun 19, 2014 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.