如何调用c++生成的dll文件 ( 积分: 50 )

  • 主题发起人 主题发起人 shenfeng_126
  • 开始时间 开始时间
S

shenfeng_126

Unregistered / Unconfirmed
GUEST, unregistred user!
dll:&nbsp;test.dll<br><br>函数:<br>FUNCTION_DECLARE(CI_RV)<br>CI_SymEncrypt(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nFunction,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;char&nbsp;&nbsp;*pEntity,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;char&nbsp;&nbsp;*pPlainData,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;long&nbsp;&nbsp;nPlainLength,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;char&nbsp;&nbsp;*pCipherData,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;long&nbsp;&nbsp;*pnCipherLength<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)&nbsp;&nbsp;;&nbsp;<br><br>请问在delphi中如何定义和调用呢?<br><br>我的定义如下:<br>function&nbsp;CI_SymEncrypt(nFunction&nbsp;:&nbsp;Integer;&nbsp;pEntity,&nbsp;pPlainData:Pchar;&nbsp;nPlainLength&nbsp;:&nbsp;Integer;&nbsp;pCipherData&nbsp;:&nbsp;Pchar&nbsp;;&nbsp;pnCipherLength:integer):Integer;&nbsp;cdecl;&nbsp;external&nbsp;'test.dll'&nbsp;name&nbsp;'CI_SymEncrypt';<br><br>调用如下:<br>CI_SymEncrypt(1,Pchar('afd'),Pchar('afd'),3,Pchar('afd'),1);<br><br>但运行程序刚启动就报错
 
补充一句:<br>//函数返回值<br>typedef&nbsp;&nbsp;unsigned&nbsp;long&nbsp;&nbsp;&nbsp;CI_RV;<br><br>上面是c++中函数返回值
 
function&nbsp;CI_SymEncrypt(nFunction&nbsp;:&nbsp;Integer;&nbsp;pEntity,&nbsp;pPlainData:Pchar;&nbsp;nPlainLength&nbsp;:&nbsp;Integer;&nbsp;pCipherData&nbsp;:&nbsp;Pchar&nbsp;;&nbsp;var&nbsp;pnCipherLength:integer):Integer;&nbsp;cdecl;&nbsp;external&nbsp;'test.dll'&nbsp;name&nbsp;'CI_SymEncrypt';
 
to&nbsp;wr960204<br>------------<br>改为了你的方法,然后用下面的程序调用,程序运行时还是报错啊<br>var<br>&nbsp;&nbsp;i&nbsp;:integer;<br>begin<br>&nbsp;&nbsp;i&nbsp;:=&nbsp;10;<br>&nbsp;&nbsp;CI_SymEncrypt(1,Pchar('afd'),Pchar('afd'),3,Pchar('adf'),i);
 
首先你nPlainLength好像是pPlainData的长度,而<br>pnCipherLength是pCipherData的长度.最后一个参数你给10但是pCipherData你才给3个字符啊<br><br>另外看看你C的调用约定是什么?<br>要是STDCALL就改成<br>function&nbsp;CI_SymEncrypt(nFunction&nbsp;:&nbsp;Integer;&nbsp;pEntity,&nbsp;pPlainData:Pchar;&nbsp;nPlainLength&nbsp;:&nbsp;Integer;&nbsp;pCipherData&nbsp;:&nbsp;Pchar&nbsp;;&nbsp;var&nbsp;pnCipherLength:integer):Integer;&nbsp;stdcall;&nbsp;external&nbsp;'test.dll'&nbsp;name&nbsp;'CI_SymEncrypt';
 
FUNCTION_DECLARE(CI_RV)<br>这个宏是怎么定义的?这里面包含调用约定信息,LZ却不发,BS
 
typedef&nbsp;&nbsp;unsigned&nbsp;long&nbsp;&nbsp;CI_RV&nbsp;;<br>typedef作用是把CI_RV定义为unsigned&nbsp;long型
 
如果是不同语言中调用的话,在C++声明函数时需要Extern&nbsp;“C”
 
function&nbsp;CI_SymEncrypt(nFunction:DWORD;pEntity,pPlainData:PChar;nPlainLength:DWORD;pCipherData:PChar;pnCipherLength:PDWORD):DWORD;&nbsp;stdcall;&nbsp;external&nbsp;'test.dll'&nbsp;name&nbsp;'CI_SymEncrypt';
 
后退
顶部