Inter-process response technology of C ++

zhaozj2021-02-11  278

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 # prgma hdrstop # incrude "Unit1.h" # Pragma Package smart_init) # pragma resource "* .dfm" TForm1 * Form1; __fastcall TForm1 :: TForm1 (TComponent * Owner): TForm (Owner) {} void __fastcall TForm1 :: btnSearchClick (TObject * Sender) {title search text and txtTitle In line with the window, put the search window handle into the h_wnd and put the handle of all its sub-windings in the array of HWndArray, CHAR Search [128]; CHAR CLASSNAME [128]; hwnd h_child; int curpos; int CMPlen Currpos = 0; LSTClassName-> Clear (); LstcontrolText-> Clear (); memoreadwrite-> clear (); h_wnd = getWindow (form1-> handle, gw_hwndfirst); traversing all windows While (h_wnd) {getWindowText (h_wnd, Search, 128); cmplen = txttitle-> text.length (); search [cmplen] = 0; if (ANSISTRING (Search) == txttitle-> text) {Looking for window titles and we enter the content in txttitle Window LstclassName-> Items-> add (saf); LstcontrolText-> Items-> add (saf); spush (h_wnd); h_child = getWindow (h_wnd, gw_child); find out all sub-windows, Handle Put the array hwndarray While (1) {hwndarray [currpos] = h_child; getClassName (h_child, classname, 128); sendMessage (h_child, wm_gettext, 128, (lparam) Search; put the class name and sub-window of the found sub-window The serial number is placed in the list box LSTClassName, through these data, we can determine what message can be sent to these sub-windows LstclassName-> items-> add (ANSISTRING (CURRPOS) " ANSISTRING (ClassName)); The text information of the sub-window is placed in the list box LSTControlText. Through this information, we can locate the position of the child window in the main window LstcontrolText-> Items-> Add (ansistring (currpos)

"-" 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 # include # include # include # include < Graphics.hpp> class TForm1: public TForm {__published: // IDE- managed Components TListBox * lstClassName; TListBox * lstControlText; TMemo * memoReadWrite; TEdit * txtTitle; TEdit * txtIndex; TEdit * txtToValue; TButton * btnSearc h; TButton * btnMain; TButton * btnChild; TButton * btnGet; TButton * btnChange; TButton * btnExit; TLabel * Label1; TLabel * Label2; TLabel * Label3; TButton * btnCtlBTN; void__fastcall btnSearchClick (TObject * Sender); void__fastcall btnMainClick (TObject * Sender; void__fastcall btnchildclick (TOBJECT * Sender); void__fastcall btngetClick (TOBJECT * Sender); void__fastcall btnexitclick (TOBJECT * Sender);

转载请注明原文地址:https://www.9cbs.com/read-4549.html

New Post(0)