cls
cls

Reputation: 561

WM_CHAR doesn't work with notepad.exe

I use Windows 7 and Microsoft Visual Studio 2010. I use this code to insert digit chars to the window of calc.exe:

STARTUPINFO         si  = { 0 };
PROCESS_INFORMATION pi  = { 0 };

memset(&si, 0, sizeof(si));
memset(&pi, 0, sizeof(pi));

si.cb = sizeof(si);

BOOL bResult = CreateProcess("c:\\windows\\syswow64\\calc.exe", NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
WaitForInputIdle(pi.hProcess, INFINITE);
HWND hWnd = FindWindow("CalcFrame", NULL);

PostMessage(hWnd, WM_CHAR, (WPARAM)'1', 0);
PostMessage(hWnd, WM_CHAR, (WPARAM)'2', 0);
PostMessage(hWnd, WM_CHAR, (WPARAM)'3', 0);
PostMessage(hWnd, WM_CHAR, (WPARAM)'4', 0);

This code works perfect. And when I replace "c:\\windows\\syswow64\\calc.exe" with "c:\\windows\\syswow64\\notepad.exe" and "CalcFrame" with "Notepad" it doesn't insert chars into the Notepad window.

Upvotes: 0

Views: 420

Answers (1)

HerrJoebob
HerrJoebob

Reputation: 2313

There is a child window of class EDIT inside the client area of Notepad's main window. For what you are doing, you need to locate and send messages to that window rather than Notepad's main window.

BOOL bResult = CreateProcess("c:\\windows\\syswow64\\notepad.exe", NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
WaitForInputIdle(pi.hProcess, INFINITE);
HWND hWnd = FindWindow("Notepad", NULL);
hWnd = FindWindowEx(hWnd, NULL, "EDIT", NULL); // <-- add this

PostMessage(hWnd, WM_CHAR, (WPARAM)'1', 0);
PostMessage(hWnd, WM_CHAR, (WPARAM)'2', 0);
PostMessage(hWnd, WM_CHAR, (WPARAM)'3', 0);
PostMessage(hWnd, WM_CHAR, (WPARAM)'4', 0);

Use Spy++ to explore window hierarchy and find these things.

Upvotes: 2

Related Questions