我建立了一个DLL想共享数据:
library Project1;
uses
  SysUtils,
  Windows,
  Dialogs,
  Classes;{$R *.res}
var
  hMem:THandle;
function GetGlobleMem:THandle;export;
begin
    Result:=HMem;
end;
begin
  hMem:=GlobalAlloc(gmem_MOVEABLE and gmem_DDEShare,256);
  if hMem=0 then
    MessageDlg('Could not allocate memory',mtWarning,[mbOk],0);
end.
这是我建立的一个服务器程序。
unit UnitServer;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
  function GetGlobalMem:THandle;far;external 'Project1.dll';var
  Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
  hMem:THandle;
  pMem:PChar;
begin
  hMem:=GetGlobalMem;
  if hMem<>0 then
  begin
    pMem:=GlobalLock(hMem);
    if pMem<>nil then
    begin
      strCopy(pMem,PChar(Memo1.text));
      GlobalUnlock(hMem);
    end
    else
      MessageDlg('Could not Lock Memory block',mtWarning,[mbOK],0);  end;
end;end.
问题出在这下面。
program ProjectServer;
uses
  Forms,
  UnitServer in 'UnitServer.pas' {Form1};{$R *.res}
begin                  //在这里之后程序不能运行了。 
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

解决方案 »

  1.   

    Dll的问题导致你在静态加载时出错
    所以在exe的开始就无法运行了
      

  2.   

    library Project1;
    uses
      SysUtils,
      Windows,
      Dialogs,
      Classes;{$R *.res}
    var
      hMem:THandle;
    function GetGlobleMem:THandle;stdcall;
    begin
        Result:=HMem;
    end;
    ------------------------------------------------------------------
    begin
      hMem:=GlobalAlloc(gmem_MOVEABLE and gmem_DDEShare,256);
      if hMem=0 then
        MessageDlg('Could not allocate memory',mtWarning,[mbOk],0);
    ---------------------------------------------------------------------
    问题可能在这里。
    exports
      GetGlobleMem;
    end.
      

  3.   

    library Project1;
    uses
      SysUtils,
      Windows,
      Dialogs,
      Classes;{$R *.res}
    var
      hMem:THandle;
    function GetGlobleMem:THandle;export;
    begin
      hMem:=GlobalAlloc(gmem_MOVEABLE and gmem_DDEShare,256);
      if hMem=0 then
        MessageDlg('Could not allocate memory',mtWarning,[mbOk],0)
      Result:=HMem;
    end;Exports
      GetGlobleMem;  //导出接口,不然在加载时是找不到这个函数的,即:
                     //function GetGlobalMem:THandle;far;external 'Project1.dll';有错beginend.
      

  4.   

    调用时尽量用stdcall方式调用。