Inter-process response technology of C
Wu Xiaoling
In a Windows environment, an application responds to another application in a specified manner, which is usually utilizing DDE, which requires a DDE service in response. However, if there is no such service, do we have any other way? Of course, it is to send a message to the application. After the application receives a standard Windows message, the corresponding operation is completed. To send a message to the application, first get its window handle, which can be implemented by two API functions getWindow and getWindowText. First use the getWindow function to traverse all Windows windows, then use the getWindowText function to get the title of each window. If the resulting title is the same as the title we want to find, you get the window handle we want. The window of the application is a tree structure, and there may be a sub-window under each sub-window. To get the handle of all sub-windows, it is necessary to traverse the whole tree. In order to achieve this, we use a simple stack in the program. operating. With the specific window handle, you can implement the operations we want to achieve by sending messages. This article gave a few simple examples that realize some commonly used operations, such as modifying the text in the text box, implementing the buttons in another application from a program, etc..
Figure 1 is the main form screen of the program run: The following is the source program part of the C Builder: unit file unit1.cpp # incrude
"-" AnsiString (Search));!! CurrPos ; sPush (h_Child); h_Child = GetWindow (h_Child, GW_HWNDNEXT); if (h_Child) {while (h_Child && stack_Top> 0) {h_Child = sPop (); h_Child = GetWindow (h_child, gw_child);} if (stack_top == 0) Break;}} Break;} else h_wnd = getWindow (h_wnd, gw_hwndnext);}} The following is a few simple operation examples, send a message to the specified sub-window , Complete the specified operation to modify the title Void __fastcall tform1 :: btnmainclick (Tobject * sender {setWindowText (h_wnd, txttthannel {setWindowText (h_wnd, txttthannel {setwindowtext (h_wnd, txttthannel {setWindowText); form index number, replacing the sub-window with the contents of the text in the text box txtToValue void __fastcall TForm1 :: btnChildClick (TObject * Sender) {int ctlIndex; ctlIndex = txtIndex-> Text.ToInt (); SetWindowText (hWndArray [ctlIndex] , txtthannel ());} Get the text in the sub-window of the text box, put the text to the text in Memoreadwrite void __fastcall tform1 :: btNgetClick (Tobject * sender) {int CTLINDEX; char thecopy Memoreadwrite-> Clear (); ctlindex = txtindex-> text.toint (); sendMessage (Hwndarray [CTLINDEX], WM_GETTEXT, 256, (LPARAM) THECOPY); MEMOREA DWRITE-> LINES-> add (ansistring (thecopy));} void __fastcall tform1 :: btnexitclick (TOBJECT * Sender) {Application-> Terminate ();} With the specified text replace the text in the sub-window of the text box void __fastcall TForm1 :: btnChangeClick (TObject * Sender) {int ctlIndex; ctlIndex = txtIndex-> Text.ToInt (); SendMessage (hWndArray [ctlIndex], EM_SETSEL, 0,1000); SendMessage (hWndArray [ctlIndex], EM_REPLACESEL, ( WPARAM) TRUE, (LPARAM) (TXTTOVALUE-> text.c_str ()));
} Send BM_Click to the sub-window of the manipulated application to the button to implement the button Click Action Void __fastcall tform1 :: btnctlbtnclick (Tobject * sender) {int CTLINDEX; CTLINDEX = txtIndex-> text.toint (); sendMessage (HWndArray [CTLINDEX], BM_CLICK, 0, 0); / * actually, if you are not familiar with the message structure of Windows, simple mouse and keyboard messages can also achieve most of the operations. For example, send a mouse to press the message, and then send a mouse to the message, you can simulate a click message.
* / SendMessage (hwndarray [CTLINDEX], WM_LBUTTONDOWN, 0); SendMessage (HWndArray [CTLINDEX], WM_LBUTTONUP, 0, 0);} header file unit1.h stack hwnd Astack [100]; stack pointer int stack_top; child window array of handles and a main window handle HWND hWndArray [100], h_Wnd; stack void sPush (HWND hWndValue) {if (stack_Top <100) {aStack [stack_Top] = hWndValue; stack_Top ;} else {MessageBox (NULL, "Stack Overflow" , "Stack error", 0);}} pops up hWnd spop () {if (stack_top> 0) {stack_top ---; returnium [stack_top];} else {MessageBox (null, "stack empty", "stack error" , 0);} #} # deflude