Link to home
Start Free TrialLog in
Avatar of jlvill
jlvill

asked on

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

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.
Avatar of philipjc
philipjc

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.
Avatar of aikimark
TIdentifier record of
  ID  : String;


Identifier.PID := 'PID';
====================
How can this work?
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)
ASKER CERTIFIED SOLUTION
Avatar of geobul
geobul

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jlvill

ASKER

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 ;-)