A:关闭调用的DLL窗体时报错:'access violation at 0x00031285:write of address 0x10c00000'.
B:主调窗体也最小化了.
DLL窗体代码如下:
unit DLL_Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;type
  Tdllform = class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
  private
    { Private declarations }
  public
    { Public declarations }
  end;
function showcalendar(ahandle:thandle):longint;stdcall;
var
  dllform: Tdllform;implementation{$R *.dfm}
function showcalendar(ahandle:thandle):longint;stdcall;begin  application.Handle:=ahandle;
  dllform:=tdllform.Create(application);
   try    dllform.ShowModal;
    result:=longint(dllform);
  finally
       dllform.Free;
       end;
end;
end.DLL代码如下:
library DLL;
uses
  SysUtils,
  Classes,
  DLL_Unit1 in 'DLL_Unit1.pas' {Form1};
  exports
 showcalendar;
{$R *.res}begin
end.
主调程序如下:
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons,wintypes,winprocs;
type
  tshowcalendar=function(ahandle:thandle;acaption:string):tdatetime;stdcall;
   EDLLLOADERROR=CLASS(EXCEPTION);
  Tmainform = class(TForm)    btngetcalendar: TBitBtn;
    BitBtn2: TBitBtn;
    lbldate: TLabel;
    procedure btngetcalendarClick(Sender: TObject);  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  mainform: Tmainform;implementation{$R *.dfm}procedure Tmainform.btngetcalendarClick(Sender: TObject);
var
 libhandle:thandle;
showcalendar:tshowcalendar;
begin
 libhandle:=loadlibrary('DLL.dll');
 try
   if libhandle = 0 then
   RAISE EDLLLOADERROR.CREATE('SFSE');// else showmessage('DLL不存在');   @showcalendar:=getprocaddress(libhandle,'showcalendar');
   if not (@showcalendar=nil)then
    showcalendar(application.Handle ,caption)
    else
     raiselastwin32error;
 finally
  freelibrary(libhandle);
  end;
end;end.

解决方案 »

  1.   

    DLL内窗体关闭的时候
    Action:=caFree;
      

  2.   


    unit DLL_Unit1; interface uses 
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
      Dialogs, StdCtrls, ComCtrls; type 
      Tdllform = class(TForm) 
        Label1: TLabel; 
        Edit1: TEdit; 
      private 
        { Private declarations } 
      public 
        { Public declarations } 
      end; 
    function showcalendar(ahandle:thandle):longint;stdcall; {---------------这里注掉----------------}
    //var 
     // dllform: Tdllform; implementation {$R *.dfm} 
    function showcalendar(ahandle:thandle):longint;stdcall; 
    var 
      dllform: Tdllform; {-------上边注掉的加到这里-----------------}
    begin   application.Handle:=ahandle; 
      dllform:=tdllform.Create(application); 
      try     dllform.ShowModal; 
        result:=longint(dllform); 
      finally 
        dllform.Release; {-----------------改了这里}
      end; 
    end; 
    end. 
      

  3.   

    public 
        { Public declarations } 
      end; 
    function showcalendar(ahandle:thandle):longint;stdcall; {---------------这里注掉----------------}
    //var 
     // dllform: Tdllform; implementation {$R *.dfm} 
    注掉后,又报错'error dll_project1.dpr(11):undeclared identifier:'dllform''
      

  4.   

    错得一踏糊涂!exe与dll交互的接口都 不一样,能不出错吗?没有办法,只好把你的代码重新调整了一下///exe
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      //tshowcalendar = function(ahandle: thandle; acaption: string): tdatetime; stdcall;
      tshowcalendar = function(ahandle: thandle; acaption: PChar): tdatetime; stdcall;  EDLLLOADERROR = class(EXCEPTION);
      TMainForm = class(TForm)
        btngetcalendar: TButton;
        procedure btngetcalendarClick(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      MainForm: TMainForm;implementation{$R *.dfm}procedure TMainForm.btngetcalendarClick(Sender: TObject);
    var
      libhandle: thandle;
      showcalendar: tshowcalendar;
    begin
      libhandle := loadlibrary('DLL.dll');
      try
        if libhandle = 0 then
          raise EDLLLOADERROR.CREATE('SFSE'); // else showmessage('DLL不存在');    @showcalendar := getprocaddress(libhandle, 'showcalendar');
        if not (@showcalendar = nil) then
          showcalendar(application.Handle, PChar(caption))
        else
          raiselastwin32error;
      finally
        freelibrary(libhandle);
      end;
    end;end.//dll
    unit DLL_Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      Tdllform = class(TForm)
        Label1: TLabel;
        Edit1: TEdit;
      private
        { Private declarations }
      public
        { Public declarations }
      end;function showcalendar(ahandle: thandle; acaption: PChar): tdatetime; stdcall;implementation{$R *.dfm}function showcalendar(ahandle: thandle; acaption: PChar): tdatetime; stdcall;
    var
      dllform: Tdllform;
    begin
      application.Handle := ahandle;
      dllform := tdllform.Create(application);
      try
        dllform.Caption := acaption; ///++++++++
        dllform.ShowModal;
        ///result := longint(dllform);
        Result := Now; ///+++++++++
      finally
        dllform.Free;
      end;
    end;end.
      

  5.   

    DLL窗体关闭后,主调窗体被最小化了.怎么回事呀,我没有设它最小化呀.
      

  6.   

    TO:blazingfire     { Private declarations }
      public
        { Public declarations }
      end;
    这个地方不加var
      dllform: Tdllform;就报错.'[error]dll_p.dpr(11):undeclared identifier;'dllform'',加了之后也行,请叫了.

    function showcalendar(ahandle: thandle; acaption: PChar): tdatetime; stdcall;implementation{$R *.dfm}function showcalendar(ahandle: thandle; acaption: PChar): tdatetime; stdcall;
    var
      dllform: Tdllform;
    begin
      application.Handle := ahandle;
      

  7.   

    DLL窗体关闭后,主调窗体被最小化了.怎么回事呀,我没有设它最小化呀.
      

  8.   

    结出的信息不足,我也不懂。
    别外你请教问题请用“请教”,不要用“请叫”,这样不点也不幽默,反而让人心里发毛!调用的时候别用Application.Handle,用Application.MainForm.Handle