G
go_soft
Unregistered / Unconfirmed
GUEST, unregistred user!
示例:
首先创建一个DLL,这个DLL用于创建文件映射对象,建立映射关系,解除映射关系和释放文件映射对象,并且输出一个对程,该过程返回一个共享内存区的指针,外部程序正是通过这个指针来访问公共区域的
library ShareData;
uses
SysUtils,Windows,Forms,
Classes;
{$R *.res}
var
hObject: THandle;
PMem: Pointer;
procedure UnmapMem;
begin
if Assigned(PMem) then
UnmapViewOfFile(PMem);
PMem := nil;
end;
procedure MapMem;
begin
hObject := CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,0,$ffff,PChar('_ShareMem'));
if hObject = 0 then
Raise Exception.Create('创建文件映射对象未成功');
if not Assigned(PMem) then
begin
UnmapMem;
Raise Exception.Create('建立映射关系没有成功');
end;
end;
procedure DLLMain(dwReason: DWord);
begin
Case dwReason of
DLL_PROCESS_ATTACH:
begin
PMem := nil;
hObject := 0;
MapMem;
end;
DLL_PROCESS_DETACH:
UnmapMem;
end;
end;
function GetPointerOfMem: Pointer;StdCall;
begin
Result := PMem;
end;
Exports GetPointerOfMem;
begin
DLLProc := @DLLMain;
DLLMain(DLL_PROCESS_ATTACH);
end.
用下例的运用程序来检测能否实现数据共享:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Memo1: TMemo;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Buf: PChar;
function GetPointerOfMem: Pointer;StdCall;External 'ShareData.dll'
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit1.GetTextBuf(Buf,Length(Edit1.Text)+1);
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Memo1.SetTextBuf(Buf);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Buf := GetPointerOfMem;
end;
end.
为什么我运行运用程序时要出现异常错误呢?请大家帮忙看问题在哪里?
首先创建一个DLL,这个DLL用于创建文件映射对象,建立映射关系,解除映射关系和释放文件映射对象,并且输出一个对程,该过程返回一个共享内存区的指针,外部程序正是通过这个指针来访问公共区域的
library ShareData;
uses
SysUtils,Windows,Forms,
Classes;
{$R *.res}
var
hObject: THandle;
PMem: Pointer;
procedure UnmapMem;
begin
if Assigned(PMem) then
UnmapViewOfFile(PMem);
PMem := nil;
end;
procedure MapMem;
begin
hObject := CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,0,$ffff,PChar('_ShareMem'));
if hObject = 0 then
Raise Exception.Create('创建文件映射对象未成功');
if not Assigned(PMem) then
begin
UnmapMem;
Raise Exception.Create('建立映射关系没有成功');
end;
end;
procedure DLLMain(dwReason: DWord);
begin
Case dwReason of
DLL_PROCESS_ATTACH:
begin
PMem := nil;
hObject := 0;
MapMem;
end;
DLL_PROCESS_DETACH:
UnmapMem;
end;
end;
function GetPointerOfMem: Pointer;StdCall;
begin
Result := PMem;
end;
Exports GetPointerOfMem;
begin
DLLProc := @DLLMain;
DLLMain(DLL_PROCESS_ATTACH);
end.
用下例的运用程序来检测能否实现数据共享:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Memo1: TMemo;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Buf: PChar;
function GetPointerOfMem: Pointer;StdCall;External 'ShareData.dll'
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit1.GetTextBuf(Buf,Length(Edit1.Text)+1);
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Memo1.SetTextBuf(Buf);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Buf := GetPointerOfMem;
end;
end.
为什么我运行运用程序时要出现异常错误呢?请大家帮忙看问题在哪里?