大家好,我最近写的一个程序需要在线程单元中调用一个C++编写的动态链接库。我写了单独调用这个dll的exe程序,没有问题。但是在线程单元中调用这个动态库的时候,Exe大家好,我最近编写的一个程序需要在Delphi的线程单元中调用一个C++编写的动态链接库,在exe程序中我已经调用了这个动态库没有问题,但是在线程单元里调用这个动态库在Execute过程退出的时候出现一个access violation错误。好像是某个内存地址不可读。下面是我的线程单元的代码,请大家看一下,我第一次写这样的程序,写的可能有问题,请帮指点一下。谢谢。
procedure THLoginThread.Execute;
type
//显示调用链接库
tthauth_authenticate2userinfo_pin = function(serverip: PChar; serverport: Integer; appkey: PChar; user: PChar; pin: PChar; userinfo: p_userinfo): Integer; stdcall; //登录接口
var
TpLogin: TFarProc;
ILoginResult: Integer;
pUserinfo: p_userinfo;
Test: tthauth_authenticate2userinfo_pin;
begin
{ Place thread code here }
EnterCriticalSection(OraCS); //进入临界区
FreeOnTerminate := True;
try
Th := LoadLibrary('thauth.dll'); //加载动态链接库
TpLogin := GetProcAddress(Th, 'thauth_authenticate2userinfo_pin');
New(pUserinfo);
@Test := TpLogin;
if @Test <> nil then
//断点调试过,就是下面这句话执行之后在退出这个过程的时候有问题,下面这句话执行通过了,返回值也对。
ILoginResult := Test(PChar('166.111.4.125'), 3335, PChar('ICCARD:abcd1234'), PChar(Tid), PChar(Tpwd), pUserinfo);
THLoginCheckResult := pUserinfo^.code;
Dispose(pUserinfo);
TpLogin := nil;
FreeLibrary(Th);
except
THLoginCheckResult := -1;
end;
Synchronize(GiveResult);
LeaveCriticalSection(OraCS); //退出临界区
end;
procedure THLoginThread.Execute;
type
//显示调用链接库
tthauth_authenticate2userinfo_pin = function(serverip: PChar; serverport: Integer; appkey: PChar; user: PChar; pin: PChar; userinfo: p_userinfo): Integer; stdcall; //登录接口
var
TpLogin: TFarProc;
ILoginResult: Integer;
pUserinfo: p_userinfo;
Test: tthauth_authenticate2userinfo_pin;
begin
{ Place thread code here }
EnterCriticalSection(OraCS); //进入临界区
FreeOnTerminate := True;
try
Th := LoadLibrary('thauth.dll'); //加载动态链接库
TpLogin := GetProcAddress(Th, 'thauth_authenticate2userinfo_pin');
New(pUserinfo);
@Test := TpLogin;
if @Test <> nil then
//断点调试过,就是下面这句话执行之后在退出这个过程的时候有问题,下面这句话执行通过了,返回值也对。
ILoginResult := Test(PChar('166.111.4.125'), 3335, PChar('ICCARD:abcd1234'), PChar(Tid), PChar(Tpwd), pUserinfo);
THLoginCheckResult := pUserinfo^.code;
Dispose(pUserinfo);
TpLogin := nil;
FreeLibrary(Th);
except
THLoginCheckResult := -1;
end;
Synchronize(GiveResult);
LeaveCriticalSection(OraCS); //退出临界区
end;
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货