代码如下,功能是,当运行两个程序时,第二个程序的Edit1,Edut2 得到文件映射对象的句柄和记录在共享内存里面的数据。不明白的地方时,当记录程序的句柄有效,而当想记录字符串类型,运行第2个程序不能得到记录的字符串,请前辈们指点。谢谢~
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;const
MapFileName = '{CAF49BBB-AF40-4FDE-8757-51D5AEB5BBBF}';type
TForm1 = class(TForm)
edt1: TEdit;
edt2: TEdit;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;type
//共享内存
PShareMem = ^TShareMem;
TShareMem = record AppHandle: THandle; //保存程序的句柄
//str:string ;
end;var
Form1: TForm1;
hMapFile: THandle;
PSMem: PShareMem;implementation{$R *.dfm}
procedure CreateMapFile;
begin
hMapFile := OpenFileMapping(FILE_MAP_ALL_ACCESS, False, PChar(MapFileName));
if hMapFile = 0 then
begin
hMapFile := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0,SizeOf(TShareMem), MapFileName);
PSMem := MapViewOfFile(hMapFile, FILE_MAP_WRITE or FILE_MAP_READ, 0, 0, 0);
//PSMem^.str := 'ni hao' ;
PSMem^.AppHandle := Application.Handle ;
end
else
begin
PSMem := MapViewOfFile(hMapFile,FILE_MAP_ALL_ACCESS, 0, 0, 0);
end;
Form1.edt1.Text := IntToStr(hMapFile );
//Form1.edt2.Text := PSMem^.str ;
Form1.edt2.Text := IntToStr(PSMem^.AppHandle );
end;procedure TForm1.FormCreate(Sender: TObject);
begin
CreateMapFile ;
end;
procedure FreeMapFile;
begin
UnMapViewOfFile(PSMem);
CloseHandle(hMapFile );
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
FreeMapFile ;
end;end.
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;const
MapFileName = '{CAF49BBB-AF40-4FDE-8757-51D5AEB5BBBF}';type
TForm1 = class(TForm)
edt1: TEdit;
edt2: TEdit;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;type
//共享内存
PShareMem = ^TShareMem;
TShareMem = record AppHandle: THandle; //保存程序的句柄
//str:string ;
end;var
Form1: TForm1;
hMapFile: THandle;
PSMem: PShareMem;implementation{$R *.dfm}
procedure CreateMapFile;
begin
hMapFile := OpenFileMapping(FILE_MAP_ALL_ACCESS, False, PChar(MapFileName));
if hMapFile = 0 then
begin
hMapFile := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0,SizeOf(TShareMem), MapFileName);
PSMem := MapViewOfFile(hMapFile, FILE_MAP_WRITE or FILE_MAP_READ, 0, 0, 0);
//PSMem^.str := 'ni hao' ;
PSMem^.AppHandle := Application.Handle ;
end
else
begin
PSMem := MapViewOfFile(hMapFile,FILE_MAP_ALL_ACCESS, 0, 0, 0);
end;
Form1.edt1.Text := IntToStr(hMapFile );
//Form1.edt2.Text := PSMem^.str ;
Form1.edt2.Text := IntToStr(PSMem^.AppHandle );
end;procedure TForm1.FormCreate(Sender: TObject);
begin
CreateMapFile ;
end;
procedure FreeMapFile;
begin
UnMapViewOfFile(PSMem);
CloseHandle(hMapFile );
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
FreeMapFile ;
end;end.
解决方案 »
- fastreport做报表,如何让每一页都显示相同的title和header?
- MDI窗体中有什幺方法可以使DLL子窗体的菜单融合。
- 请教:在stringgrid中修改单元格的问题
- 元芳,此事你怎么看?
- 有关事务管理的问题,各位大虾也说说啊!(先解答先得分喔)在线等待
- 在李维的书中说在D5中用TDCOMConnection建立MTS/COM+对象之前需对其中的两个方法进行修改,那么在D6中还需要修改吗?
- 这种记录结构是什么意思?
- 求教ActionMainMenubar中菜单分隔线如何实现?
- 在DELPHI中如何编写锁住硬盘某一目录的程序?
- 确认TTreeView中每一个结点的终身ID
- TPageControl 的一个问题。。。。。。。。。
- 关于创建多线程的问题
或者 PShareMem = ^TShareMem;
TShareMem = record AppHandle: THandle; //保存程序的句柄
str:string[20] ; //指定大小
end;
lz试试2种方法,应该有可行的。