Thread: User defined Window Messages

  1. #1
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120

    Question User defined Window Messages

    Another problem sent to destroy me! ;0

    I'm have created a hook to hook a thread from another app. This all works fine. It monitors a RICHEDIT window and sends a message to my own app every time the EM_REPLACESEL message is sent to the Richedit control. This would be fine except that the string pointer passed with EM_REPLACESEL seems to be invalid when I send it to my app.

    In my DLL which implements my hook I have the following:

    Code:
    LRESULT CALLBACK HookProc(int iCode, WPARAM wParam, LPARAM lParam)
    {
    	if (iCode < 0)
    		return CallNextHookEx(g_hook, iCode, wParam, lParam);
    
    	CWPSTRUCT *msg = (CWPSTRUCT *)lParam;
    	if(msg->hwnd == g_watchWnd)
    	{
    		if(msg->message == EM_REPLACESEL)
    			SendMessage(g_callingWnd, WM_NEWACTION, 0, msg->lParam);
    
    		if(msg->message == WM_DESTROY)
    			RemoveTableHook();		
    	}
    
    	return 0;
    }
    g_watchWnd is the RICHEDIT window and g_callingWnd is the main window of my app. In my app I have the following message handling procedure:

    Code:
    LRESULT CDLLTestAppDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
    {	
    	if(message == WM_NEWACTION)
    	{
    		m_Edit.SetWindowText((LPCTSTR)lParam);
    		return TRUE;
    	}
    
    	return CDialog::WindowProc(message, wParam, lParam);
    }
    m_Edit is an edit control in my app which I'm using for test purposes only - it wont be used in the actual implementation.

    The WM_NEWACTION messageID was declared using RegisterWindowMessage() in both the app and the DLL and this seems to work fine. the message gets sent and is picked up. However, the string pointer in lParam seems no longer to work, as if the address does not point to a string any longer.

    If my hook is set as thread specific (using the thread that the RICHEDIT window belongs to), the message handling code causes the app to crash. If the hook is declared system wide, it doesn't crash but no text is passed over, I just get "".

    The thing that really confuses me is that if I do the following there is no problem:

    Code:
    	CWPSTRUCT *msg = (CWPSTRUCT *)lParam;
    	if(msg->hwnd == g_watchWnd)
    	{
    		if(msg->message == EM_REPLACESEL)
    			SendMessage(g_callingWnd, EM_REPLACESEL, 0, msg->lParam);
    ...
    
    	if(message == EM_REPLACESEL)
    	{
    		m_Edit.SetWindowText((LPCTSTR)lParam);
    		return TRUE;
    	}
    What is the EM_REPLACESEL messageID doing that my own messageID isnt??!

    I know I can use this code - but I don't want to send the EM_REPLACESEL message when that's not want I'm going to be doing with the text - and I want to solve this problem!!

    Any help would be very much appreciated

    dt
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

  2. #2
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    EM_REPLACESEL replaces text oof the currently selected text. You don't seem to be sending text as your LPARAM in the SendMessage() call. You're sending the message's LPARAM value instead, which is a number not a char or char*. You also have not listed what WM_NEWACTION is valued at. Please let me know what you have set WM_NEWACTION to be valued at befor the call to RegisterWidowsMessage().
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Déjà vu.

    You can not arbitarily use memory pointers between processes. You need to use some sort of inter process communication (IPC).
    The easiest in this scenario is WM_SETTEXT or WM_COPYDATA.
    Please see the discussion on these a few messages back.

    http://cboard.cprogramming.com/showt...threadid=44341

  4. #4
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    Cheers anonytmouse, I saw that discussion after I posted - didnt see that WM_COPYDATA was a solution - doh.

    Clears things up thanks.
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 06:57 PM
  2. Linking problems in Visual Studio
    By h3ro in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2008, 02:39 PM
  3. creating a child window
    By rakan in forum Windows Programming
    Replies: 2
    Last Post: 01-23-2007, 03:22 PM
  4. Window scrollbar
    By maxorator in forum Windows Programming
    Replies: 2
    Last Post: 10-07-2005, 12:31 PM
  5. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM