calling a running process

Hi,
First I want to say I am new to C++ so please bare with me. I am trying to access a running application from within my current C++ application. I have been able to use the ShellExecute command to start an application using the following

(32 >= (int)ShellExecute(NULL,"open","notepad", NULL, NULL, SW_SHOWNORMAL));

What I want to do is run the copy of notepad (or what ever my app is) this is currently running. I can get the HWND to the running app by using

CWnd* mywin = FindWindow(NULL, TEXT("Untitled - Notepad"));
But how do i call this process and more importantly send parameters to it ?
thanks in advance
Art
If you have the window handle, you can use SendMessage
thanks,
how exactly do i do that ?

call SendKeysToNotepad() from your main() and it should start notepad and write Hello in the window.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
int  g_ChildNum;
HWND g_ChildWnd;

BOOL CALLBACK EnumChildWindowsCB( HWND hWnd, LPARAM lParam )
{
  // every application is different, after some experimenting notepad's first child window
  // is the edit control you type into.
  if( !g_ChildNum )
    g_ChildWnd = hWnd;

  g_ChildNum++;
  return TRUE;
}


void SendKeysToNotepad()
{
  HINSTANCE hInst = ShellExecute( NULL, _T("open"), _T("notepad"), NULL, NULL, SW_SHOWNORMAL );

  if( (int)hInst <= 32 )
    return;

  // wait some time to make sure window is completly created.
  Sleep( 1000 );

  HWND hWnd = FindWindow( NULL, _T("Untitled - Notepad") );

  if( !hWnd )
    return;

  // The top window is not the edit control you type into.
  // Need to find, this is a hack and different for every application.
  EnumChildWindows( hWnd, EnumChildWindowsCB, 0 );

  SendMessage( g_ChildWnd, WM_CHAR, 'H', 0 );
  SendMessage( g_ChildWnd, WM_CHAR, 'e', 0 );
  SendMessage( g_ChildWnd, WM_CHAR, 'l', 0 );
  SendMessage( g_ChildWnd, WM_CHAR, 'l', 0 );
  SendMessage( g_ChildWnd, WM_CHAR, 'o', 0 );
}
thanks
but when i try this with notepad already open it does not work? I need to send "keys" or commands to an executable that is already running. I am able to send the set the window text by using WM_SETTEXT , but can't seem to get the notepad window to move to the foreground or pass any data to it
Topic archived. No new replies allowed.