我想在一个应用程序中用ClientDataSet取得表的记录,在另一个应用程序中通过共享内存查询这些记录。
请问应该如何设置共享内存?下面是我写的代码:-----用ClientDataSet取得表记录设置共享的代码------
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, DB, DBClient, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
FileCds: TClientDataSet;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
FileHandle : THandle;
ShareCds: TClientDataSet;
public
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
FileCds.Close;
FileCds.FileName := '表文件';
FileCds.Open; FileHandle := CreateFileMapping($ffffffff, nil, Page_ReadOnly,
0, Sizeof(TClientDataSet), Pchar('ShareCds'));
if FileHandle <> 0 then
begin
ShareCds := MapViewOfFile(FileHandle, Windows.FILE_MAP_READ, 0, 0, Sizeof(TClientDataSet));
if ShareCds = nil then
ShowMessage('内存映射文件失败');
end; ShareCds.Data := FileCds.Data;//这里报错了end;procedure TForm1.Button2Click(Sender: TObject);
begin
if ShareCds <> nil then
windows.UnmapViewOfFile(ShareCds);
if FileHandle<> 0 then
windows.CloseHandle(FileHandle);
end;end.
--------------------------------
我发现在上述代码中,虽然我为ShareCds分配了内存地址,却没有为对象初始化,所以无法赋值
------------------------------------------通过共享内存查询记录的代码----------
unit OpenUnit;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, DB, DBClient, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
MetaCds: TClientDataSet;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
FileHandle : THandle;
ShareCds: TClientDataSet;
public
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
FileHandle := OpenFileMapping(Windows.FILE_MAP_READ, True, Pchar('ShareCds'));
if FileHandle <> 0 then
begin
ShareCds := MapViewOfFile(FileHandle, Windows.FILE_MAP_READ, 0, 0, Sizeof(TClientDataSet));
if ShareCds = nil then
ShowMessage('内存映射文件失败');
end; MetaCds:= ShareCds;end;procedure TForm1.Button2Click(Sender: TObject);
begin
if ShareCds <> nil then
windows.UnmapViewOfFile(ShareCds);
if FileHandle<> 0 then
windows.CloseHandle(FileHandle);
end;end.求教!!!!!!!!!!!!!!!!!
请问应该如何设置共享内存?下面是我写的代码:-----用ClientDataSet取得表记录设置共享的代码------
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, DB, DBClient, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
FileCds: TClientDataSet;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
FileHandle : THandle;
ShareCds: TClientDataSet;
public
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
FileCds.Close;
FileCds.FileName := '表文件';
FileCds.Open; FileHandle := CreateFileMapping($ffffffff, nil, Page_ReadOnly,
0, Sizeof(TClientDataSet), Pchar('ShareCds'));
if FileHandle <> 0 then
begin
ShareCds := MapViewOfFile(FileHandle, Windows.FILE_MAP_READ, 0, 0, Sizeof(TClientDataSet));
if ShareCds = nil then
ShowMessage('内存映射文件失败');
end; ShareCds.Data := FileCds.Data;//这里报错了end;procedure TForm1.Button2Click(Sender: TObject);
begin
if ShareCds <> nil then
windows.UnmapViewOfFile(ShareCds);
if FileHandle<> 0 then
windows.CloseHandle(FileHandle);
end;end.
--------------------------------
我发现在上述代码中,虽然我为ShareCds分配了内存地址,却没有为对象初始化,所以无法赋值
------------------------------------------通过共享内存查询记录的代码----------
unit OpenUnit;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, DB, DBClient, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
MetaCds: TClientDataSet;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
FileHandle : THandle;
ShareCds: TClientDataSet;
public
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
FileHandle := OpenFileMapping(Windows.FILE_MAP_READ, True, Pchar('ShareCds'));
if FileHandle <> 0 then
begin
ShareCds := MapViewOfFile(FileHandle, Windows.FILE_MAP_READ, 0, 0, Sizeof(TClientDataSet));
if ShareCds = nil then
ShowMessage('内存映射文件失败');
end; MetaCds:= ShareCds;end;procedure TForm1.Button2Click(Sender: TObject);
begin
if ShareCds <> nil then
windows.UnmapViewOfFile(ShareCds);
if FileHandle<> 0 then
windows.CloseHandle(FileHandle);
end;end.求教!!!!!!!!!!!!!!!!!
解决方案 »
- 下午delphi面试,his方面,怎么办?
- DLL:cannot assign a tfont to a tfont
- 怎样才能得到TDBGrid中的记录数?
- 如何在windows"开始-程序"菜单中添加和删除菜单项?高分送!
- 第27题):文件夹的保护资料?
- 如何判断按下的键为非功能键?so 菜
- 关于MaskEdit的清空问题
- 请问高手们如何用delphi做一个远程启动的程序,也就是说在一台计算机上启动另一台计算机中的程序。
- 如何使应用程序启动最大化?
- 关于chart的问题
- 请问FLASH语法和Delphi的问题!谢谢
- 请问用mscomm发送数据如何知道串口已经把最后一位发出了?outBuffersite =0 不准啊。急啊!
得到内存指针,然后CopyMemory到内存映射文件另一程序中定义一个Variant,
VarArrayLock后将共享内存Copy出来,将内容写进去,
然后把Variant赋值给ClientDataSet.Data再查询