dll的问题,在线等待 ( 积分: 100 )

  • 主题发起人 主题发起人 缘尽
  • 开始时间 开始时间

缘尽

Unregistered / Unconfirmed
GUEST, unregistred user!
我的dll代码
library regdll;

{ 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, inifiles;

{$R *.res}

function cjwj(xrsj:string):string;stdcall;
var found:integer;
sr: TSearchRec;
myinifile:TInifile;
filename:string;
xjm,gjz,drz:string;
begin
xjm:='ssss';
gjz:='ssss';
Found := FindFirst(ExtractFilePath(Paramstr (0))+'jiu.ini', faAnyFile , sr);
if found=0 then
begin
if myinifile.ReadString(xjm,gjz,drz)='' then
begin
filename:=ExtractFilePath(paramstr(0))+'jiu.ini';
myinifile:=TInifile.Create(filename);
myinifile.writestring(xjm,gjz,xrsj);
result:=xrsj;
end
else
begin
result:=myinifile.ReadString(xjm,gjz,drz);
end;

end
else
begin
result:='';
end;
end;
function wrqjm(var rq: string):string;
var
yrq:array[1..10]of string;
zjs:array[1..11]of string;
i:integer;
fhjm:string;
s:string;
begin
for i:=1 to 10 do
begin
yrq:=copy(rq,i,1);
end;
yrq[9]:=inttostr(strtoint(yrq[9])+6);
yrq[8]:='0';
yrq[6]:=inttostr(strtoint(yrq[6])+7);
yrq[5]:='1';
yrq[3]:=inttostr(strtoint(yrq[3])+2);
yrq[2]:=inttostr(strtoint(yrq[2])+5);
yrq[1]:=inttostr(strtoint(yrq[1])+5);
//第1个随机数
for i:=1 to 11 do
begin
zjs:='';
end;
for i:=1 to 6 do
begin
randomize;
s:=inttostr(random(99));
if length(s)<2 then s:='0'+s;
zjs[1]:=zjs[1]+s;
end;
//第2个随机数
for i:=1 to 6 do
begin
randomize;
s:=inttostr(random(99));
if length(s)<2 then s:='0'+s;
zjs[2]:=zjs[2]+s;
end;
//第3个随机数
for i:=1 to 5 do
begin
randomize;
s:=inttostr(random(99));
if length(s)<2 then s:='0'+s;
zjs[3]:=zjs[3]+s;
end;
//第4个随机数
for i:=1 to 4 do
begin
randomize;
s:=inttostr(random(99));
if length(s)<2 then s:='0'+s;
zjs[4]:=zjs[4]+s;
end;
//第5个随机数
for i:=1 to 3 do
begin
randomize;
s:=inttostr(random(99));
if length(s)<2 then s:='0'+s;
zjs[5]:=zjs[5]+s;
end;
//第6个随机数
for i:=1 to 2 do
begin
randomize;
s:=inttostr(random(99));
if length(s)<2 then s:='0'+s;
zjs[6]:=zjs[6]+s;
end;
//第7个随机数
for i:=1 to 3 do
begin
randomize;
s:=inttostr(random(99));
if length(s)<2 then s:='0'+s;
zjs[7]:=zjs[7]+s;
end;
//第8个随机数
for i:=1 to 3 do
begin
randomize;
s:=inttostr(random(99));
if length(s)<2 then s:='0'+s;
zjs[8]:=zjs[8]+s;
end;

randomize;
s:=inttostr(random(9));
zjs[8]:=zjs[8]+s;
//第9个随机数
for i:=1 to 3 do
begin
randomize;
s:=inttostr(random(99));
if length(s)<2 then s:='0'+s;
zjs[9]:=zjs[9]+s;
end;
//第10个随机数
for i:=1 to 2 do
begin
randomize;
s:=inttostr(random(99));
if length(s)<2 then s:='0'+s;
zjs[10]:=zjs[10]+s;
end;
//第11个随机数
for i:=1 to 7 do
begin
randomize;
s:=inttostr(random(99));
if length(s)<2 then s:='0'+s;
zjs[11]:=zjs[11]+s;
end;
randomize;
s:=inttostr(random(9));
zjs[11]:=zjs[11]+s;
fhjm:=zjs[1]+yrq[9]+zjs[2]+yrq[6]+zjs[3]+yrq[2]+zjs[4]+yrq[4]+zjs[5]+yrq[1]+zjs[6]+yrq[5]+zjs[7]+yrq[3]+zjs[8]+yrq[7]+zjs[9]+yrq[10]+zjs[10]+yrq[8]+zjs[11];
result:=fhjm;
end;

function wrqfjm(var jmrq: string): string;
var
yrq:array[1..10]of string;
i:integer;
fhjm:string;
begin
yrq[1]:=copy(jmrq,53,1);
yrq[2]:=copy(jmrq,37,1);
yrq[3]:=copy(jmrq,65,1);
yrq[4]:=copy(jmrq,46,1);
yrq[5]:=copy(jmrq,58,1);
yrq[6]:=copy(jmrq,26,1);
yrq[7]:=copy(jmrq,73,1);
yrq[8]:=copy(jmrq,85,1);
yrq[9]:=copy(jmrq,13,1);
yrq[10]:=copy(jmrq,80,1);
yrq[9]:=inttostr(strtoint(yrq[9])-6);
yrq[8]:='-';
yrq[6]:=inttostr(strtoint(yrq[6])-7);
yrq[5]:='-';
yrq[3]:=inttostr(strtoint(yrq[3])-2);
yrq[2]:=inttostr(strtoint(yrq[2])-5);
yrq[1]:=inttostr(strtoint(yrq[1])-5);
for i:=1 to 10 do
begin
fhjm:=fhjm+yrq;
end;
result:=fhjm;
end;

exports
cjwj;
exports
wrqjm;
exports
wrqfjm;

begin
end.
我在程序中使用
function wrqjm(var rq:string):string;stdcall;
external 'regdll.dll';
procedure Tmain.Button1Click(Sender: TObject);
var
rq:string;
rqjm:string;
begin
rq:=datetostr(date);
self.Edit1.Text:=wrqjm(rq);
end;
运行是报的错误
"is not a valid integer value"
 
把dll内函数的声明改为这样:
function cjwj(xrsj:pchar):pchar;stdcall;

string类型在不同的内存管理器下要出错的。
 
改了还是不行啊
 
改后进入调试程序里了更郁闷
 
dll内只要是需要导出的函数,全部的string类型都要改为pchar的
 
你改后调试了没?
 
dll和exe之间使用string类型需要采用额外的共享内存单元,这是基本常识啊。
 
我才开始学习用啊 ,我改后还是不行哦
access violation at 0x00000000:read of address 0x00000000,
 
dll单元的前面不写着如下内容啊
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.
用google翻译如下:
重要说明关于DLL内存管理: sharemem必须首先加在您lib的USES部分和您的项目的(选择项目--查看源)用途条款,即使 你的DLL输出任何程序或功能是通过字符串作为参数或者函数结果. 这适用于所有通过串并从您的DLL --即使是那些坐落在记录和类. sharemem是接口单位到borlndmm.dll共享存储管理,必须部署连同您的DLL . 避免使用borlndmm.dll , pass string information using pchar或者shortstring参数.
 
终于出来了,我把传入的参数和返回都改成pchar就可以了,非常感谢你啊
 
另外,dll内的局部变量还是可以使用string类型的。
毕竟用string类型比较方便。
 
接受答案了.
 
后退
顶部