?
Solved

Delphi - How send a structure to an other probramm by using  SendMessage function

Posted on 2005-03-23
6
Medium Priority
?
798 Views
Last Modified: 2010-04-05
HI everybody,

I'm tryind to send a structure from Program "A" to program "B" by using sendmessage function.

My structure is defined into a (Program "A" and Program "B") common library like this
==>

TIdentifier record of
  ID  : String;
  SID : String;
end;


Into program "A" I'm  fulling my structure as below
==>

Program "A"
var
   Identifier: TIdentifier;
begin
  Identifier.PID := 'PID';
  Identifier.SID := 'SID';
  sendmessage(ProgramBHandle,1101,0,integer(@Identifier));
end;


I'm receiving the message into program "A" like below
==>
procedure Tform1.WndProc(var Msg : TMessage);
var
  PID:string;
  MsgParm: integer;
  Identifier: TIdentifier;
begin
  if msg.Msg = 1101 then begin
    messagebeep(0);
    id := Pointer(msg.lParam);
    Identifier(id).
    PID:=id^.PID;
    showmessage(PID);
    PID:=id.PID;
    showmessage(PID);
  end;
  inherited;
end;

But my problem is... can't read my structure. I'm receiving the structure address but I can't
do anything.
0
Comment
Question by:jlvill
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 2

Expert Comment

by:philipjc
ID: 13612387
You may be getting an address but its not correct, remember in win32 each process has it own address space so when you try to read program A's address in program B, program B is actually looking in it own address space

To do what you want you should use memory mapped files or use some sort of interprocess communication like pipes etc.
0
 
LVL 46

Expert Comment

by:aikimark
ID: 13612475
TIdentifier record of
  ID  : String;


Identifier.PID := 'PID';
====================
How can this work?
0
 
LVL 46

Expert Comment

by:aikimark
ID: 13612588
philipjc is probably on the correct path.  You need to pass your object, not a pointer to your object.

Possible solution paths:
* memory mapped files (philipjc)
* named pipes (philipjc)
* atoms
* TCP (you can stream objects)
* UDP one-way messages (you need to establish msg structure)
* MSMQ for guraranteed delivery
* You can use the file system to transmit data (requires programs to hook the Windows Shell for changes notification or timer-based monitoring)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Accepted Solution

by:
geobul earned 500 total points
ID: 13612592
Hi,

The following is an example of using  WM_COPYDATA message:
The sending app:
----------
type
  TIdentifier = record
    PID  : String[255]; // it cannot be 'string' here
    SID : String[255];
  end;

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    procedure SendToServer(WindowName: string; data: TIdentifier);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Identifier: TIdentifier;

implementation

{$R *.DFM}

procedure TForm1.SendToServer(WindowName: string; data: TIdentifier);
var
  COPYDATA: TCopyDataStruct;
  h: HWND;
begin
  h := FindWIndow(nil, PChar(WindowName));
  if IsWindow(h) then begin
    // Fill the COPYDATASTRUCT:
    with COPYDATA do begin
      dwData:= 0;
      cbData:= SizeOf(data); // size
      lpData:=@data; // address
    end;
    // Send to the server:
    SendMessage(h, // Who are we sending to?
             WM_COPYDATA,  // This message enables it all
             Handle,       // Handle of the sender
             Integer(@COPYDATA) // Address of the filled COPYDATASTRUCT
            );
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Identifier.PID := 'PID';
  Identifier.SID := 'SID';
  SendToServer('ServerApp', Identifier); // replace 'ServerApp' with the correct title or
          // modify SendToServer function to accept HWND directly.
end;
--------
The receiving app:
----------
type
  TIdentifier = record
    PID  : String[255];
    SID : String[255];
  end;

  TForm1 = class(TForm)
  private
    { Private declarations }
    procedure WMCopyData(var Msg: TWMCopyData); message WM_COPYDATA;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  data: TIdentifier;

implementation

{$R *.DFM}

procedure TForm1.WMCopyData(var Msg: TWMCopyData);
begin
  data := TIdentifier(Msg.CopyDataStruct^.lpData^);
  ShowMessage(data.PID+' '+data.SID);
end;
--------

Regards, Geo
0
 
LVL 12

Assisted Solution

by:esoftbg
esoftbg earned 500 total points
ID: 13613187
download an example from:
page:        http://geocities.com/esoftbg/
  link:        Q_21057608.zip
0
 

Author Comment

by:jlvill
ID: 13618710
Great!!!
Thanks esoftbg
Thanks Geo P.S I corrected  FindWIndow(nil, PChar(WindowName)) by  FindWIndow(PChar(WindowName),nil)

It's working!

philipjc and aikimark you're right, the program not run in the same address space.

Thanks all of you ;-)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question