我想在一个应用程序中用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.求教!!!!!!!!!!!!!!!!!