How to Send information (String, Image, Record) between-applications

Source: Internet
Author: User

Http://delphi.about.com/od/windowsshellapi/a/wm_copydata.htm

Here is many situation when you need to allow for both applications to communicate. If you don't want to mess with TCP and sockets communication (because both applications is running on the same machine), You can *simply* send (and properly receive) a special Windows message:wm_copydata.

Since handling Windows messages in Delphi are simple, issuing a SendMessage API call along with the wm_copydata filled with The data to was sent is quite straight forward.

Wm_copydata and Tcopydatastruct

The WM_COPYDATA message enables the send data from one application to another. The receiving application receives the data in a tcopydatastruct record.

The tcopydatastruct is defined in the Windows.pas unit and wraps the COPYDATASTRUCT structure that contains the data to be Passed.

Here's the declaration and the description of the Tcopydatastruct record:

typetcopydatastruct=packed RecordDwdata:dword; Up to  +Bits ofData toBe passed toThe receiving application Cbdata:dword; The size,inchbytes ofThe data pointed toBy the lpdata member Lpdata:pointer; Points toData toBe passed toThe receiving application. This member canNil. End;
Send a String over Wm_copydata

For a ' Sender ' application to send data to ' Receiver ' the copydatastruct must be filled and passed using the SendMessage F Unction. Here's how to send a string of value over Wm_copydata:

proceduretsendermainform.sendstring ();varStringtosend:string; copydatastruct:tcopydatastruct;beginStringtosend:='About Delphi Programming'; Copydatastruct.dwdata:=0; Use it toIdentify themessagecontents Copydatastruct.cbdata:=1+Length (stringtosend); Copydatastruct.lpdata:=PChar (stringtosend); SendData (copydatastruct);End;

The SendData custom function locates the receiver using the FindWindow API call:

procedureTsendermainform.SendData(Constcopydatastruct:tcopydatastruct);varReceiverhandle:thandle; Res:integer;beginReceiverhandle:= FindWindow (PChar ('Treceivermainform'), PChar ('Receivermainform' ) ); ifReceiverhandle =0  Then  beginShowMessage ('copydata Receiver not found!' );  Exit; End; Res:=SendMessage (Receiverhandle, Wm_copydata, Integer (Handle), Integer (@copyDataStruct));End;

In the code above, the "Receiver" application is found using the FindWindow API call by passing the class name of the MAI N Form ("Treceivermainform") and the caption of the window ("Receivermainform").

Note:the SendMessage Returns an integer value assigned by the code that handled the WM_COPYDATA message.

Handling wm_copydata-receiving a String

The "Receiver" application handles the Wm_copydata mesage as in:

typeTreceivermainform=class(Tform)Private    procedure Wmcopydata(varMsg:twmcopydata);messageWm_copydata; End;typeThe TwmcopydataRecord  isDeclared as: Twmcopydata=packed Recordmsg:cardinal; From:hwnd; Handle ofThe Window that passed the data copydatastruct:pcopydatastruct;//data passed Result:longint;Use it toSend a value back toThe "Sender"End;ImplementationprocedureTreceivermainform.Wmcopydata(varmsg:twmcopydata);varS:string;beginS:=PChar (Msg.CopyDataStruct.lpData); Send something back Msg. Result: =2006;End;
Sending String, Custom Record or an Image?

The accompanying source code demonstrates how to send a string, record (complex data type) and even graphics (bitmap) to a nother application.

If You cannot wait for the download, here's how to send a Tbitmap graphics:

proceduretsendermainform.sendimage ();varMs:tmemorystream;  Bmp:tbitmap; copydatastruct:tcopydatastruct;beginMS:=tmemorystream.create; TryBMP:=Self .    Getformimage; Trybmp.    Savetostream (MS); finallybmp.    Free; End;
Copydatastruct.dwdata:= Integer (cdtimage); //identify the data copydatastruct.cbdata:=Ms. Size; Copydatastruct.lpdata:=Ms. Memory; SendData (COPYDATASTRUCT);
finallyMs. Free; End;End;//And how to receive it:proceduretreceivermainform.handlecopydataimage (copydatastruct:pcopydatastruct);varMs:tmemorystream;beginMS:=tmemorystream.create; TryMs. Write (copydatastruct.lpdata^, copydatastruct.cbdata); Ms. Position:=0; ReceivedImage.Picture.Bitmap.LoadFromStream (MS); finallyMs. Free; End;End;

Downloadwm_copydata Source code example.

How to Send information (String, Image, Record) between-applications

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.