Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Send data from one Delphi application to another 1

Status
Not open for further replies.

Stretchwickster

Programmer
Apr 30, 2001
1,746
GB
This technique uses wm_CopyData and provides an easy means of sending data between applications. The sample code given is by no means bullet-proof - so adapt it at your leisure!!! The code passes a filename from Form1 of one application to MyForm of another application which loads the specified file into a RichEdit box.

Here are the steps you need to implement:
1) Ensure the Messages unit is in the Uses section of both applications .

2) In the sending application, place the following code in the OnClick event of a button or wherever is appropriate for your application:
Code:
var
  hwnd: THandle;
  DataStruct: CopyDataStruct;
begin
  // get the handle of the window
  hwnd := FindWindow('TMyForm', nil);
  if hwnd <> 0 then
  begin
    DataStruct.dwData := 0;
    DataStruct.cbData := length(Ed_Filename.Text) + 1; 
    DataStruct.lpData := PChar(Ed_Filename.Text);
    // activate the receiving window
    SetForegroundWindow(hwnd);
    // send the data
    SendMessage(hwnd, wm_CopyData, Form1.Handle, Integer (@DataStruct));
  end;
end;

3) In the receiving application, declare the following routine:
Code:
procedure CopyData(var Msg: TWMCopyData); message wm_CopyData;
Then add the following code to its' implementation:
procedure TMyForm.CopyData(var Msg: TWmCopyData);
var
Filename: String;
begin
// restore the window if minimized
if IsIconic(Application.Handle) then
Application.Restore;
// extract the data
Filename := PChar(Msg.CopyDataStruct.lpData);
// open the file and goto error line
try
RichEdit1.Lines.LoadFromFile(Filename);
except
MessageDlg('Invalid file', mtWarning, [mbOK], 0);
end;
end;
Clive [infinity]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top