这个函数在VC中的声明是:
extern "C" __declspec(dllimport) void WINAPI C_StartUP(long nAddrMsg);参数是一个回调函数的地址。回调函数声明如下:
void GetMsgFromClient(long nVer, long uin, long i, const char* pbstr, const char* sSrcIP, long nSrcPort)
在VC中的调用此函数是这样的:
C_StartUP((long)&GetMsgFromClient); 我写的调用此函数的Delphi代码
type
TJMSvr=class
.................
................
public
procedure startsvr(whenTalk:IOnClientTalk);
end;
..........
.........
procedure getMsgFromClient(nver,nUid,itype:LongInt;sMSg,sSrcIP:PChar;port:LongInt);stdcall;
procedure C_StartUP(nAddrMsg:LongInt); cdecl;external '../DLLS/P2PServerDLL.dll';implementation
procedure TJMSvr.startsvr(whenTalk:IOnClientTalk);
var
p:LongInt;
begin
..................
..................
P:=LongInt(@getMsgFromClient);
C_StartUP(p);//运行到此处,出现一个
//Project Project1.exe raised exception class EAccessViolation with message
//'Access violation at address 1007D763 in module 'P2PServerDLL.dll'. Read of address 00000004'.
//的错误
end;对DLL调用不熟,请高手指教。
同时报错说明没有正确调用其中的函数
第一,extern "C" __declspec(dllimport) void WINAPI C_StartUP(long nAddrMsg);这样的声明,是不是确信它是cdecl的压栈方式,还是stdcall的压栈方式。
第二,C++的默认声明void GetMsgFromClient(long nVer, long uin, long i, const char* pbstr, const char* sSrcIP, long nSrcPort),是采用stdcall,还是cdecl?这样也要弄清楚。
这两个没有错的话,就应该可以调用了。至于写在interface,还是implementation,都没有关系的。
getMsgFromClient:我实现了,不过里面还没代码
procedure getMsgFromClient(nver,nUid,itype:Integer;sMSg,sSrcIP:PChar;port:integer);stdcall;
begin
// 以后实现
end;
我试了C_Startup;cdecl;getMsgFromClient;cdecl;
C_Startup;stdcall;getMsgFromClient;stdcall;
C_Startup;cdecl;getMsgFromClient;stdcall;
C_Startup;stdcall;getMsgFromClient;cdecl;都不行,还有其他的问题吗?
1)传入一个整数调用看看,是否一切正常?
2)能否改为这样:P:=LongInt(getMsgFromClient); (我长久不用Delphi了,不知道这样语法行不行?好像Delphi中有个AddressOf的函数)
3)考虑一下,能否使用类似于Windows的回掉函数,这个调用肯定没有问题。
1,我传0,1进去,都不行。
2,P:=Longint(getMsgFromClient);编译不过
AddressOf是VB里面的。
3,不大明白“类似于Windows”的回调函数是什么,请指教。
这个getMsgFromClient回调函数是DLL提供者规定的。
extern "C" __declspec(dllimport) void WINAPI C_StartUP(long nAddrMsg);其中WINAPI也就是__stdcall
对应DELPHI中的stdcall二, GetMsgFromClient在DELPHI中对应的栈约定是cdecl然后就是细心就是耐心了..
是不是把C_StartUP,GetMsgFromClient都声明成stdcall吗,这个我试过了,还是没有用阿
由external '../DLLS/P2PServerDLL.dll'
改成external'\DLLS\P2PServerDLL.dll';
同时所有函数都改成stdcall.就可以了。谢谢各位大大,结帖