CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Delphi >  语言基础/算法/系统设计

DLL调用正常,但在freelibrary出错。

楼主caike(ck)2005-10-19 13:41:15 在 Delphi / 语言基础/算法/系统设计 提问

在程序中我动态调用DLL中的函数,DLL文件中包含一个form,引出的函数将建立一个form,执行都很正常,但在freelibrary时出错,提示如下:    
  Project   e:\delphi\test.exe   raised   too   many   consecutive   exceptions:'access   violation   at   0x00000000:read   of   address   0x00000000',Process   stopped.    
  后来经过各种方法试验,发现不用freelibrary这条语句,程序就执行正常。我觉得奇怪,就试着运行delphi5开发人员指南中的DLL一章的例子,也出现同样的错误。Delphi5开人员指南的例子如下:    
  ================    
  DLL代码    
  ================    
  library   CalendarLib;    
   
  uses    
      ShareMem,    
      SysUtils,    
      Classes,    
      DLLFrm   in   'DLLFrm.pas'   {DLLForm};    
   
  exports    
      ShowCalendar;    
         
  begin    
  end.    
   
  ========    
  DLLFrm.pas代码    
  ========    
  unit   DLLFrm;    
   
  interface    
   
  uses    
      SysUtils,   WinTypes,   WinProcs,   Messages,   Classes,   Graphics,   Controls,    
      Forms,   Dialogs,   Grids,   Calendar;    
   
  type    
   
      TDLLForm   =   class(TForm)    
          calDllCalendar:   TCalendar;    
          procedure   calDllCalendarDblClick(Sender:   TObject);    
      end;    
   
  {   Declare   the   export   function   }    
  function   ShowCalendar(AHandle:   THandle;   ACaption:   String):   TDateTime;   StdCall;    
   
  implementation    
  {$R   *.DFM}    
   
  function   ShowCalendar(AHandle:   THandle;   ACaption:   String):   TDateTime;    
  var    
      DLLForm:   TDllForm;    
  begin    
      //   Copy   application   handle   to   DLL's   TApplication   object    
      Application.Handle   :=   AHandle;    
      DLLForm   :=   TDLLForm.Create(Application);    
      try    
          DLLForm.Caption   :=   ACaption;    
          DLLForm.ShowModal;    
          Result   :=   DLLForm.calDLLCalendar.CalendarDate;   //   Pass   the   date   back   in   Result    
      finally    
          DLLForm.Free;    
      end;    
  end;    
   
  procedure   TDLLForm.calDllCalendarDblClick(Sender:   TObject);    
  begin    
      Close;    
  end;    
   
  end.    
   
  =================================    
  调用程序代码    
  =================================    
  unit   MainFfm;    
   
  interface    
   
  uses    
      SysUtils,   WinTypes,   WinProcs,   Messages,   Classes,   Graphics,   Controls,    
      Forms,   Dialogs,   StdCtrls;    
   
  type    
      TShowCalendar   =   function   (AHandle:   THandle;   ACaption:   String):   TDateTime;   StdCall;    
   
      EDLLLoadError   =   class(Exception);    
   
      TMainForm   =   class(TForm)    
          lblDate:   TLabel;    
          btnGetCalendar:   TButton;    
          procedure   btnGetCalendarClick(Sender:   TObject);    
      end;    
   
  var    
      MainForm:   TMainForm;    
   
  implementation    
   
  {$R   *.DFM}    
   
  procedure   TMainForm.btnGetCalendarClick(Sender:   TObject);    
  var    
      LibHandle   :   THandle;    
      ShowCalendar:   TShowCalendar;    
  begin    
      LibHandle   :=   LoadLibrary('CALENDARLIB.DLL');    
      try    
          if   LibHandle   =   0   then    
              raise   EDLLLoadError.Create('Unable   to   Load   DLL');    
          @ShowCalendar   :=   GetProcAddress(LibHandle,   'ShowCalendar');    
          if   not   (@ShowCalendar   =   nil)   then    
              lblDate.Caption   :=   DateToStr(ShowCalendar(Application.Handle,   Caption))    
          else    
              RaiseLastWin32Error;    
      finally    
          FreeLibrary(LibHandle);   //这一句出错    
      end;    
  end;    
   
  开始我以为是参数类型为String的问题,但后来试着将String改为Pchar,还是出现同样的问题。    
  实在是不知该问题出在哪里,还请大侠们指点,谢谢!    
  问题点数:20、回复次数:7Top

1 楼myjerry(网络猎人)回复于 2005-10-19 22:17:45 得分 5

回去再看  
  帮你Up先!  
  :)Top

2 楼zdgdh(老吴子)回复于 2005-10-21 01:00:59 得分 0

在DLL和EXE相关的DPR文件的interface下的uses   ShareMem,注意ShareMem必须是第一个。Top

3 楼zdgdh(老吴子)回复于 2005-10-21 01:02:12 得分 15

如果还不行,将编译选项的Stack   Frame选上试试。Top

4 楼caike(ck)回复于 2005-10-21 08:21:43 得分 0

to   zdgdh(老吴子)   :  
  大侠,我程序中uses第一句是ShareMem单元呀!  
  编译选项下哪有Stack   Frame?我是用Delphi7Top

5 楼zdgdh(老吴子)回复于 2005-10-21 17:16:25 得分 0

注意不论是DLL还是EXE,而且是在DPR文件中,否则没用。在Code   Generation组中的第二项。Top

6 楼caike(ck)回复于 2005-10-23 20:41:29 得分 0

to   zdgdh(老吴子):大侠你好!  
  您说的Code   Generation是在哪个菜单下?我在Project的Options中没有看到这一项呀,在tool-Debug   option中也没有看到这一项呀!  
   
   
   
  ====CSDN   小助手   V2.0   2005年10月16日发布====  
  CSDN小助手是一款脱离浏览器也可以访问Csdn论坛的软件  
  界面:http://blog.csdn.net/Qqwwee_Com/archive/2005/10/16/504620.aspx  
  下载:http://szlawbook.com/csdnv2/csdnv2.rar  
   
  为神六喝彩,向所有科技工作者致敬!  
  拒绝日货。Top

7 楼caike(ck)回复于 2005-10-26 15:42:29 得分 0

谢谢各位!问题解决!  
  Top

相关问题

  • DLL调用出错
  • 调用.dll出错
  • 弱问:DLL调用出错
  • Dll调用出错?who can help me?
  • vc调用vc的DLL出错??
  • Dll的调用为什么会出错?
  • 调用DLL出错,请高手指教!
  • 动态调用DLL出错为何?
  • dll调用出错,附源码
  • 调用DLL 出错,以知代码 ?

关键词

  • 文件
  • delphi
  • csdn
  • dll
  • application
  • dllfrm
  • dllform
  • sharemem
  • freelibrary
  • tdllform

得分解答快速导航

  • 帖主:caike
  • myjerry
  • zdgdh

相关链接

  • Delphi类图书
  • Delphi类源码下载
  • Delphi控件下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo