动态装入DLL文件的问题(50分)

  • 主题发起人 主题发起人 uranium235
  • 开始时间 开始时间
U

uranium235

Unregistered / Unconfirmed
GUEST, unregistred user!
如何在程序中动态装入dll文件,我指的是在exe程序中输入dll文件名(当它在exe文件目录下时)<br>或用opendialog打开任意目录下的dll文件并装入,然后可以输入dll中的函数名,同时在应用程序<br>中调用这个新函数。
 
这个不难呀,《DELPHI5开发人员指南》上有例子,DFW上也有很多例子!!![:D]
 
??????????<br>我找不到一模一样的例子。。。。。。
 
那你要事先知道函数的参数格式
 
动态转载dll:loadlibrary<br><br>调用函数:GetProcessAddress
 
LoadLibrary<br>The LoadLibrary function maps the specified executable module into the address space of the calling process. <br><br>For additional load options, use the LoadLibraryEx function. <br><br>HMODULE LoadLibrary(<br>&nbsp; LPCTSTR lpFileName &nbsp; // file name of module<br>);<br>不用不要忘了释放!<br>FreeLibrary<br>The FreeLibrary function decrements the reference count of the loaded dynamic-link library (DLL). When the reference count reaches zero, the module is unmapped from the address space of the calling process and the handle is no longer valid. <br><br>BOOL FreeLibrary(<br>&nbsp; HMODULE hModule &nbsp; // handle to DLL module<br>);<br>
 
看一下代码:<br>public<br>&nbsp;h:hwd;<br>var<br>f:function(x,y:integer):integer;stdcall;<br><br>procedure ...<br>var x,y,z:integer<br>begin<br>&nbsp;x:=strtoint(edit1.text);<br>&nbsp;y:=strtoint(edit2.text);<br>&nbsp;f:=nil;<br>&nbsp;h:=loadlibrary(pchar(opendialog1.filename));<br>&nbsp;if h&lt;&gt;nil then<br>&nbsp; f:=GetProcessAddress(pchar('add'));<br>&nbsp;if assigned(f) then<br>&nbsp; z:=f(x,y);<br>&nbsp;edit3.text:=inttostr(z);<br>end;<br><br>错在哪里?似乎可以挂上dll但是取不到函数的地址
 
&nbsp;f:=GetProcessAddress(pchar('add')); &nbsp;//GetProcessAddress要有两个参数才行呀
 
你该是<br>type f = function(x,y:integer):integer;stdcall;<br>然后<br>var<br>&nbsp; f1: f;<br>然后<br>&nbsp; @f1 := GetprocessAddress()
 
没治了,我把我测试dll的程序贴上来<br><br>unit main;<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; f = function(x,y:integer):integer;stdcall;<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; Edit2: TEdit;<br>&nbsp; &nbsp; Edit3: TEdit;<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Edit5: TEdit;<br>&nbsp; &nbsp; Edit4: TEdit;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; OpenDialog1: TOpenDialog;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; h: Hwnd;<br>&nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; fn:string;<br>&nbsp; f1:f;<br>&nbsp; x,y,z:integer;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; x:=strtoint(edit1.Text);<br>&nbsp; y:=strtoint(edit2.Text);<br>&nbsp; f1:=nil;<br>&nbsp; h:=loadlibrary(pchar(fn));<br>&nbsp; if h&lt;&gt;0 then<br>&nbsp; begin<br>&nbsp; &nbsp;showmessage('成功挂上!');<br>&nbsp; &nbsp;@f1:=GetProcAddress(h,PChar(edit5.Text));<br>&nbsp; &nbsp;end else<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp;messagebox(handle,'文件未找到!','NOTICE',MB_OK);<br>&nbsp; &nbsp;exit;<br>&nbsp; &nbsp;end;<br>&nbsp; if assigned(f1) then<br>&nbsp; begin<br>&nbsp; z:=f1(x,y);<br>&nbsp; edit3.Text:=inttostr(z);<br>&nbsp; &nbsp;end;<br>&nbsp; &nbsp;freelibrary(h);<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>&nbsp;if opendialog1.Execute then<br>&nbsp; fn:=opendialog1.FileName;<br>&nbsp; edit4.Text:=fn;<br>end;<br><br>end.
 
注意大小写<br>dll的声明的过程名的大小写敏感<br>
 
sorry!<br>一粗心,dll文件的函数名给弄反了,程序是对的,抱歉!<br>感谢薄荷兄的解答!
 
把lib<br>的原码贴出
 
后退
顶部