• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 691
  • Last Modified:

delphi string messages

HELLO HERE IS MU code to send STring messages between 2 applications


unit Unit1;
interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, JvComponent, JvSystemPopup;


type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Déclarations privées }
  public
    { Déclarations publiques }
    procedure DefaultHandler(var msg); override;

  end;

var
  Form1: TForm1;
var
  WM_OURMESSAGE: DWORD;

implementation

{$R *.dfm}
procedure TForm1.DefaultHandler(var msg);
var
  buffer: PChar;
  atom_recu:Atom;
  TexteRecu:pchar;
begin

  with TMessage(msg) do
  begin
    if (Msg = WM_OURMESSAGE) then
    begin
      // Checking if this message is not from us
      if WParam <> Handle then  begin;

      ************************** THERE IS A BUG HERE ***********************************************************
       ******************************************************************************************************  
       label1.Caption:=(pchar(pointer(lparam)));
       ******************************************************************************************************  

       end;
    end;
end;
inherited DefaultHandler(msg);
end;


procedure TForm1.Button1Click(Sender: TObject);
var buffer: PChar;
atom_Envoye: Atom;
var s:string;

begin
s:='hello world';
s := s + #0;
   SendMessage(HWND_BROADCAST,WM_OURMESSAGE,Handle,longint(@s[1]));
end;

initialization
  WM_OURMESSAGE := RegisterWindowMessage('Our broadcast message');

end.



Messages are processed but I have  a ERROR (pointer error I suppose) with
label1.Caption:=(pchar(pointer(lparam)));

after I have sent a message with
SendMessage(HWND_BROADCAST,WM_OURMESSAGE,Handle,longint(@s[1]));


how to correct it ?

0
yarek
Asked:
yarek
1 Solution
 
robert_marquardtCommented:
You get the message from another application?
Then any pointer from the other application is invalid in your application because each application uses its own virtual address space.
You either have to use WM_COPYDATA where the OS transfers a data structure between the applications or use a Memory Mapped File as a sharing mechanism.
See Win32 functinos CreateFileMapping and MapViewOfFile.
0
 
Ivanov_GCommented:
How to Send Information (String, Image, Record) Between Two Applications
http://delphi.about.com/od/windowsshellapi/a/wm_copydata.htm
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now