Mega Code Archive

 
Categories / Delphi / Examples
 

How to communicate between applications

Title: How to communicate between applications // 1. Define type of your message structure, it could be something like this: type TWMMYMessage = record Msg: Cardinal; // ( first is the message ID ) Handle: HWND; // ( this is the wParam, Handle of sender) Info: Longint; // ( this is lParam, pointer to our data) Result: Longint; end; // 2. Override your form's DefaultHandler method and add // method for handling your message, like this TForm1 = class(TForm) ...public { Public declarations } ...procedure DefaultHandler(var Message); override; procedure WMMYMessage(var Msg: TWMMYMessage); ...end; // 3. Declare message variable: var WM_OURMESSAGE: DWORD; // 4. Insert realisation of DefaultHandler and our message handler methods: procedure TForm1.DefaultHandler(var Message); var ee: TWMMYMessage; begin with TMessage(Message) do begin if (Msg = WM_OURMESSAGE) then begin ee.Msg := Msg; ee.Handle := wParam; ee.Info := lParam; // Checking if this message is not from us if ee.Handle Handle then WMMYMessage(ee); end else inherited DefaultHandler(Message); end; end; procedure TForm1.WMMYMessage(var Msg: TWMMYMessage); begin label1.Caption := Format('Our another form handle :%d', [Msg.Handle]); Label2.Caption := Format('Our another form top :%d', [Msg.Info]); end; // 5. Add registration of your message that you could // 5. Die Message registrieren. initialization WM_OURMESSAGE := RegisterWindowMessage('Our broadcast message'); // 6. Add the message sending somewhere: procedure TForm1.Button1Click(Sender: TObject); begin SendMessage(HWND_BROADCAST, WM_OURMESSAGE, Handle, Top); end; // 7. Compile and run two copies of your application and test it functionality.