Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. WM_SETTEXT and Virtual Address Space [modified]

WM_SETTEXT and Virtual Address Space [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionc++
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sawerr
    wrote on last edited by
    #1

    Hi 1-) I compiled a DLL which is a hook dll. It calls SetWindowHookEx and hook Keyboard. and there is a hookproc.

    //DLL
    void StartHook(HWND hWnd)
    {

    hHook = SetWindowsHookEx(WH\_KEYBOARD, HookProc, hmod, NULL);
    hWndServer = hWnd;
    

    };

    2-) And I compiled a MFC app. which includes this dll. And calls StartHook. Also it sends its handle to dll. So they can communicate each other.

    //EXE
    HWND hWnd = ::GetDlgItem(this->m_hWnd, IDC_EDIT1);
    StartHook(hWnd);

    3-) In MFC app. there is an edit control which I want to show dynamically which keys user pressed. So first i get editcontrols textlength int textlength = SendMessage(hWndServer, WM_GETTEXTLENGTH, 0, 0) + 1 ; and then try to replace text of the edit control with WM_SETTEXT:

    // DLL
    TCHAR *td = new TCHAR(textlength+1);
    SendMessage(hWndServer, WM_GETTEXT, textlength, (LPARAM)td);

    	SendMessage(hWndServer, WM\_SETTEXT, 0, (LPARAM)td);  
    	delete\[\] td;
    

    4-) It doesn't work and run-time error. Here something wrong. In MSDN for WM_Settext's lParam parameter: "Pointer to a null-terminated string that is the window text." It is a pointer to another process address space and with sendmessage it is sended to another process. And it is meaningless for other process. Is this caused run-time error? How can i solve this problem? (I mean using WM_settext to send a string to another process) Thanks..

    modified on Tuesday, September 9, 2008 4:42 PM

    M 2 Replies Last reply
    0
    • S sawerr

      Hi 1-) I compiled a DLL which is a hook dll. It calls SetWindowHookEx and hook Keyboard. and there is a hookproc.

      //DLL
      void StartHook(HWND hWnd)
      {

      hHook = SetWindowsHookEx(WH\_KEYBOARD, HookProc, hmod, NULL);
      hWndServer = hWnd;
      

      };

      2-) And I compiled a MFC app. which includes this dll. And calls StartHook. Also it sends its handle to dll. So they can communicate each other.

      //EXE
      HWND hWnd = ::GetDlgItem(this->m_hWnd, IDC_EDIT1);
      StartHook(hWnd);

      3-) In MFC app. there is an edit control which I want to show dynamically which keys user pressed. So first i get editcontrols textlength int textlength = SendMessage(hWndServer, WM_GETTEXTLENGTH, 0, 0) + 1 ; and then try to replace text of the edit control with WM_SETTEXT:

      // DLL
      TCHAR *td = new TCHAR(textlength+1);
      SendMessage(hWndServer, WM_GETTEXT, textlength, (LPARAM)td);

      	SendMessage(hWndServer, WM\_SETTEXT, 0, (LPARAM)td);  
      	delete\[\] td;
      

      4-) It doesn't work and run-time error. Here something wrong. In MSDN for WM_Settext's lParam parameter: "Pointer to a null-terminated string that is the window text." It is a pointer to another process address space and with sendmessage it is sended to another process. And it is meaningless for other process. Is this caused run-time error? How can i solve this problem? (I mean using WM_settext to send a string to another process) Thanks..

      modified on Tuesday, September 9, 2008 4:42 PM

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      sawerr wrote:

      SendMessage(hWndServer, WM_GETTEXT, textlength, (LPARAM)td);

      That should be SendMessage(hWndServer, WM_GETTEXT, textlength+1, (LPARAM)td);

      sawerr wrote:

      4-) It doesn't work and run-time error.

      What error? You may get better results posting a message to a window on the same thread as the message loop the edit control is on. Let the handler for that message do the WM_GETTEXT/WM_SETTEXT stuff instead of doing it from the hook proc. Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      S 1 Reply Last reply
      0
      • M Mark Salsbery

        sawerr wrote:

        SendMessage(hWndServer, WM_GETTEXT, textlength, (LPARAM)td);

        That should be SendMessage(hWndServer, WM_GETTEXT, textlength+1, (LPARAM)td);

        sawerr wrote:

        4-) It doesn't work and run-time error.

        What error? You may get better results posting a message to a window on the same thread as the message loop the edit control is on. Let the handler for that message do the WM_GETTEXT/WM_SETTEXT stuff instead of doing it from the hook proc. Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        S Offline
        S Offline
        sawerr
        wrote on last edited by
        #3

        OK. Understood. But is there a way to send a text string to another application with SendMessage+WM_SETTEXT? Because of lPararm is just a pointer, this way doesn't allow to send a buffer to another application i think. Or I couldn't find a way...

        M 1 Reply Last reply
        0
        • S sawerr

          OK. Understood. But is there a way to send a text string to another application with SendMessage+WM_SETTEXT? Because of lPararm is just a pointer, this way doesn't allow to send a buffer to another application i think. Or I couldn't find a way...

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          sawerr wrote:

          is there a way to send a text string to another application with SendMessage+WM_SETTEXT?

          No. You could use WM_COPYDATA or some other IPC mechanism[^] however. Did you mention another application and I missed it? I thought this was DLL to EXE... Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          S 1 Reply Last reply
          0
          • S sawerr

            Hi 1-) I compiled a DLL which is a hook dll. It calls SetWindowHookEx and hook Keyboard. and there is a hookproc.

            //DLL
            void StartHook(HWND hWnd)
            {

            hHook = SetWindowsHookEx(WH\_KEYBOARD, HookProc, hmod, NULL);
            hWndServer = hWnd;
            

            };

            2-) And I compiled a MFC app. which includes this dll. And calls StartHook. Also it sends its handle to dll. So they can communicate each other.

            //EXE
            HWND hWnd = ::GetDlgItem(this->m_hWnd, IDC_EDIT1);
            StartHook(hWnd);

            3-) In MFC app. there is an edit control which I want to show dynamically which keys user pressed. So first i get editcontrols textlength int textlength = SendMessage(hWndServer, WM_GETTEXTLENGTH, 0, 0) + 1 ; and then try to replace text of the edit control with WM_SETTEXT:

            // DLL
            TCHAR *td = new TCHAR(textlength+1);
            SendMessage(hWndServer, WM_GETTEXT, textlength, (LPARAM)td);

            	SendMessage(hWndServer, WM\_SETTEXT, 0, (LPARAM)td);  
            	delete\[\] td;
            

            4-) It doesn't work and run-time error. Here something wrong. In MSDN for WM_Settext's lParam parameter: "Pointer to a null-terminated string that is the window text." It is a pointer to another process address space and with sendmessage it is sended to another process. And it is meaningless for other process. Is this caused run-time error? How can i solve this problem? (I mean using WM_settext to send a string to another process) Thanks..

            modified on Tuesday, September 9, 2008 4:42 PM

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            I just noticed this

            sawerr wrote:

            int textlength = SendMessage(hWndServer, WM_GETTEXTLENGTH, 0, 0) + 1 ;

            You already added 1 ... I didn't see that, but this:

            sawerr wrote:

            TCHAR *td = new TCHAR(textlength+1);

            is BAD! That should be

            // Notice the square brackets!
            TCHAR *td = new TCHAR[textlength]; // + 1 for NULL terminator already added above

            The wrong brackets is probably the runrime error problem, since not enough space is allocated for a string longer than 0 TCHARs if you use parenthesis. :)

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            S 1 Reply Last reply
            0
            • M Mark Salsbery

              sawerr wrote:

              is there a way to send a text string to another application with SendMessage+WM_SETTEXT?

              No. You could use WM_COPYDATA or some other IPC mechanism[^] however. Did you mention another application and I missed it? I thought this was DLL to EXE... Mark

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              S Offline
              S Offline
              sawerr
              wrote on last edited by
              #6

              Yes, it is dll to exe. But dll calls SetWindowsHookEx. So it is loaded to all processes address spaces. So it becomes exe to exe, as in the figure: http://www.codeproject.com/KB/DLL/hooks.aspx[^] I thought i could update textbox's text from dll as i tried to explain above.(In dll first get text with Sendmessage-WM_gettext and append new presssed key and send it to the application with SendMessage+WM_SETTEXT)

              M 1 Reply Last reply
              0
              • M Mark Salsbery

                I just noticed this

                sawerr wrote:

                int textlength = SendMessage(hWndServer, WM_GETTEXTLENGTH, 0, 0) + 1 ;

                You already added 1 ... I didn't see that, but this:

                sawerr wrote:

                TCHAR *td = new TCHAR(textlength+1);

                is BAD! That should be

                // Notice the square brackets!
                TCHAR *td = new TCHAR[textlength]; // + 1 for NULL terminator already added above

                The wrong brackets is probably the runrime error problem, since not enough space is allocated for a string longer than 0 TCHARs if you use parenthesis. :)

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                S Offline
                S Offline
                sawerr
                wrote on last edited by
                #7

                Shame on me. OK. I see it. But although these stupid errors, i think wm_settext+sendmessage could not send text as i tried to explain my previous post.

                1 Reply Last reply
                0
                • S sawerr

                  Yes, it is dll to exe. But dll calls SetWindowsHookEx. So it is loaded to all processes address spaces. So it becomes exe to exe, as in the figure: http://www.codeproject.com/KB/DLL/hooks.aspx[^] I thought i could update textbox's text from dll as i tried to explain above.(In dll first get text with Sendmessage-WM_gettext and append new presssed key and send it to the application with SendMessage+WM_SETTEXT)

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #8

                  sawerr wrote:

                  So it becomes exe to exe, as in the figure:

                  Then I guess other options are to one of the IPC methods described at the previous link or do something similar to what Joe does in his article but post the WPARAM and LPARAM passed to the KeyboardProc to a window in the exe process and let the EXE do the string stuff.

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups