The communication between Delphi two applications (processes)

Source: Internet
Author: User

Communication between two applications is actually a communication between two processes. Because of my limited knowledge, I decided to apply the message to achieve. What you need to know:

1.RegisterWindowMessage ()//parameter type: Pchar; return value: Longint;

2.FindWindow (
Lpclassname, {Window's class name}
Lpwindowname:pchar {Window's caption}
): HWND; {Returns a handle to the window; return 0}

3.Wndproc ()//Each window will have a callback function called a window procedure (Wndproc) with four parameters, namely: Window handle (Windows Handle), message ID and two message parameters (WParam, LParam)

4.PostMessage ()//This function puts a message in the message queue to the thread created by the specified window, does not wait for the thread to process the message and returns, is the asynchronous message pattern. Messages in Message Queuing are made by calling GetMessage and PeekMessage. After the acquisition, the WndProc is processed.

Well, the knowledge needed is here, now start communicating between our applications.

First in the two application of the main form of the creation process registration message, the message number must not be less than wm_user, and then in Program 1 to get the main form handle of program 2, and send a message through postmessage; next, in Program 2, the main form of the process registration and program 1 the same number of messages, Then reload the WndProc procedure for program 2. The nonsense is not much to say, direct sticker code:

Program 1//////////////////////////////////////// //////////////////////////////////

Unit Unit1;

Interface

Uses
Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
Dialogs, Stdctrls, Buttons;

Type
TForm1 = Class (Tform)
Button2:tbutton;
Button3:tbutton;
BITBTN1:TBITBTN;
Procedure Button2click (Sender:tobject);
Procedure Formshow (Sender:tobject);
Procedure Bitbtn1click (Sender:tobject);
Private
{Private declarations}

I:integer;

Public
{Public declarations}

End

Var
Form1:tform1;

Implementation


{$R *.DFM}

{TForm1}
Procedure Tform1.formshow (Sender:tobject);
Begin
i:= registerwindowmessage (' mymessage ');
End
Procedure Tform1.bitbtn1click (Sender:tobject);
Var
H1:hwnd;
Begin
h1:= FindWindow (Nil, ' Form2 ');
PostMessage (h1,i,0,1);

End

Program 2//////////////////////////////////////// /////////////////////////////

Unit Unit1;

Interface

Uses
Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
Dialogs, Stdctrls, Appevnts, Buttons,qextctrls, Extctrls;
Const
MY_MOUSL = wm_user+100;
Type
TForm2 = Class (Tform)
Button1:tbutton;
applicationevents1:tapplicationevents;
BITBTN1:TBITBTN;
BTNCREATEPANEL:TBITBTN;
BITBTN2:TBITBTN;
BITBTN3:TBITBTN;
Panel1:tpanel;
Button2:tbutton;
Procedure Formshow (Sender:tobject);
Private
{Private declarations}
J:integer;
Public
{Public declarations}
Procedure WndProc (var message:tmessage); Override
End

Var
Form2:tform2;

Implementation

{$R *.DFM}

Procedure Tform1.formshow (Sender:tobject);
Begin
j:= registerwindowmessage (' mymessage ');
End

Procedure Tform1.wndproc (var message:tmessage);
Begin
If message.msg = J Then
ShowMessage (' Get message ')
else inherited;

End

At this point, the communication between applications is complete, it is important to note: FindWindow must find the application you want to get the message, that is, if you use FindWindow (nil, ' Form2 '), you must ensure that the form caption:= The FORM2 program is unique.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.