在DLL中封装一个FORM,DLL原代码如下:
unit moduleForm;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls;type
  TDllFrm = class(TForm)
    MonthCalendar1: TMonthCalendar;
    procedure MonthCalendar1DblClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
function showCalendar(AHandle: THandle; ACaption: pchar):TDate; stdcall;implementation{$R *.dfm}procedure TDllFrm.MonthCalendar1DblClick(Sender: TObject);
begin
   close;
end;function showCalendar(AHandle: THandle; ACaption: pchar):TDate;
var
  DllFrm: TDllFrm;
begin
  Application.Handle:=AHandle;
  DllFrm:=TDllFrm.Create(application);
  try
     DllFrm.Caption :=ACaption;
     DllFrm.ShowModal;
     result:=DllFrm.MonthCalendar1.Date;
  finally
     DllFrm.Free;
  end;
end;end.library DllForm;{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }uses
  SysUtils,
  Classes,
  moduleForm in 'moduleForm.pas' {DllFrm};exports
  showCalendar;  {$R *.res}begin
end.
测试程序如下:
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}function showCalendar(AHandle: THandle; ACaption: pchar):TDate; stdcall; external'DllForm';procedure TForm1.Button1Click(Sender: TObject);
begin
  showCalendar(application.handle,'testDLLForm');
end;end.
program Project1;uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1};{$R *.res}begin(***)
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.
现在出现的问题是在程序运行到(***)行时就出现一个CPU信息窗口.提示程序有错.请问这是什么原因呢?怎样解决!多谢每个回答的朋友!

解决方案 »

  1.   

    你的代码应该没问题,你在声明函数时把DLL的完整路径加进去看一下,例如:
    function showCalendar(AHandle: THandle; ACaption: pchar):TDate; stdcall; external'E:\MyErp\BIN\DllForm.dll';
      

  2.   

    这是d5开发指南里的例子吧?
    你把你做的dll和你的测试程序放一个目录吧~
      

  3.   

    没仔细看楼主贴的代码了,不过DLL封装FORM可以参看此贴http://expert.csdn.net/Expert/topic/2048/2048544.xml?temp=.3620722(窗体在游戏里弹出,不失真!如何做!快快快!)中mosker(sdfg)贴出的代码,也是DLL封装了一个FORM在其他FORM运行时通过热键呼出,代码我测试过很运行。
      

  4.   

    implementation{$R *.dfm}function showCalendar(AHandle: THandle; ACaption: pchar):TDate; stdcall; external'DllForm';///////////这里external'.\DllForm.dll';//如果DllForm.dll在当前目录下的话
      

  5.   

    谢谢各位了.我把DLL路径加完整后就可以了.还有DLL库的后缀DLL加上.
      

  6.   

    你的加载DLL文件中有点小问题,不能“DLLForm”,要不加全路径,要不将Dll文件放在应用工程目录下,为"DllForm.dll"
      

  7.   

    楼主,你好好看看《Delphi 5开发人员指南》,这本书写得非常清楚,而且有源代码!!!
      

  8.   

    ShareMem must be the
      first unit in your library's USES clause