在D6中如何动态调用dll?为什么我在最后FreeLibrary时要出错.(200分)

  • 主题发起人 主题发起人 m_y_h
  • 开始时间 开始时间
M

m_y_h

Unregistered / Unconfirmed
GUEST, unregistred user!
在动态链接库调用结束时有出错信息:<br>"0x00000000"指令引用的"0x00000000"内存.该内存不能为"read".<br><br>dll中的代码:<br>function ShowCalendar(AHandle: THandle; ACaption: String): TDateTime;<br>var<br>&nbsp; DLLForm: TDllForm;<br>begin<br>&nbsp; // Copy application handle to DLL's TApplication object<br>&nbsp; Application.Handle := AHandle;<br>&nbsp; DLLForm := TDLLForm.Create(Application); <br>&nbsp; try<br>&nbsp; &nbsp; DLLForm.Caption := ACaption;<br>&nbsp; &nbsp; DLLForm.ShowModal;<br>&nbsp; &nbsp; Result := DLLForm.calDLLCalendar.CalendarDate; // Pass the date back in Result<br>&nbsp; finally<br>&nbsp; &nbsp; DLLForm.Free;<br>&nbsp; end;<br>end;<br><br>exe程序代码:<br>procedure TMainForm.btnGetCalendarClick(Sender: TObject);<br>var<br>&nbsp; LibHandle &nbsp; : THandle;<br>&nbsp; ShowCalendar: TShowCalendar;<br>begin<br>&nbsp; { Attempt to load the DLL }<br>&nbsp; LibHandle := LoadLibrary('CALENDARLIB.DLL');<br>&nbsp; try<br>&nbsp; &nbsp; { If the load failed, LibHandle will be zero.<br>&nbsp; &nbsp; &nbsp; If this occurs, raise an exception. }<br>&nbsp; &nbsp; if LibHandle = 0 then<br>&nbsp; &nbsp; &nbsp; raise EDLLLoadError.Create('Unable to Load DLL');<br>&nbsp; &nbsp; { If the code makes it here, the DLL loaded successfully, now obtain<br>&nbsp; &nbsp; &nbsp; the link to the DLL's exported function so that it can be called. }<br>&nbsp; &nbsp; @ShowCalendar := GetProcAddress(LibHandle, 'ShowCalendar');<br>&nbsp; &nbsp; { If the function is imported successfully, then set lblDate.Caption to reflect<br>&nbsp; &nbsp; &nbsp; the returned date from the function. Otherwise, show the return raise<br>&nbsp; &nbsp; &nbsp; an exception. }<br>&nbsp; &nbsp; if not (@ShowCalendar = nil) then<br>&nbsp; &nbsp; &nbsp; lblDate.Caption := DateToStr(ShowCalendar(Application.Handle, Caption))<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; //RaiseLastWin32Error;<br>&nbsp; &nbsp; &nbsp; RaiseLastOSError;<br>&nbsp; finally<br>&nbsp; &nbsp; FreeLibrary(LibHandle); // Unload the DLL.<br>&nbsp; end;<br>end;<br>
 
请参考:<br>http://www.delphibbs.com/delphibbs/dispq.asp?lid=0948807
 
看看d动态连接库工程文件中的注释吧!<br>libiary *** 后面的部分(英文),<br>Delphi告诉我们最好用Shortstring或 Pchar代替string类型的传入参数。<br>而且,在工程源文件(project-&gt;view source调出)加入,ShareMem,并且在第一行。<br>你自己的Dll文件中ShareMem也是这个位置。试试吧。:) <br>还有,不呀忘了在系统发行时,带上BORLNDMM.DLL。
 
后退
顶部