Click to See Complete Forum and Search --> : Please help me with SendMessage


ROOKIE
April 5th, 1999, 10:19 AM
I am attempting to use SendMessage to control an application from within my own app. I am attempting to use the menu commands by issuing WM_CHAR and WM_SYSCHAR messages. I am using notepad to practice trying to close an app via the menu. Anyone with any suggestions, please respond.

(if you use the code below, make sure that you have a new instance of Notepad running or you will get an error.)

{

CWnd* hwnd;

hwnd = FindWindow(NULL, "Untitled - Notepad");

char* d;
char buf[80];
sprintf(buf, "fx");
d = '\0';
if(buf[0]!='\0'){
d = buf;
while(*d){
hwnd->SendMessage( WM_SYSCHAR,*d,1L);
d++;
hwnd->SendMessage( WM_CHAR,*d,1L);
d++;
}
}
}

April 5th, 1999, 10:47 AM
Since you're trying to control the main menu, why not send a WM_COMMAND message
using MAKEWPARAM(menu_id,0) as the wParam and 0 as the lParam? The menu_id
is the menu identifier for the desired menu action. I'm assuming you're using
Win32. If you're using Win16, the WM_COMMAND message is a little different.

If you don't know the menu id's for Notepad, just load Notepad.exe (more
preferably, load a copy of Notepad.exe) into a resource editor (DevStudio can
load an exe as a resource file). This will reveal all of the menu id's that
are available on the main menu.

My experience is that trying to emulate the actions of the keyboard by sending
WM_KEYDOWN's, WM_KEYUP's, WM_CHAR, etc. ad nauseaum, is a very fragile way of
"controlling" an application since keyboard handling is not as straightforward
as you think it is. I've had trouble controlling my own applications, let
alone another app, by trying to emulate the job of the keyboard via messaging.