Mega Code Archive

 
Categories / Delphi / Examples
 

Msgfromanotherapp

GET MESSAGE (IE DATA) FROM ANOTHER APPLICATION Date sent: Wed, 24 Feb 1999 16:27:35 -0500 From: Brad Stowers <bstowers@pobox.com> To: Delphi@Kyler.com Subject: Re: Getting a message from another app Send reply to: Delphi@Kyler.com > var > cds : TcopyDataStruct; > Hwnd : Thandle; > begin > cds.dwData := Top; > cds.cbData := length('Hello')+1; > getmem (cds.lpData , pchar('Hello')); > strcopy (cds.lpData , pchar('Hello'); > Hwnd := FindWindow('Tform1','Form1'); > if Hwnd <> 0 then > SendMessage ( Hwnd , WM_RECEIVE_NOTIFICATION , Cardinal(@cds)) > end; > and at the get data you have to make the following > procdure getdata (var msg : TwmCopyData); message M_RECEIVE_NOTIFICATION; > and at it's implementation you add > procedure getdata (var msg : TwmCopyData); > begin > ... work with data > end; Don't forget to free memory that you allocate: > var > cds : TcopyDataStruct; > Hwnd : Thandle; > begin > cds.dwData := Top; > cds.cbData := length('Hello')+1; > getmem (cds.lpData , pchar('Hello')); > strcopy (cds.lpData , pchar('Hello'); > Hwnd := FindWindow('Tform1','Form1'); > if Hwnd <> 0 then > SendMessage ( Hwnd , WM_RECEIVE_NOTIFICATION , Cardinal(@cds)) freemem(cds.lpdata); > end; If you use PostMessage instead of SendMessage it gets messier. Generally, in that case, you would free the memory in the "poster" if PostMessage failed, and free it in the "receiver" if it didn't. With SendMessage you don't have to worry about it since SendMessage won't return until after the message has been processed (the data has been used). Regards, Brad Stowers Delphi Free Stuff -- http://www.delphifreestuff.com/ "If you're a professional Delphi programmer who hasn't read the Object Pascal Language Guide, then you're not." **************************************************** If you don't want to see any more of these messages, send a message to: MajorDomo@Kyler.com with a message of: UNSUBSCRIBE delphi