tags:

views:

203

answers:

1

Why does Windows SendMessage() always return ZERO, even the message delivery is success? Is there anyway to check the message delivery failure with SendMessage() ?

EDIT

Forgot to mention that I'm using SendMessage() inside a c++ DLL

LRESULT result = ::SendMessage(hwndOtherWindow,WM_COPYDATA, NULL/*(WPARAM)this->GetSafeHwnd()*/,(LPARAM)&structCDS);

"result" is always zero :(, but message delivers to other window successfully

EDIT

BOOL CDlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
{
    return /*CDialog::OnCopyData(pWnd, pCopyDataStruct)*/ true;  //true is the trick
 }
+2  A: 

A zero return from SendMessage for WM_COPYDATA means the target application didn't process the message (FALSE = 0).

The message might deliver successfully, but if the target application doesn't handle the message properly (ie, wrong return value, or passing it to the default window procedure) then your SendMessage call will appear to come back with the wrong result.

It might be worth your time to see what the target application's handling of the WM_COPYDATA message is, if possible.

Matthew Iselin
In the receiving window, I simply implement OnCopyData method and just return. If I'm correct, Receiving window can either process or just IGNORE and return.But I'm having a clue on this return result since my DLL does not have a window, I just pass NULL to wParam. However, the SendMessage is a blocking call, still it should return correct result. Am I correct ?
nimo
Yes, SendMessage blocks and returns the value returned from the window procedure function when the message is handled. If you're using OnCopyData (MFC?), you'll need to return TRUE rather than zero.If you could edit the original post to also show your implementation of WM_COPYDATA on the application side, that would help a lot.
Matthew Iselin
Thanks mattew, As you suggest "return true" is the trick, prevoiusly I just called "CDialog::OnCopyData". By the way, I would like know what is wrong with previous implementation.
nimo
Calling CDialog::OnCopyData effectively calls the default handler, which most likely returns FALSE (ie, not handled by the application - "default behaviour"). If you explicitly return TRUE you are not calling a default handler, but actually handling the message. You probably shouldn't have just "return true;" as your implementation, however: you should probably handle the message properly and perform an action based on the data.
Matthew Iselin