H
hqpeng
Unregistered / Unconfirmed
GUEST, unregistred user!
我在DLL中做了两个函数reverseStr和squared,其中一个函数ReverseStr
调用成功,但squared始终调用不成功.请大侠帮助找找毛病在什么地方.
源程序如下:
library Reverse;
uses
SysUtils,
Classes,
reverse01 in 'reverse01.pas',
reverse02 in 'reverse02.pas';
exports
Squared,ReverseStr;
begin
end.
unit reverse01;
interface
uses
SysUtils,
Classes;
function ReverseStr(StrToReversechar):integer;export;stdcall;
implementation
function ReverseStr(StrToReversechar):integer;
var
BFGLen:Integer;
Temp:char;
Count:Integer;
begin
BFGLen:=strLen(StrToReverse);
for count:=0 to (BfGLen div 2)-1 do
begin
temp:=strToReverse[count];
strToReverse[count]:=strToReverse[BFGLen-1-Count];
strToReverse[BFGLen-1-Count]:=Temp;
end;
ReverseStr:=0;
end;
end.
unit reverse02;
interface
uses
SysUtils,
Classes;
function Squared(va:integer):integer;export;stdcall;
implementation
function Squared(va:integer):integer;
var
va1:integer;
va2:integer;
begin
va1:=va;
va2:=va1*va1;
va:=va2;
squared:=va1;
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Edit6: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
type
TDllFunc1=function(VarStr:string):integer;stdcall;
TDllFunc2=function(varint:integer):integer;stdcall;
var
hlib:Thandle;
DllFunc1:TDllFunc1;
DllFunc2:TDllFunc2;
SouStr:string;
SouNum:integer;
begin
hlib:=LoadLibrary('Reverse.dll');
if hlib=0 then
begin
showMessage('Can not Open Dll');
exit;
end;
DllFunc1:=TDllFunc1(GetProcAddress(hlib,'ReverseStr'));
DllFunc2:=TDllFunc2(getprocaddress(hlib,'Suqared'));
if @DllFunc1=nil then
ShowMessage('function reversestr fail!')
else
begin
SouStr:=edit1.text;
Edit2.text:=intToStr(dllfunc1(SouStr));
edit3.text:=SouStr;
end;
if @DllFunc2=nil then
showMessage('function squared fail!')
else
begin
SouNum:=StrToint(edit4.Text);
edit5.Text:=inttostr(dllfunc2(sounum));
edit6.Text:=inttostr(souNum);
end;
FreeLibrary(hlib);
end;
end.
调用成功,但squared始终调用不成功.请大侠帮助找找毛病在什么地方.
源程序如下:
library Reverse;
uses
SysUtils,
Classes,
reverse01 in 'reverse01.pas',
reverse02 in 'reverse02.pas';
exports
Squared,ReverseStr;
begin
end.
unit reverse01;
interface
uses
SysUtils,
Classes;
function ReverseStr(StrToReversechar):integer;export;stdcall;
implementation
function ReverseStr(StrToReversechar):integer;
var
BFGLen:Integer;
Temp:char;
Count:Integer;
begin
BFGLen:=strLen(StrToReverse);
for count:=0 to (BfGLen div 2)-1 do
begin
temp:=strToReverse[count];
strToReverse[count]:=strToReverse[BFGLen-1-Count];
strToReverse[BFGLen-1-Count]:=Temp;
end;
ReverseStr:=0;
end;
end.
unit reverse02;
interface
uses
SysUtils,
Classes;
function Squared(va:integer):integer;export;stdcall;
implementation
function Squared(va:integer):integer;
var
va1:integer;
va2:integer;
begin
va1:=va;
va2:=va1*va1;
va:=va2;
squared:=va1;
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Edit6: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
type
TDllFunc1=function(VarStr:string):integer;stdcall;
TDllFunc2=function(varint:integer):integer;stdcall;
var
hlib:Thandle;
DllFunc1:TDllFunc1;
DllFunc2:TDllFunc2;
SouStr:string;
SouNum:integer;
begin
hlib:=LoadLibrary('Reverse.dll');
if hlib=0 then
begin
showMessage('Can not Open Dll');
exit;
end;
DllFunc1:=TDllFunc1(GetProcAddress(hlib,'ReverseStr'));
DllFunc2:=TDllFunc2(getprocaddress(hlib,'Suqared'));
if @DllFunc1=nil then
ShowMessage('function reversestr fail!')
else
begin
SouStr:=edit1.text;
Edit2.text:=intToStr(dllfunc1(SouStr));
edit3.text:=SouStr;
end;
if @DllFunc2=nil then
showMessage('function squared fail!')
else
begin
SouNum:=StrToint(edit4.Text);
edit5.Text:=inttostr(dllfunc2(sounum));
edit6.Text:=inttostr(souNum);
end;
FreeLibrary(hlib);
end;
end.