小弟正在学习delphi的dll文件的编写,把以下一个小函数放在mytools.dll中
然后在程序中测试调用的时候能得到结果,可是也得到一个错误提示"Invalid pointer operation".请各位能帮帮我.
//IntToLZStr小函数
function IntToLZStr(Value:integer;Digits:byte) : string;stdcall;
var
Res : string;
begin
Res:=IntToStr(value);
while Length(Res)<digits do
Insert('0',res,1);
IntToLZStr:=Res;
end;测试调用mytools.dll中的IntToLZStr函数的代码
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, printers, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}function IntToLZStr(Value:integer;Digits:byte) : string;stdcall; external 'mytools.dll';procedure TForm1.Button1Click(Sender: TObject);
var
str:string;
begin
str:= trim(edit2.Text);
edit1.Text:=IntToLZStr(strtoint(str),10);
end;en
然后在程序中测试调用的时候能得到结果,可是也得到一个错误提示"Invalid pointer operation".请各位能帮帮我.
//IntToLZStr小函数
function IntToLZStr(Value:integer;Digits:byte) : string;stdcall;
var
Res : string;
begin
Res:=IntToStr(value);
while Length(Res)<digits do
Insert('0',res,1);
IntToLZStr:=Res;
end;测试调用mytools.dll中的IntToLZStr函数的代码
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, printers, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}function IntToLZStr(Value:integer;Digits:byte) : string;stdcall; external 'mytools.dll';procedure TForm1.Button1Click(Sender: TObject);
var
str:string;
begin
str:= trim(edit2.Text);
edit1.Text:=IntToLZStr(strtoint(str),10);
end;en
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;function IntToLZStr(Value:integer;Digits:byte) : PChar; stdcall; export;
var
Res : String;
begin
Res:=IntToStr(value);
while Length(Res)<digits do
Insert('0',res,1);
IntToLZStr:=PChar(Res);
end;exports
IntToLZStr index 1 name 'IntToLZStr' resident;begin
end.
-------------------------------------------------------2、Exe:unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, printers, StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}function IntToLZStr(Value:integer;Digits:byte) : PChar; stdcall; external 'MyDll.dll';//Dll名字我改了,你再改回你习惯的名字,全改procedure TForm1.Button1Click(Sender: TObject);
var
str:string;
begin
str:= trim(edit2.Text);
edit1.Text:=IntToLZStr(strtoint(str),10);
end;end.