动态链接库的动态调用(200分)

  • 主题发起人 主题发起人 pandababy
  • 开始时间 开始时间
P

pandababy

Unregistered / Unconfirmed
GUEST, unregistred user!
我现在是静态调用成功,<br>但动态调用时sumnum出现错误,AddNumber不出现错五但好象没有执行<br>//以下为定义DLL<br>library Project1;<br><br>{ Important note about DLL memory management: ShareMem must be the<br>&nbsp; first unit in your library's USES clause AND your project's (select<br>&nbsp; Project-View Source) USES clause if your DLL exports any procedures or<br>&nbsp; functions that pass strings as parameters or function results. This<br>&nbsp; applies to all strings passed to and from your DLL--even those that<br>&nbsp; are nested in records and classes. ShareMem is the interface unit to<br>&nbsp; the BORLNDMM.DLL shared memory manager, which must be deployed along<br>&nbsp; with your DLL. To avoid using BORLNDMM.DLL, pass string information<br>&nbsp; using PChar or ShortString parameters. }<br><br>uses<br>&nbsp; SysUtils,<br>&nbsp; Classes;<br><br>{$R *.res}<br>function AddNumber(Num1,Num2:integer):integer;stdcall; //定义求和函数<br>begin<br>&nbsp; result:=Num1+Num2;<br>end;<br><br>procedure sumnum(N1,N2:integer;var sumn:integer);stdcall;<br>begin<br>&nbsp; sumn:=N1+N2;<br>end;<br><br>exports<br><br>&nbsp; AddNumber , sumnum;<br>begin<br>end.<br><br><br>//以下为调用<br>unit Unit2;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls;<br><br>type<br>TAddNum = function(Num1,Num2:integer):integer;<br>TsumNum = function(Num1,Num2:integer;var sumn:integer):integer;<br>THandle = Integer;<br><br>&nbsp; TForm2 = 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; Edit4: TEdit;<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; Form2: TForm2;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm2.Button1Click(Sender: TObject);<br>var<br>&nbsp; Handle: THandle;<br>&nbsp; AddNum1: TAddNum;<br>&nbsp; sumnum1: TsumNum;<br>&nbsp; Number1,Number2:integer;<br>&nbsp; Sum:integer;<br>begin<br>&nbsp; sum:=0;<br>&nbsp; Handle := LoadLibrary('Project1.dll');<br>&nbsp; if Handle &lt;&gt; 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; @AddNum1 := GetProcAddress(Handle, 'AddNumber');<br>&nbsp; &nbsp; @sumnum1 := GetProcAddress(Handle, 'sumnum');<br>&nbsp; &nbsp; if (@AddNum1 &lt;&gt; nil) and (@sumnum1&lt;&gt;nil) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Number1:=strtoint(Edit1.Text);<br>&nbsp; &nbsp; &nbsp; Number2:=strtoint(Edit2.Text);<br>&nbsp; &nbsp; &nbsp; sum:=AddNum1(Number1,Number2); //调用求和函数计算结果<br>&nbsp; &nbsp; &nbsp; Edit3.Text:=inttostr(Sum);<br>&nbsp; &nbsp; &nbsp; sumnum1(Number1,Number2,Sum);<br>&nbsp; &nbsp; &nbsp; Edit4.Text:=inttostr(Sum);<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>&nbsp; FreeLibrary(Handle); &nbsp;<br>end;<br><br>end.
 
调用的声明也要加上调用协议的 <br>TAddNum = function(Num1,Num2:integer):integer; stdcall;<br>TsumNum = function(Num1,Num2:integer;var sumn:integer):integer; stdcall;<br>
 
呵呵,就这么简单,200元是你的了
 
TAddNum = function(Num1,Num2:integer):integer; stdcall;<br>TsumNum = procedure(Num1,Num2:integer;var sumn:integer); stdcall;
 

Similar threads

后退
顶部