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.
代码如下:
//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.