delphi 调用c++的动态链接库错误 ( 积分: 100 )

G

gutian

Unregistered / Unconfirmed
GUEST, unregistred user!
c++的声明
namespace Fish {

extern "C"{
__declspec(dllexport) bool DoSplash(char*,int FAR*, int FAR*,int FAR*)

}
delphi调用
TDoSplash=function(fname:pChar;var res,depth,full:pInteger):Boolean;stdcall;
THandle=Integer;

var
Handle:THandle;
DoSplash:TDoSplash;
res:pInteger;
depth:pInteger;
full:pInteger;
begin
Handle:=LoadLibrary('FishSdk.dll')

new(res);
new(depth);
new(full);
res^ := 0;
depth^ := 0;
full^ := 0;
if Handle<>0 then
begin
@DoSplash:=GetProcAddress(Handle,'DoSplash')
//取得DLL中函数Min1( )的地址
if (@DoSplash<>nil)then
begin
DoSplash(PChar('Setup.cfg'),res,depth,full);
end else ShowMessage('调用函数“GetProcAddress”时出错!');
Dispose(res);
Dispose(depth);
Dispose(full);
FreeLibrary(Handle)

end;
end;
执行后返回的res,depth,full出错。




}
 
TDoSplash=function(fname:pChar;var res,depth,full:pInteger):Boolean;stdcall;
改成
TDoSplash=function(fname:pChar;const res,depth,full:pInteger):Boolean;stdcall;
 
对,好似不能用VAR
 
改成const后,执行到FreeLibrary(Handle)
出现access violation at address 0012f6ef
 
奇怪,在 DoSplash(PChar('Setup.cfg'),res,depth,full);
之后加了几句之后,错误消失:
DoSplash(PChar('Setup.cfg'),res,depth,full);
mmoResult.Lines.Add(IntToStr(res^));
mmoResult.Lines.Add(IntToStr(depth^));
mmoResult.Lines.Add(IntToStr(full^));
但是,如果没有这几句语句,执行会出错
 
为什么不能用var呢?
貌似winsock
function getpeername(s: TSocket
var name: TSockAddr
var namelen: Integer): Integer
stdcall;
就用了var
 
可以用var
但你用PInteger类型,所以用VAR没有意义
 
to 41426277:有道理
但是执行到FreeLibrary(Handle)
出现access violation at address 0012f6ef
这个错误不知道是什么原因呢?
 
FreeLibrary(Handle)
后,再
Dispose(res);
Dispose(depth);
Dispose(full);
试试。
另外,你这个Handle变量还是换换名吧。别么叫。它和窗体的Handle同名。
 
奇怪,把函数声明为:
function DoSplash(fname:pChar;var Ires,Idepth,Ifull:Integer):Boolean;
然后作以下操作后
Ires := res^;
Idepth := depth^;
Ifull := full^;
就没有问题了.
 
1> C++库不是用stdcall吧

2>因为老兄用了var, 所以 res depth full该声明为integer

3>DoSplash第一个参数是指针,Pchar('Setup.cfg')是一个常量吧?


4> &quot;奇怪,把函数声明为:
function DoSplash(fname:pChar;var Ires,Idepth,Ifull:Integer):Boolean;
然后作以下操作后
Ires := res^;
Idepth := depth^;
Ifull := full^;
就没有问题了.&quot;

res^就是integer类型了,和第二条对应吧
 
TDoSplash=function(fname:pChar;var res,depth,full:pInteger):Boolean;cdecl;
 
TDoSplash=function(fname:pChar;var res,depth,full:Integer):Boolean;cdecl;
 
用VAR 和不用VAR 不一样的,一个好似是传值,一个是传址
 
多人接受答案了。
 

Similar threads

D
回复
0
查看
1K
DelphiTeacher的专栏
D
I
回复
0
查看
479
import
I
I
回复
0
查看
681
import
I
I
回复
0
查看
497
import
I
顶部