DLL接口函数定义问题 熟悉Delphi和VB的朋友请进(100分)

  • 主题发起人 主题发起人 followindy
  • 开始时间 开始时间
F

followindy

Unregistered / Unconfirmed
GUEST, unregistred user!
我现在要定义VBDLL.dll的三个接口函数,<br>在VB中这样定义:<br>Private Declare Function xcom_connect Lib "vbdll.dll" () As Long<br>Private Declare Function xcom_send Lib "vbdll.dll" (ByVal pMessageId As String, ByVal pbuffer As String, <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ByVal nSize As Long, Optional ByVal nUsingDataBase As Long = 0, Optional ByVal nSendType As Long = 0, <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Optional ByVal nBlockorFunction As Long = 50, <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Optional ByVal nUnsendSave As Long = 0) As Long<br>Private Declare Function xcom_recv Lib "vbdll.dll" (ByVal nChannelId As Long, ByVal pbuffer As String, <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ByVal nSize As Long, ByVal pMessageId As String, nRecvLength As Long, nBlockorFunction As Long, <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Optional ByVal nUnsendSave As Long = &amp;HFFFFFFFF) As Long<br><br>结果VB程序中调用这三个函数一切正常。<br><br>我在Delphi中这样定义<br>&nbsp;Function xcom_connect: longInt; stdCall external 'vbdll.dll';<br>&nbsp;Function xcom_send(pMessageId, pbuffer: PChar; nSize: longInt; nUsingDataBase: longInt = 0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nSendType: longInt = 0; nBlockorFunction: longInt = 50; &nbsp;nUnsendSave: longInt = 0): longInt; &nbsp;stdCall external 'vbdll.dll';<br>&nbsp;function XCOM_recv(nChannelId: longInt; pbuffer: PChar; nSize: longInt;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pMessageId: PChar; nRecvLength: longInt; nBlockorFunction: longInt;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nWaitTime: longInt = $HFFFFFFFF): longInt; stdCall external 'vbdll.dll';<br><br>在Delphi程序中,XCOM_Connect调用正常,但XCOM_Send和XCOM_RECV一直报异常。<br>我怀疑是参数类型问题,在Delpi中定义时把PChar换成String、widestring,结果还是异常。<br><br>请指教。<br><br>(另外XCOM_RECV函数中参数nWaitTime: longInt = $HFFFFFFFF定义有问题,请问<br>这个缺省值怎样写呢?是$用得不对吗 )<br><br><br>
 
如何快速学习delphi呢?谢谢
 
勤学多问。
 
有这方面经验的朋友吗?
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=491691
 
To Kccp:<br>调用时我已经注意了PChar处理<br>var<br>&nbsp; Buffer: PChar;<br>&nbsp; B2: PChar;<br>begin<br>&nbsp; GetMem(Buffer,Length('GJK01R') + 1);<br>&nbsp; StrCopy(Buffer, PChar('GJK01R'));<br><br>&nbsp; GetMem(B2,Length('333') + 1); &nbsp; &nbsp; <br>&nbsp; StrCopy(B2, PChar('333'));<br><br>&nbsp; xcom_send(Buffer, B2, length(B2));<br>end;<br><br>但一样的异常。报读地址错误。请指教。<br>
 
我做过DELPHI写DLL,VB调用。你的代码我也没看出毛病来<br><br>Optional 在VB里边是什么意思?
 
在线等候。
 
很困难的问题吗?
 
是stdCall吗?<br><br>ByVal是传值的意思,默认是传地址!<br><br>Option是可选的意思,如果没有指定则使用默认!
 
函数在VBDLL.DLL里面怎么定义的我不知道。<br>按说VB能正常调用,Delphi就肯定有办法能调用出来。<br>请教各位参数类型怎么定?
 
&nbsp;xcom_send(Buffer, B2, length(B2));<br>这一句有问题, Length不能求得B2的长度,因为B2是一个PCHar啊.<br>如果你要求长度的话,应该用StrLen(B2).<br>我只看到这一个错误.<br>
 
To Proman:谢谢指正。<br>但改过来后异常信息还是一样。考虑该DLL可能为C++编写,把stdall改为cdecl,结果一样。<br><br>就算DLL内部使用的是非win正常数据类型,<br>但VB能正常调用,delphi为何就不行呢。
 
var<br>&nbsp; Buffer: PChar;<br>&nbsp; B2: PChar;<br>begin<br>&nbsp; Buffer := PChar('GJK01R');<br>&nbsp; B2 := PChar('333');<br>&nbsp; xcom_send(Buffer, B2, Strlen(B2));<br>没法帮你调,试试上面这样写呢?还有你报的是什么错,把错添消息贴出来啊。
 
一样报错。<br>Project Project1.exe raised exception class EAccessViolation with message<br>'Access violation at address 10002F95 in module 'vbdll.dll'. Read of address <br>00000008'. Process Stopped. Use Step or Run to continue.
 
我知道了,是错在这个地方吧:<br>var<br>&nbsp; Buffer: PChar;<br>&nbsp; B2: PChar;<br>begin<br>&nbsp; GetMem(Buffer,Length('GJK01R') + 1); &nbsp;&lt;================= Length() &lt;=&gt; StrLen()<br>&nbsp; StrCopy(Buffer, PChar('GJK01R'));<br><br>&nbsp; GetMem(B2,Length('333') + 1); &nbsp; &nbsp; <br>&nbsp; StrCopy(B2, PChar('333'));<br><br>&nbsp; xcom_send(Buffer, B2, length(B2));<br>end;<br>
 
报相同的异常。<br>Delphi中PChar和string兼容。Length或者StrLen是一样的。<br><br>
 
你到VB里跟踪一下看看收到的是什么东西
 
1.change stdCall to cdecl,<br>2. remove sharemem.pas, <br>3.use pchar insted of string
 
nWaitTime: longInt = $HFFFFFFFF<br>nWaitTim是INteger,你赋的值溢出了,强制转换一下 <br>nWaitTime:longInt=Integer($FFFFFFFF),等一下,这应该是-1,$FFFFFFFF,<br>应该是警告,你多了个H自然是错误了,<br>1.你可以DUMP一下VBD.dll,可以看出它是永生么编译器编译的,会好办一点<br>2.查查VB中的默认调用方式和各种数据类型的定义,我不用VB,<br>3.Private Declare Function xcom_recv Lib "vbdll.dll" (ByVal nChannelId As Long, ByVal pbuffer As String, <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ByVal nSize As Long, ByVal pMessageId As String, [red]nRecvLength As Long, nBlockorFunction As Long, [/red]<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Optional ByVal nUnsendSave As Long = &amp;HFFFFFFFF) As Long<br>这两个参数是否应该是var 参数?<br>3.
 
后退
顶部