为什么这个最普通的动态链接库调用程序在D6中运行出错?(100分)

M

m_y_h

Unregistered / Unconfirmed
GUEST, unregistred user!
程序是从一张光盘上拷下来的.<br>在动态链接库调用结束时有出错信息:<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>不要用LoadLibrary函数来动态的加载DLL文件,而是使用直接申明调用的方法。<br>在implementation后直接申明:<br>function ShowCalendar(AHandle: THandle; ACaption: String): TDateTime;external 'DLL文件名称.dll';<br>在程序里就可以自己使用函数ShowCalendar了。<br><br>
 
我想在D6中动态加载dll,不知该怎么做?
 
将 <br>&nbsp; &nbsp; FreeLibrary(LibHandle); // Unload the DLL.<br>放在 form 的 OnDestroy 中。<br><br>声明 LibHandle 为全局变量<br><br>&nbsp;if not(libhandle=null) then free........;
 
jsxjd:<br>我刚试了一下,现在错误出现在form的ondestroy时<br>好像FreeLibrary(LibHandle)执行不正常.
 
顶部