Delphi32.com - Home!
| Home/News | Downloads | Forums | D32 Magazine | Resources | Info and Facts |  
 
 Using WM_COPYDATA.


How can I use the WM_COPYDATA message to pass information between 6-bit and 32-bit applications?   

    The following example shows how to use the WM_COPYDATA message to send a record containing data between a 6-bit and 32-bit application. This method can also be used to call 6-bit or 32-bit dll from a 6-bit or 32-bit application (known as thunking). Thunking can be accomplished by creating an invisible window to respond to the WM_COPYDATA message, and call the dll in your behalf.

    Compile the following unit source code under both 6-bit and 32-bit versions of Delphi. Use separate directories for the two projects. Each project will require a form, a button, and a memo component on it's form.

    To use the WM_COPYDATA message, you must send the address of a TCopyDataStruct to the receiving window. The dwData parameter of the TCopyDataStruc is a user defined parameter that allows you to store any 32 bit value you wish, for additional information. The cbData member of the TCopyDataStruc contains the size of the data pointed to by the lpData member of the TCopyDataStruc. The lpData parameter of the TCopyDataStruct can point to any data structure you wish.

    Note that any pointers contained in the structure you send will not be valid from the receiving application since windows has no way to convert them. Note that the data that is passed to the receiving application is only valid during the call. If the receiving application needs to retain the information passed, it should make a copy of the data that is local to the receiving application. The receiving application should not modify the data that is passed during the call. If modifications are necessary, the receiving application should make a local copy of the data, and send a WM_COPYDATA message back to the sending application using the modified copy.

    It's worth noting that the WM_COPYDATA message will work going to and from any combination of 6/32 bit applications. Finally, be aware that the call to SendMessage will not return untill the message is processed. Also be aware that you should never use PostMessage() with the WM_COPYDATA message.

    unit Unit ;

    interface

    {$IFDEF WIN32} uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; {$ELSE} uses SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; {$ENDIF}

    const WM_COPYDATA = $004A;

    type TForm = class(TForm) Memo : TMemo; Button : TButton; procedure FormCreate(Sender: TObject); procedure Button Click(Sender: TObject); private { Private declarations } procedure WMCopyData(var m : TMessage); message WM_COPYDATA; public { Public declarations } end;

    var Form : TForm ;

    implementation

    {$R *.DFM}

    type PCopyDataStruct = ^TCopyDataStruct; TCopyDataStruct = record dwData: LongInt; cbData: LongInt; lpData: Pointer; end;

    type PRecToPass = ^TRecToPass; TRecToPass = packed record s : string[255]; i : integer; end;

    procedure TForm .FormCreate(Sender: TObject); begin {$IFDEF WIN32} Form .Caption := 'My 32-Bit App' {$ELSE} Form .Caption := 'My 6-Bit App' {$ENDIF} end;

    procedure TForm .WMCopyData(var m : TMessage); begin Memo .Lines.Add('Sending Window Handle := ' + IntToStr(m.WParam)); Memo .Lines.Add('Data Size := ' + IntToStr(PCopyDataStruct(m.LParam)^.cbData)); Memo .Lines.Add('User Defined Data Param := ' + IntToStr(PCopyDataStruct(m.LParam)^.dwData)); Memo .Lines.Add('TRecToPass.s := ' + PRecToPass(PCopyDataStruct(m.LParam)^.lpData)^.s); Memo .Lines.Add('TRecToPass.i := ' + IntToStr( PRecToPass(PCopyDataStruct(m.LParam)^.lpData)^.i)); end;

    procedure TForm .Button Click(Sender: TObject); var h : THandle; cd : TCopyDataStruct; rec : TRecToPass; begin {$IFDEF WIN32} h := FindWindow(nil, 'My 6-Bit App'); rec.s := 'Hello World From: My 32-Bit App'; rec.i := 32; cd.dwData := 3232; {$ELSE} h := FindWindow(nil, 'My 32-Bit App'); rec.s := 'Hello World From: My 6-Bit App'; rec.i := 6; cd.dwData := 6 6; {$ENDIF} cd.cbData := sizeof(rec); cd.lpData := @rec; if h <> 0 then SendMessage(h, WM_COPYDATA, Form .Handle, LongInt(@cd)); end;

    end.

    7/ 6/98 4:3 :28 PM

     



  << Previous Faq     Complete List     Next Faq >>  



 
 Hits/month  2,500,000+ 
 Downloads
 (Since May 2000)
 7,393,709 
 Total Files  6,023 
 Forum msgs  7,670 
 Articles/FAQs  70+/900+ 
Kylix
Tips n Tricks
FAQs
Knowledge Base
Bug Listings
Articles
Books
Newsgroups
Links
Submissions
Testimonials
Advertising
Contact Us
About Us
Search Amazon:
Top Selling Software at Amazon

| Home/News | Downloads | Forums | Resources | Info and Facts | Testimonials |
  Site Search:
 


Comments/Problems: Webmaster@delphi32.com
Copyright © 1998-2006, Delphi32.com. All rights reserved.
Terms of Use