DLL中调用DLL失败!100分解决!(100分)

  • 主题发起人 主题发起人 lining1980
  • 开始时间 开始时间
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> &nbsp;SysUtils,<br> &nbsp;Classes;<br><br>{$R *.res}<br>function add(x,y:Integer):Integer;stdcall;<br>begin<br> &nbsp;Result:=x+y;<br>end;<br><br>exports<br> &nbsp;add;<br>begin<br>end.<br>//////////<br><br>ProPlus.dll代码:<br>///////////////<br>library ProPlus;<br>uses<br> &nbsp;Windows,<br> &nbsp;SysUtils,<br> &nbsp;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> &nbsp;Result:=add(x,y);<br>end;<br><br>exports<br> &nbsp;plus;<br>begin<br>end.<br>///////////////<br><br><br>ProPlus2.dll的代码:<br>///////////<br>library ProPlus2;<br>uses<br> &nbsp;Windows,<br> &nbsp;SysUtils,<br> &nbsp;Classes;<br><br>type<br> &nbsp;Tadd=function(x,y:Integer):Integer;stdcall;<br> &nbsp;ELoadLibraryFailed=class(Exception);<br>{$R *.res}<br>function plus(x,y:Integer):Integer;stdcall;<br>var<br> &nbsp;AHandle:THandle;<br> &nbsp;add:Tadd;<br>begin<br> &nbsp;AHandle:=LoadLibrary('ProAdd.dll');<br> &nbsp;try<br> &nbsp; &nbsp;if AHandle = 0 then Result:=0;<br> &nbsp; &nbsp;@add:= GetProcAddress(AHandle,'add');<br> &nbsp; &nbsp;if Assigned(@add) then<br> &nbsp; &nbsp; &nbsp;Result:=add(x,y)<br> &nbsp; &nbsp;else <br> &nbsp; &nbsp; &nbsp;Result:= 1;<br> &nbsp;finally<br> &nbsp; &nbsp;FreeLibrary(AHandle);<br> &nbsp;end;<br>end;<br><br>exports<br> &nbsp;plus;<br>begin<br>end.<br>////////////<br><br>主程序动态调用ProPlus.dll代码:<br>//////////////<br>unit uPlus;<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;Tplus=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;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>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;AHandle:THandle;<br> &nbsp;plus:Tplus;<br>begin<br> &nbsp;AHandle:=LoadLibrary('ProPlus.dll');<br> &nbsp;try<br> &nbsp; &nbsp;if AHandle=0 then <br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;ShowMessage('failed');<br> &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;@plus:= GetProcAddress(AHandle,'plus');<br> &nbsp; &nbsp;if Assigned(@plus) then<br> &nbsp; &nbsp; &nbsp;Edit3.Text:=IntToStr(plus(StrToInt(Edit1.Text),StrToInt(Edit2.Text)));<br> &nbsp;finally<br> &nbsp; &nbsp;FreeLibrary(AHandle);<br> &nbsp;end;<br>end;<br><br>end.<br>/////////////<br><br>主程序动态调用ProPlus2.dll代码:<br>////////////////////<br>unit uPlus;<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;Tplus=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;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>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;AHandle:THandle;<br> &nbsp;plus:Tplus;<br>begin<br> &nbsp;AHandle:=LoadLibrary('ProPlus2.dll');<br> &nbsp;try<br> &nbsp; &nbsp;if AHandle=0 then <br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;ShowMessage('failed');<br> &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;@plus:= GetProcAddress(AHandle,'plus');<br> &nbsp; &nbsp;if Assigned(@plus) then<br> &nbsp; &nbsp; &nbsp;Edit3.Text:=IntToStr(plus(StrToInt(Edit1.Text),StrToInt(Edit2.Text)));<br> &nbsp;finally<br> &nbsp; &nbsp;FreeLibrary(AHandle);<br> &nbsp;end;<br>end;<br><br>end.<br>///////////////////
 
[Warning] ProPlus.dpr(28):Return value of function 'plus' might be undefined。怎么回事?<br><br>没有返回值, &nbsp;Result := ??;<br><br><br>Edit3.Text:=IntToStr(plus(StrToInt(Edit1.Text),StrToInt(Edit2.Text)));<br><br><br>plus 函数没有返回值
 
谢谢cnbell帮我回答了第一个问题!我修改了一下问题,能帮我回答一下第2个和第3个吗?
 
function LoadLibrary(lpLibFileName: PChar): HMODULE; stdcall;<br><br><br>参数应该是 PChar 类型<br><br><br>改成<br>AHandle:=LoadLibrary(Pchar('ProPlus.dll'));
 
GetProcAddress(AHandle,'plus');<br><br>也应该是 Pchar('plus')
 
怎么不结帖啊. &nbsp;<br><br>还是&quot;处&quot;啊.
 
这个绝对没有任何问题:<br>GetProcAddress(AHandle,'plus');
 
谢谢二位,我的机子不知出什么问题了,现在再编译以上代码,发现即使不加Pchar,程序也可以运行。谢谢,cnbell帮我解决了第一个问题,cnbell70分,LSUPER30分
 
后退
顶部