SendMessage not working with WM_USER message

I am trying to test sending a message from a thread to a window (controlled by another thread) by using SendMessage().

Here are the general steps of what I did:

1) Create a project with the MFC wizard (mostly default settings)
2) in CMainFrame::OnCreate() I create an object which is used to store a handle to the Main Frame object. (This is my 'Notifier' object). I then spawn a thread with that object:

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) {
//...
Notifier ^n = gcnew Notifier(this);

Thread ^nt = gcnew Thread(gcnew ThreadStart(n, &Notifier::runme));
nt->Start();
}

4) In Notifier::runme() I put the following code to test sending a message:

(Note: cwnd is equal to the main frame object passed in the Notifier constructor)

hw = cwnd->GetSafeHwnd();
SendMessage(hw, WM_USER, 0, 0);
SendMessage(hw, WM_SETTEXT, 0, (LPARAM) "NEW TITLE");

5) I set up handlers for those messages in my Main Frame class:

(MainFrm.cpp)

ON_COMMAND(WM_SETTEXT, &CMainFrame::processWMUser)
ON_COMMAND(WM_USER, &CMainFrame::processWMUser)

6) Here is the handler code:

void CMainFrame::processWMUser() {
AfxMessageBox("why dont I see this??");
}

When I run my code I see the window title change to 'NEW TITLE', but neither of the messages kicks off the message box. I tried trapping the message in the App, Doc, and View, but that doesn't work either.

If I change one of the message mappings in the main frame to the following, my handler is called correctly when I try manually do file-open in the menu.

ON_COMMAND(ID_FILE_OPEN,&CMainFrame::processWMUser)

So it seems that both my message sending and receiving and working separately, but I can't get them to work together.

Can someone please help?
[1965 byte] By [locksleyu] at [2007-11-20 11:57:17]
# 1 Re: SendMessage not working with WM_USER message
First of all, once you have written application it would have been easier for you to post it to illustrate your problem.
Instead you have given some kind of recipe to follow; this does not guarantee that code created would be the same as yours.
Having said so, I will try to help since I have spotted you problem without a need to recreate your app.
Take a look at macros you are using to map messages.

ON_COMMAND macro is used to map WM_COMMAND messages only. Check MSDN and MFC code to see how ON_COMMAND macro expands. Also look at description of WM_COMMAND message parameters.
ON_COMMAND macro takes command ID as first parameter and a member function name.
In your case you have used window message and passed it as command ID. You app never receives command with ID equal to WM_SETTEXT or WM_USER.

WM_SETTEXT and WM_USER are defined as Windows messages as WM_COMMAND is.
Lookup SendMessage and PostMessage in <SDN to see what parameters are passed in a parameter list.
To handle messages other than WM_COMMAND wizard provides set of different macros, most of them in a form of ON_WM_xxx . This set of macros is limited to a number of Windows messages.

If the message does not have corresponding macro in wizards database, MFC provides generic way of mapping messages to a member function: ON_MESSAGE.
This is a macro you should have used to map WM_SETTEXT and WM_USER.
The following is an example of the macro, function definition and declaration:
ON_MESSAGE(WM_SETTEXT, OnSetText)
LRESULT CMDIForTestsView::OnSetText(WPARAM wParam, LPARAM lParam)
{
//handle message here
return 0;
}

afx_msg LRESULT OnSetText(WPARAM wParam, LPARAM lParam); You are responsible for interpretation of wParam and lParam.

Besides all try to use WM_APP rather than WM_USER to define user messages; MFC uses WM_USER to define own private messages. WM_APP is better to avoid conflicts.

Yet another thing: read more about MFC message mapping and MFC command message routing. You can find this information in MSDN.
JohnCz at 2007-11-11 4:02:04 >
# 2 Re: SendMessage not working with WM_USER message
Thanks JohnCz, you were 100% right. As soon as I changed to ON_MESSAGE and adjusted my function as you said it worked!

Jeff
locksleyu at 2007-11-11 4:03:06 >
# 3 Re: SendMessage not working with WM_USER message
You are very welcome. Glad to be of help. :)
JohnCz at 2007-11-11 4:04:05 >