[BDS2006 +Windows2000]
Hi all,
I expect using shared string types between 2 instances of my application. For this, I would like to use the TSharedMem object (found in $(BDS)\Demos\DelphiWin32\VCLWin32\IPCDemos).
I built a very simple project that should enable me to:
1. on 1st instance of my application: when clicking on Button1 -> set shared memory with 'Delphi' string
2. on 2nd instance of my application: when clicking on Button2 -> get shared memory value with expected 'Delphi' string.
But it does not work as expected, ie 'Delphi' string value is not shown when clicking on Button2 (from the 2nd application instance)
Does somebody why? And how to fix it?
Is it not because of the type of pointer (string) that is not made for being used in these conditions?
Moreover, this produces a memory leak at application shutdown if traced with FastMM (memory leak checker)
If that is the case, how to share string types between 2 or more applications
Thanks for help
Didier
****** simple delphi project ********
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls
, IPCThrd; //in $(BDS)\Demos\DelphiWin32\VCLWin32\IPCDemos
type
PSharedInfo = ^TSharedInfo;
TSharedInfo = packed record
UserId: string;
end;
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Déclarations privées }
FSharedMem: TSharedMem;
FSharedInfo: PSharedInfo;
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
FSharedMem := TSharedMem.Create('data', SizeOf(TSharedInfo));
FSharedInfo := PSharedInfo(FSharedMem.Buffer);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FSharedMem.Free;
end;
// set shared memory
procedure TForm1.Button1Click(Sender: TObject);
begin
FSharedInfo^.UserId := 'delphi';
end;
// get shared memory
procedure TForm1.Button2Click(Sender: TObject);
var
s: string;
begin
s := FSharedInfo^.UserId;
showmessage(s);
end;
end.