L
lining1980
Unregistered / Unconfirmed
GUEST, unregistred user!
问题有点长,请大家耐心一点看,我觉得这个问题还是挺有价值的。问题如下:<br>三个DLL文件,ProAdd.dll,ProPlus.dll和ProPlus2.dll,用ProPlus.dll静态调用ProAdd.dll,用ProPlus2.dll动态调用ProAdd.dll,然后编一个主程序分别对ProPlus.dll和ProPlus2.dll动态调用,以比较结果。<br>问题1:<br>用ProPlus2.dll动态调用ProAdd.dll时出现警告:<br>[Warning] ProPlus.dpr(28):Return value of function 'plus' might be undefined。怎么回事?<br>问题2:当主程序动态调用ProPlus.dll(ProPlus.dll静态调用ProAdd.dll)时,问题出现在主程序对ProPlus.dll的调用上,LoadLibrary('ProPlus.dll')获得的句柄为0.<br>问题3:当主程序动态调用ProPlus2.dll(ProPlus2.dll动态调用ProAdd.dll)时,问题出现在ProPlus2.dll对ProAdd.dll的调用上,LoadLibrary('ProAdd.dll')获得的句柄为0.<br><br><br>代码如下:<br><br>ProAdd.dll代码:<br>/////////////////<br>library ProAdd;<br>uses<br> SysUtils,<br> Classes;<br><br>{$R *.res}<br>function add(x,y:Integer):Integer;stdcall;<br>begin<br> Result:=x+y;<br>end;<br><br>exports<br> add;<br>begin<br>end.<br>//////////<br><br>ProPlus.dll代码:<br>///////////////<br>library ProPlus;<br>uses<br> Windows,<br> SysUtils,<br> Classes;<br><br>function add(x,y:Integer):Integer;stdcall;external 'ProAdd.dll';<br>{$R *.res}<br>function plus(x,y:Integer):Integer;stdcall;<br>begin<br> Result:=add(x,y);<br>end;<br><br>exports<br> plus;<br>begin<br>end.<br>///////////////<br><br><br>ProPlus2.dll的代码:<br>///////////<br>library ProPlus2;<br>uses<br> Windows,<br> SysUtils,<br> Classes;<br><br>type<br> Tadd=function(x,y:Integer):Integer;stdcall;<br> ELoadLibraryFailed=class(Exception);<br>{$R *.res}<br>function plus(x,y:Integer):Integer;stdcall;<br>var<br> AHandle:THandle;<br> add:Tadd;<br>begin<br> AHandle:=LoadLibrary('ProAdd.dll');<br> try<br> if AHandle = 0 then Result:=0;<br> @add:= GetProcAddress(AHandle,'add');<br> if Assigned(@add) then<br> Result:=add(x,y)<br> else <br> Result:= 1;<br> finally<br> FreeLibrary(AHandle);<br> end;<br>end;<br><br>exports<br> plus;<br>begin<br>end.<br>////////////<br><br>主程序动态调用ProPlus.dll代码:<br>//////////////<br>unit uPlus;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls;<br><br>type<br> Tplus=function(x,y:Integer):Integer;stdcall;<br> TForm1 = class(TForm)<br> Edit1: TEdit;<br> Edit2: TEdit;<br> Edit3: TEdit;<br> Button1: TButton;<br> procedure Button1Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> AHandle:THandle;<br> plus:Tplus;<br>begin<br> AHandle:=LoadLibrary('ProPlus.dll');<br> try<br> if AHandle=0 then <br> begin<br> ShowMessage('failed');<br> Exit;<br> end;<br> @plus:= GetProcAddress(AHandle,'plus');<br> if Assigned(@plus) then<br> Edit3.Text:=IntToStr(plus(StrToInt(Edit1.Text),StrToInt(Edit2.Text)));<br> finally<br> FreeLibrary(AHandle);<br> end;<br>end;<br><br>end.<br>/////////////<br><br>主程序动态调用ProPlus2.dll代码:<br>////////////////////<br>unit uPlus;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls;<br><br>type<br> Tplus=function(x,y:Integer):Integer;stdcall;<br> TForm1 = class(TForm)<br> Edit1: TEdit;<br> Edit2: TEdit;<br> Edit3: TEdit;<br> Button1: TButton;<br> procedure Button1Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> AHandle:THandle;<br> plus:Tplus;<br>begin<br> AHandle:=LoadLibrary('ProPlus2.dll');<br> try<br> if AHandle=0 then <br> begin<br> ShowMessage('failed');<br> Exit;<br> end;<br> @plus:= GetProcAddress(AHandle,'plus');<br> if Assigned(@plus) then<br> Edit3.Text:=IntToStr(plus(StrToInt(Edit1.Text),StrToInt(Edit2.Text)));<br> finally<br> FreeLibrary(AHandle);<br> end;<br>end;<br><br>end.<br>///////////////////