一个Delphi的简单DLL代码问题
测试调用程序部分:implementationtypesay=Function(Str:String):String;Stdcall;varTh:THandle;Ts:S...
测试调用程序部分:
implementation
type
say = Function (Str:String):String;Stdcall;
var
Th:THandle;
Ts:Say;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit1.Text :=Ts(Edit1.Text);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
FreeLibrary(Th);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Th:=LoadLibrary('TSay.dll');
if TH=0 then Exit;
@Ts:=GetProcAddress(Th,'Tsay');
if @Ts=nil then Exit;
end;
Dll使用Unit文件:
unit Unit1;
interface
Uses Vcl.Dialogs,System.StrUtils;
Function Say(Str:String):String;Stdcall;
implementation
Function Say(Str:String):String;Stdcall;
var
s:String;
s1:Char;
begin
s:=Str;
s1:=s[1];
IF Pos(s,'5026500')>0 Then
Str:='正确'
Else IF pos(s,'885231')>0 then
Result:='正确';
end;
end.
在Library部分是这样的:
uses
Unit1 In 'Unit1.pas',
System.SysUtils,
System.Classes,
Vcl.Dialogs,
System.StrUtils;
{$R *.res}
Exports Say name 'Tsay';
begin
end.
错误的现象是因为:调用DLL函数的时候没有错误,调试的时候成功的载入并且卸载了,但是DLL的函数的返回值为0,求人帮我看看,谢谢。 展开
implementation
type
say = Function (Str:String):String;Stdcall;
var
Th:THandle;
Ts:Say;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit1.Text :=Ts(Edit1.Text);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
FreeLibrary(Th);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Th:=LoadLibrary('TSay.dll');
if TH=0 then Exit;
@Ts:=GetProcAddress(Th,'Tsay');
if @Ts=nil then Exit;
end;
Dll使用Unit文件:
unit Unit1;
interface
Uses Vcl.Dialogs,System.StrUtils;
Function Say(Str:String):String;Stdcall;
implementation
Function Say(Str:String):String;Stdcall;
var
s:String;
s1:Char;
begin
s:=Str;
s1:=s[1];
IF Pos(s,'5026500')>0 Then
Str:='正确'
Else IF pos(s,'885231')>0 then
Result:='正确';
end;
end.
在Library部分是这样的:
uses
Unit1 In 'Unit1.pas',
System.SysUtils,
System.Classes,
Vcl.Dialogs,
System.StrUtils;
{$R *.res}
Exports Say name 'Tsay';
begin
end.
错误的现象是因为:调用DLL函数的时候没有错误,调试的时候成功的载入并且卸载了,但是DLL的函数的返回值为0,求人帮我看看,谢谢。 展开
1个回答
2013-04-15 · 知道合伙人数码行家
关注
展开全部
你好,你的调用并没有问题。
测试部分
unit Unitform;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
type
say = Function (Str:String):String;Stdcall;
var
Th:THandle;
Ts:Say;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit1.Text :=Ts(Edit1.Text);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
FreeLibrary(Th);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Th:=LoadLibrary('TSay.dll');
if TH=0 then Exit;
@Ts:=GetProcAddress(Th,'Tsay');
if @Ts=nil then Exit;
end;
end.
dll部分
library Tsay;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
SysUtils,
Classes,
Unit1 in 'Unit1.pas';
{$R *.res}
Exports Say name 'Tsay';
begin
end.
unit Unit1;
interface
Uses Dialogs,StrUtils;
Function Say(Str:String):String;Stdcall;
implementation
Function Say(Str:String):String;Stdcall;
var
s:String;
s1:Char;
begin
s:=Str;
s1:=s[1];
IF Pos(s,'5026500')>0 Then
Str:='正确'
Else IF pos(s,'885231')>0 then
Result:='正确';
end;
end.
end.
测试情况:(在字符串不属于5026500的情况下,只要字符串属于885231就会返回正确)
IF Pos(s,'5026500')>0 Then
Str:='正确'
Else IF pos(s,'885231')>0 then
Result:='正确';
result才是返回,注意了!
测试1:
测试前
测试后(‘1’不属于‘5026500’,然后进行‘1’属于‘885231’判断,属于返回正确)
测试2:
测试前
测试后(‘2’不属于‘5026500’,跳出if方法,而后没有result返回,所以result返回默认值空‘’)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询