DLL高手请进!!!(在线等答案) ( 积分: 100 )

  • 主题发起人 主题发起人 rich_l
  • 开始时间 开始时间
R

rich_l

Unregistered / Unconfirmed
GUEST, unregistred user!
我用dll文件进行socket传送,但是在引用函数后,总是无法获值,并且都提示access violation at address…………,如何解决??
代码如下:

//If_for_bill.dpr
library Ifforbill;

{ 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,
umain in 'umain.pas' {Form1};

{$R *.res}
exports getbill;
begin
end.



//umain.pas
unit umain;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp;

type
TSend = Record
phone : array [0..30] of char;
end;
TRecv = Record
phone : array [0..1000] of char;
end;

TForm1 = class(TForm)
ClientSocket1: TClientSocket;
private
{ Private declarations }
public
{ Public declarations }
end;
function GetBill(Handle:THandle;userPhone:string):integer; cdecl;
function Extrstring(str:string):integer;
var
Form1: TForm1;
sendstr:Tsend;
revsendstr:TRecv;
implementation

{$R *.dfm}
function GetBill(Handle:THandle;userPhone:string):integer;
var
strmsg:string;
pStream:TWinSocketStream;
buf,revbuf:array of byte;
Intnumber:integer;
revstr:string;
Form1:Tform1;
begin
//查询话费
randomize;
Intnumber := random(10000);
Application.Handle := Handle;
result := -1;
Form1:=TForm1.Create(Application);
with Form1 do
begin
try
with ClientSocket1 do
begin
close;
address := '10.236.8.24';
clientType := ctBlocking;
if copy(userPhone,1,3) = '133' then
begin
Port := 14447;
strMsg := '1~7112~' + userPhone + '~' + inttostr(intNumber) + '~001~~#';
end
else
begin
Port := 14441;
strMsg := '1~1112~' + userPhone + '~' + inttostr(intNumber) + '~001~~#';
end;
Open;
fillchar(sendstr.phone,sizeof(sendstr.phone),0);
fillchar(buf,sizeof(buf),0);
copymemory(@sendstr.phone[0],@strMsg[1],length(strMsg));

copymemory(@buf,@sendstr.phone,sizeof(buf));
//copymemory(@buf[0],@sendstr.phone[0],sizeof(buf));
try
pStream := TWinSocketStream.Create(Socket,100);
pStream.Write(buf,sizeof(buf));
if pStream.WaitForData(1000) then
begin
setlength(revbuf,1000);
fillchar(revbuf,sizeof(revbuf),0);

if pStream.Read(revbuf,1000) = 0 then
begin
close;
result := -1;
end;
//pStream.Free;
fillchar(revsendstr.phone,sizeof(revsendstr.phone),0);
//fillchar(sendstr.phone,sizeof(sendstr.phone),0);
copymemory(@revsendstr,@revbuf,sizeof(revsendstr));
//copymemory(@revstr[1],@revsendstr.phone[0],sizeof(revsendstr));
revstr := StrPas(revsendstr.phone);
result := Extrstring(revstr);
end
else
begin
result := -100;
end;
socket.Close;
finally
free;
end;

end;
//ShowModal;
finally
//result := -100;
form1.Free;
end;
end;
end;
function Extrstring(str:string):integer;
var
strTemp,strTemp1,strTemp2,strTemp3,strTemp4,strTemp5,strTemp6,strTemp7,strTemp8:string;
begin
//命令
strtemp := str;
strtemp1 := copy(strtemp,1,pos('~',strtemp)-1);
//命令2
strtemp := copy(strtemp,pos('~',strtemp)+1,length(strtemp) - pos('~',strtemp));
strtemp2 := copy(strtemp,1,pos('~',strtemp)-1);

//手机号码
strtemp := copy(strtemp,pos('~',strtemp)+1,length(strtemp) - pos('~',strtemp));
strtemp3 := copy(strtemp,1,pos('~',strtemp)-1);

//空
strtemp := copy(strtemp,pos('~',strtemp)+1,length(strtemp) - pos('~',strtemp));
strtemp4 := copy(strtemp,1,pos('~',strtemp)-1);

//可能是序号
strtemp := copy(strtemp,pos('~',strtemp)+1,length(strtemp) - pos('~',strtemp));
strtemp5 := copy(strtemp,1,pos('~',strtemp)-1);

//空
strtemp := copy(strtemp,pos('~',strtemp)+1,length(strtemp) - pos('~',strtemp));
strtemp6 := copy(strtemp,1,pos('~',strtemp)-1);

//话费字符
strtemp := copy(strtemp,pos('~',strtemp)+1,length(strtemp) - pos('~',strtemp));
strtemp7 := copy(strtemp,1,pos('~',strtemp)-1);


//分解话费字符
strtemp := strtemp7;

strtemp := copy(strtemp,pos('!',strtemp)+1,length(strtemp) - pos('!',strtemp));

strtemp := copy(strtemp,pos('!',strtemp)+1,length(strtemp) - pos('!',strtemp));

strtemp := copy(strtemp,pos('!',strtemp)+1,length(strtemp) - pos('!',strtemp));

strtemp := copy(strtemp,pos('!',strtemp)+1,length(strtemp) - pos('!',strtemp));
strtemp := copy(strtemp,1,pos('!',strtemp)-1);
if strtemp = '' then
result := -100
else
result := strtoint(strtemp);

end;


end.

 
我用dll文件进行socket传送,但是在引用函数后,总是无法获值,并且都提示access violation at address…………,如何解决??
代码如下:

//If_for_bill.dpr
library Ifforbill;

{ 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,
umain in 'umain.pas' {Form1};

{$R *.res}
exports getbill;
begin
end.



//umain.pas
unit umain;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp;

type
TSend = Record
phone : array [0..30] of char;
end;
TRecv = Record
phone : array [0..1000] of char;
end;

TForm1 = class(TForm)
ClientSocket1: TClientSocket;
private
{ Private declarations }
public
{ Public declarations }
end;
function GetBill(Handle:THandle;userPhone:string):integer; cdecl;
function Extrstring(str:string):integer;
var
Form1: TForm1;
sendstr:Tsend;
revsendstr:TRecv;
implementation

{$R *.dfm}
function GetBill(Handle:THandle;userPhone:string):integer;
var
strmsg:string;
pStream:TWinSocketStream;
buf,revbuf:array of byte;
Intnumber:integer;
revstr:string;
Form1:Tform1;
begin
//查询话费
randomize;
Intnumber := random(10000);
Application.Handle := Handle;
result := -1;
Form1:=TForm1.Create(Application);
with Form1 do
begin
try
with ClientSocket1 do
begin
close;
address := '10.236.8.24';
clientType := ctBlocking;
if copy(userPhone,1,3) = '133' then
begin
Port := 14447;
strMsg := '1~7112~' + userPhone + '~' + inttostr(intNumber) + '~001~~#';
end
else
begin
Port := 14441;
strMsg := '1~1112~' + userPhone + '~' + inttostr(intNumber) + '~001~~#';
end;
Open;
fillchar(sendstr.phone,sizeof(sendstr.phone),0);
fillchar(buf,sizeof(buf),0);
copymemory(@sendstr.phone[0],@strMsg[1],length(strMsg));

copymemory(@buf,@sendstr.phone,sizeof(buf));
//copymemory(@buf[0],@sendstr.phone[0],sizeof(buf));
try
pStream := TWinSocketStream.Create(Socket,100);
pStream.Write(buf,sizeof(buf));
if pStream.WaitForData(1000) then
begin
setlength(revbuf,1000);
fillchar(revbuf,sizeof(revbuf),0);

if pStream.Read(revbuf,1000) = 0 then
begin
close;
result := -1;
end;
//pStream.Free;
fillchar(revsendstr.phone,sizeof(revsendstr.phone),0);
//fillchar(sendstr.phone,sizeof(sendstr.phone),0);
copymemory(@revsendstr,@revbuf,sizeof(revsendstr));
//copymemory(@revstr[1],@revsendstr.phone[0],sizeof(revsendstr));
revstr := StrPas(revsendstr.phone);
result := Extrstring(revstr);
end
else
begin
result := -100;
end;
socket.Close;
finally
free;
end;

end;
//ShowModal;
finally
//result := -100;
form1.Free;
end;
end;
end;
function Extrstring(str:string):integer;
var
strTemp,strTemp1,strTemp2,strTemp3,strTemp4,strTemp5,strTemp6,strTemp7,strTemp8:string;
begin
//命令
strtemp := str;
strtemp1 := copy(strtemp,1,pos('~',strtemp)-1);
//命令2
strtemp := copy(strtemp,pos('~',strtemp)+1,length(strtemp) - pos('~',strtemp));
strtemp2 := copy(strtemp,1,pos('~',strtemp)-1);

//手机号码
strtemp := copy(strtemp,pos('~',strtemp)+1,length(strtemp) - pos('~',strtemp));
strtemp3 := copy(strtemp,1,pos('~',strtemp)-1);

//空
strtemp := copy(strtemp,pos('~',strtemp)+1,length(strtemp) - pos('~',strtemp));
strtemp4 := copy(strtemp,1,pos('~',strtemp)-1);

//可能是序号
strtemp := copy(strtemp,pos('~',strtemp)+1,length(strtemp) - pos('~',strtemp));
strtemp5 := copy(strtemp,1,pos('~',strtemp)-1);

//空
strtemp := copy(strtemp,pos('~',strtemp)+1,length(strtemp) - pos('~',strtemp));
strtemp6 := copy(strtemp,1,pos('~',strtemp)-1);

//话费字符
strtemp := copy(strtemp,pos('~',strtemp)+1,length(strtemp) - pos('~',strtemp));
strtemp7 := copy(strtemp,1,pos('~',strtemp)-1);


//分解话费字符
strtemp := strtemp7;

strtemp := copy(strtemp,pos('!',strtemp)+1,length(strtemp) - pos('!',strtemp));

strtemp := copy(strtemp,pos('!',strtemp)+1,length(strtemp) - pos('!',strtemp));

strtemp := copy(strtemp,pos('!',strtemp)+1,length(strtemp) - pos('!',strtemp));

strtemp := copy(strtemp,pos('!',strtemp)+1,length(strtemp) - pos('!',strtemp));
strtemp := copy(strtemp,1,pos('!',strtemp)-1);
if strtemp = '' then
result := -100
else
result := strtoint(strtemp);

end;


end.

 
function GetBill(Handle:THandle;userPhone:string):integer; stdcall;


The cdecl convention is useful when you call functions from DLLs written in C or C++, while stdcall and safecall are used for Windows API calls. The safecall convention must be used for declaring dual-interface methods. The pascal convention is maintained for backward compatibility.

 
还是不行呀
 
access violation at address……
内存地址错,检查一下看有哪个对象还没有创建就使用了,或者已经消亡了还在使用。
 
[red]Form1:=TForm1.Create(Application);
这句话有错!在dll中并不存在Application对象。改成如下:
Form1:=TForm1.Create(nil);

还有在函数导出时一定要注意大小写,一定要与dll中导出的函数名大小写一致。[/red]
 
动态库函数最好不要用string数据类型!如果一定要用的话,那么使用这个动态库的应用就要引用sharemem单元,和borlandmem.dll!
 
还是不行 如何引用borlandmem.dll??
 
在向导生成的文件部分已经详细说明了关于DLL内存管理的问题。
在DLL/EXE的dpr文件当中uses的第一个单元必须是ShareMem才行

{...using PChar or ShortString parameters. }
uses
ShareMem,
SysUtils,
Classes,
umain in 'umain.pas' {Form1};
 
错误1:function GetBill(Handle:THandle;userPhone:string):integer; cdecl;
改为:function GetBill(Handle:THandle;userPhone:pchar):integer; stdcall;

错误2:Form1:=TForm1.Create(Application);
???:看你程序的意思,似乎是要代一个handle进去,是不是想在工程中引用dll的窗口?
 
这样是可以的了,我删去了handle:THandle,因为我实在看不出它有什么用。
不知道为什么带上handle:Thandle以后,退出DLL时连调用的程序都会一起退出了。
这么写umain.pas不太好,应该把调用窗口的函数写在If_for_bill.dpr更好一些,当然还是要看你的具体应用。

//umain.pas
unit umain;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp;

type
TSend = Record
phone : array [0..30] of char;
end;
TRecv = Record
phone : array [0..1000] of char;
end;

TForm1 = class(TForm)
ClientSocket1: TClientSocket;
private
{ Private declarations }
public
{ Public declarations }
end;
function GetBill(userPhone:pchar):integer; stdcall;
function Extrstring(str:string):integer;
var
Form1: TForm1;
sendstr:Tsend;
revsendstr:TRecv;
implementation

{$R *.dfm}
function GetBill(userPhone:pchar):integer;stdcall;
var
strmsg:string;
pStream:TWinSocketStream;
buf,revbuf:array of byte;
Intnumber:integer;
revstr:string;
Formx:^Tform1;
begin

//查询话费
randomize;
Intnumber := random(10000);
//Application.Handle := Handle;
//result := -1;


new(Formx);
Formx^ := Tform1.Create(nil);
//Form1:=TForm1.Create(Application);

with Formx^ do
begin
try
with ClientSocket1 do
begin
close;
address := '10.236.8.24';
clientType := ctBlocking;
if copy(userPhone,1,3) = '133' then
begin
Port := 14447;
strMsg := '1~7112~' + userPhone + '~' + inttostr(intNumber) + '~001~~#';
end
else
begin
Port := 14441;
strMsg := '1~1112~' + userPhone + '~' + inttostr(intNumber) + '~001~~#';
end;
Open;

fillchar(sendstr.phone,sizeof(sendstr.phone),0);
fillchar(buf,sizeof(buf),0);
copymemory(@sendstr.phone[0],@strMsg[1],length(strMsg));

copymemory(@buf,@sendstr.phone,sizeof(buf));
//copymemory(@buf[0],@sendstr.phone[0],sizeof(buf));
try
pStream := TWinSocketStream.Create(Socket,100);
pStream.Write(buf,sizeof(buf));
if pStream.WaitForData(1000) then
begin
setlength(revbuf,1000);
fillchar(revbuf,sizeof(revbuf),0);

if pStream.Read(revbuf,1000) = 0 then
begin
close;
result := -1;
end;
//pStream.Free;
fillchar(revsendstr.phone,sizeof(revsendstr.phone),0);
//fillchar(sendstr.phone,sizeof(sendstr.phone),0);
copymemory(@revsendstr,@revbuf,sizeof(revsendstr));
//copymemory(@revstr[1],@revsendstr.phone[0],sizeof(revsendstr));
revstr := StrPas(revsendstr.phone);
result := Extrstring(revstr);
end
else
begin
result := -100;
end;
socket.Close;
finally
free;
end;
end;
// ShowModal;
finally
formx^.free;
// Formx^.showmodal;
end;
end;
end;
function Extrstring(str:string):integer;
var
strTemp,strTemp1,strTemp2,strTemp3,strTemp4,strTemp5,strTemp6,strTemp7:string;
begin
//命令
strtemp := str;
strtemp1 := copy(strtemp,1,pos('~',strtemp)-1);
//命令2
strtemp := copy(strtemp,pos('~',strtemp)+1,length(strtemp) - pos('~',strtemp));
strtemp2 := copy(strtemp,1,pos('~',strtemp)-1);

//手机号码
strtemp := copy(strtemp,pos('~',strtemp)+1,length(strtemp) - pos('~',strtemp));
strtemp3 := copy(strtemp,1,pos('~',strtemp)-1);

//空
strtemp := copy(strtemp,pos('~',strtemp)+1,length(strtemp) - pos('~',strtemp));
strtemp4 := copy(strtemp,1,pos('~',strtemp)-1);

//可能是序号
strtemp := copy(strtemp,pos('~',strtemp)+1,length(strtemp) - pos('~',strtemp));
strtemp5 := copy(strtemp,1,pos('~',strtemp)-1);

//空
strtemp := copy(strtemp,pos('~',strtemp)+1,length(strtemp) - pos('~',strtemp));
strtemp6 := copy(strtemp,1,pos('~',strtemp)-1);

//话费字符
strtemp := copy(strtemp,pos('~',strtemp)+1,length(strtemp) - pos('~',strtemp));
strtemp7 := copy(strtemp,1,pos('~',strtemp)-1);


//分解话费字符
strtemp := strtemp7;

strtemp := copy(strtemp,pos('!',strtemp)+1,length(strtemp) - pos('!',strtemp));

strtemp := copy(strtemp,pos('!',strtemp)+1,length(strtemp) - pos('!',strtemp));

strtemp := copy(strtemp,pos('!',strtemp)+1,length(strtemp) - pos('!',strtemp));

strtemp := copy(strtemp,pos('!',strtemp)+1,length(strtemp) - pos('!',strtemp));
strtemp := copy(strtemp,1,pos('!',strtemp)-1);
if strtemp = '' then
result := -100
else
result := strtoint(strtemp);

end;


end.


 
参数用PCHAR类型的
 
zjan521,已经按你的提示改了,还是不行,总是在释放form的时候提示错误
 
将整个文件发过来,调调看
46524223
 
感谢各位 问题解决了
 
后退
顶部