开发者

How to avoid repeated SendMessage

I have a problem: I use SendMessage from a procedure in a DLL to communicate with the main window; procedure is a hook procedure that allows main window to know when mouse right button is clicked in a editbox; it sends also handle of the editbox. It works well, except for this bug: when program is running without breakpoints main window receives twice the same message (in this case WM_APP), while if I put a breakpoint in the hook procedure or in the block that handles WM_APP messages the message is considered once. For further descriptions ask me. Following the code of the hook procedure and of the block that handles WM_APP messages. Thanks

Hook procedure

MYDLL_API LRESULT CALLBACK mouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
// processes the message
if(nCode >= 0)
{
    // if user clicked with mouse right button
    if(wParam != NULL && (wParam == WM_RBUTTONDOWN || wParam == WM_RBUTTONUP))
    {
        wchar_t *s = (wchar_t*) malloc(CLASSNAMELEN*sizeof(wchar_t));
        //MessageBox(mainHwnd, (LPCWSTR)L"Captured mouse right button", (LPCWSTR)L"Test", MB_OK);
        MOUSEHOOKSTRUCT *m = (MOUSEHOOKSTRUCT*) lParam;
        GetClassName(m->hwnd, (LPWSTR) s, CLASSNAMELEN);
        //MessageBox(mainHwnd, (LPCWSTR) s, (LPCWSTR)L"Test", MB_OK);
      开发者_StackOverflow社区  // only if user clicked on a edit box
        if(wcsncmp(s, L"Edit", 4) == 0)
            SendMessage(mainHwnd, WM_APP, 0, (LPARAM) lParam);
        free(s);
        s = NULL;
    }
}

// calls next hook in chain
return CallNextHookEx(NULL, nCode, wParam, lParam);
}

block in main program that handles WM_APP messages

case WM_APP:
    {
        //MessageBox(hWnd, (LPCWSTR)L"Received WM_APP", (LPCWSTR)L"Test", MB_OK);
        // copies text from the edit box
        MOUSEHOOKSTRUCT *m = (MOUSEHOOKSTRUCT*) lParam;
        int n = GetWindowTextLength(m->hwnd);
        // if text has been inserted
        if(n > 0 && n < 1024)
        {
            wchar_t *s = (wchar_t*) malloc((n+1)*sizeof(wchar_t));
            // gets text
            GetWindowText(m->hwnd, (LPWSTR) s, n+1);
            s[n] = (wchar_t) 0;
            //MessageBox(hWnd, (LPCWSTR)s, (LPCWSTR)L"Test", MB_OK);
            // saves text in database
            stateClassPointer->insertInList(s); 
        }
    }
    break;


It is probably because you are sending the message for WM_RBUTTONDOWN and WM_RBUTTONUP, that is when the right button is pressed and when it is released.

When you are debugging the WM_RBUTTONUP is eaten by the debugger so you don't get it.

PS: Shouldn't you use PostMessage() instead of SendMessage(), just to be safe?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜