Delphi调用vb的dll出错!!!

402 2004-10-05 07:14:57
vb调用该dll的演示代码如下:
Private Declare Function Dacsp_init Lib "dacsplib.dll" () As Long
Private Declare Function Dacsp_login Lib "dacsplib.dll" (ByVal Context As Long) As Integer
Private Declare Function Dacsp_final Lib "dacsplib.dll" (ByVal Context As Long) As Integer

Private Declare Function Dacsp_FileEnc Lib "dacsplib.dll" (ByVal Context As Long, ByVal infil As String, ByVal outfile As String, ByVal pubkey As String) As Integer
Private Declare Function Dacsp_FileDec Lib "dacsplib.dll" (ByVal Context As Long, ByVal infil As String, ByVal outfile As String, ByVal keyNo As Integer) As Integer


Private Declare Function Dacsp_StrEnc Lib "dacsplib.dll" (ByVal Context As Long, ByVal indata As String, ByVal indatalen As Integer, ByVal outdata As String, ByVal pubkey As String) As Integer
Private Declare Function Dacsp_StrDec Lib "dacsplib.dll" (ByVal Context As Long, ByVal indata As String, ByVal indatalen As Integer, ByVal outdata As String, ByVal keyNo As Integer) As Integer

Dim Context As Long
Dim ret As Integer
Dim pubkey As Byte

Private Sub Command1_Click()
Dim str As String
Dim str2 As String * 2048
Dim str3 As String * 2048
Dim pubkey As String


Context = Dacsp_init()
ret = Dacsp_login(Context)
If ret < 0 Then
MsgBox ("登陆失败!")
Exit Sub
End If

以上代码能成功运行无错!!
--------------------------------------------------------------------------------------
我改写成Delphi版的代码如下:
Interface
Function Dacsp_init():Longint;
Function Dacsp_login(Context:Longint):Integer;
Function Dacsp_final(Context:Longint):Integer;
Function Dacsp_FileEnc(Context:Longint;infil,outfile,pubkey:String):Integer;
Function Dacsp_FileDec(Context:Longint;infil,outfile:String;keyno:Integer):Integer;
Function Dacsp_StrEnc(Context:Longint;indata:String;indatalen:Integer;outdata,pubkey:String):Integer;
Function Dacsp_StrDec(Context:Longint;indata:String;indatalen:Integer;outdata:String;keyno:Integer):Integer;

implementation
Function Dacsp_init:Longint;external 'dacsplib.dll';
Function Dacsp_login(Context:Longint):Integer;external 'dacsplib.dll';
Function Dacsp_final(Context:Longint):Integer;external 'dacsplib.dll';
Function Dacsp_FileEnc(Context:Longint;infil,outfile,pubkey:String):Integer;external 'dacsplib.dll';
Function Dacsp_FileDec(Context:Longint;infil,outfile:String;keyno:Integer):Integer;external 'dacsplib.dll';
Function Dacsp_StrEnc(Context:Longint;indata:String;indatalen:Integer;outdata,pubkey:String):Integer;external 'dacsplib.dll';
Function Dacsp_StrDec(Context:Longint;indata:String;indatalen:Integer;outdata:String;keyno:Integer):Integer;external 'dacsplib.dll';

procedure TFrm_Main.Button2Click(Sender: TObject);
var
ret:Integer;
Context:Longint;

Context := Dacsp_init();
ret := Dacsp_login(Context);
if ret < 0 Then
Messagedlg('操作失败!',mtError,[mbok],0);

程序一执行就报错,翻译后大概内容为:"内存访问错误,在模块dacsplib中"

请问为什么同样一个dll在vb中能被成功调用,而在delphi中却会报错呢?是不是变量类型的问题?
还是什么传值和传地址之间的转换??
求高手指点~~~
...全文
186 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
402 2004-10-06
  • 打赏
  • 举报
回复
不好意思,我想是我把标题弄错了。
该dll应该是c++写的,而且用vb调用很成功,但是用delphi调用后两个函数就出错。
http://community.csdn.net/Expert/topic/3428/3428921.xml?temp=.7383234
popmailzjw 2004-10-06
  • 打赏
  • 举报
回复
不是说VB不能编纯DLL吗,他做出来的DLL是以COM方式调用的,必须在系统中注册一下
402 2004-10-06
  • 打赏
  • 举报
回复
在线等待~~
402 2004-10-06
  • 打赏
  • 举报
回复
现在加了stdcall后,前面两个函数调用已经没问题了.但下面两个函数调用仍然抱内存错误阿:
Function Dacsp_StrEnc(Context:Longint;indata:String;indatalen:Integer;outdata,pubkey:String):Integer;stdcall;external 'dacsplib.dll';
Function Dacsp_StrDec(Context:Longint;indata:String;indatalen:Integer;outdata:String;keyno:Integer):Integer;stdcall;external 'dacsplib.dll';
我将上面的String类型替换成Pchar类型后,仍然抱错,替换前是"不能写入0000000",替换后报错"某某内存地址在dacsplib模块中访问错误"
------------------------------------------------------------------------------------
下面是vb的函数定义,而且已经测试成功!
Private Declare Function Dacsp_StrEnc Lib "dacsplib.dll" (ByVal Context As Long, ByVal indata As String, ByVal indatalen As Integer, ByVal outdata As String, ByVal pubkey As String) As Integer
Private Declare Function Dacsp_StrDec Lib "dacsplib.dll" (ByVal Context As Long, ByVal indata As String, ByVal indatalen As Integer, ByVal outdata As String, ByVal keyNo As Integer) As Integer



402 2004-10-06
  • 打赏
  • 举报
回复
to netrobo(……)
那为什么delphi里用string会报内存错误呢??
netrobo 2004-10-06
  • 打赏
  • 举报
回复
你的String型输入数据在VB下是按值传递的,而且测试成功,那在Delphi下就不能用PChar,因为PChar是指针,你改用数组试试。
402 2004-10-06
  • 打赏
  • 举报
回复
ding ~
hsmserver 2004-10-05
  • 打赏
  • 举报
回复

参数里面string:pchar
402 2004-10-05
  • 打赏
  • 举报
回复
那么说我现在这样只要加入stdcall就可以了??
hsmserver 2004-10-05
  • 打赏
  • 举报
回复
不好意思,无奈IE一直打不开
对于不细心而胡答一气,请楼主原谅
stdcall调用方式能解决不同语言的编写的动态连接库的兼容,他是制从右到左的参数调用顺序


有LONGINT类型的
402 2004-10-05
  • 打赏
  • 举报
回复
继续等解答~
402 2004-10-05
  • 打赏
  • 举报
回复
接上一楼,请问是不是必须添加stdcall 呢?
还有一种说法是如果vb用到了string类型的定义,delphi调用时应该改为Pchar类型?
请问是不是这样啊,如果是, 那其他类型(比如long和longint)该如何相互转换呢~
402 2004-10-05
  • 打赏
  • 举报
回复
to hsmserver(撒哈拉之雨的悲伤):
我只把dll放在system32下了(win2000server)
还有啊,我刚才查到帖子说调用dll的时候必须添加stdcall
例如 Function Dacsp_init:Longint;stdcall;external 'dacsplib.dll';
hsmserver 2004-10-05
  • 打赏
  • 举报
回复
写法看着没问题啊
你把DLL拷到当前目录下了吗
402 2004-10-05
  • 打赏
  • 举报
回复
在线等待

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧