动态调用dll时的问题。请高手帮助(100分)

  • 主题发起人 主题发起人 fxh7622
  • 开始时间 开始时间
<br>实际调用的窗体的单元(窗体上只放了一个按钮)<br>unit dfd;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>&nbsp; TdllF=function:string;stdcall;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; H:THandle;<br>&nbsp; fdll:TDLLF;<br>begin<br>&nbsp; h:=safeloadlibrary(pchar('dll.dll'));<br>&nbsp; if h&gt;0 then<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp;@fdll:=getprocaddress(h,'getstr');<br>&nbsp; &nbsp; &nbsp; &nbsp;if @fdll&lt;&gt;nil then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; showmessage(fdll);<br>&nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; &nbsp;freelibrary(h);<br>&nbsp; &nbsp; end;<br>end;<br><br>end.
 
正在试,成功马上给分。谢谢。
 
这是一段<br>动态调用的代码,你看看对你有没有帮助吧<br>interface<br><br>uses<br>&nbsp; SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,<br>&nbsp; Forms, Dialogs, StdCtrls;<br><br>type<br>&nbsp; { First, define a procedural data type, this should reflect the<br>&nbsp; &nbsp; procedure that is exported from the DLL. }<br>&nbsp; TShowCalendar = function (AHandle: THandle; ACaption: String): TDateTime; StdCall;<br><br>&nbsp; { Create a new exception class to reflect a failed DLL load }<br>&nbsp; EDLLLoadError = class(Exception);<br><br>&nbsp; TMainForm = class(TForm)<br>&nbsp; &nbsp; lblDate: TLabel;<br>&nbsp; &nbsp; btnGetCalendar: TButton;<br>&nbsp; &nbsp; procedure btnGetCalendarClick(Sender: TObject);<br>&nbsp; end;<br><br>var<br>&nbsp; MainForm: TMainForm;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TMainForm.btnGetCalendarClick(Sender: TObject);<br>var<br>&nbsp; LibHandle &nbsp; : THandle;<br>&nbsp; ShowCalendar: TShowCalendar;<br>begin<br><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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; finally<br>&nbsp; &nbsp; FreeLibrary(LibHandle); // Unload the DLL.<br>&nbsp; end;<br>end;<br><br>end.
 
多人接受答案了。
 
后退
顶部