WM_SETTEXT and Virtual Address Space [modified]
-
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
-
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
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:
-
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:
-
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...
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:
-
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
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 aboveThe 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:
-
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:
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)
-
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 aboveThe 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:
-
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)
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: