delphi 中
function StartReadCard(aHandle: THandle; ComPort: PChar; BaudRate: Integer; var Buffer: array of Byte; var DataLength: Integer): boolean;  stdcall;  external 'ReadCard.dll';var
  buf: array[0..4] of Byte; 
  i: integer;
  iDateLen: Integer;
  sCommPort: string;
  iCommType: Integer;
  iIDReaderType: Byte;
  iBaudRate: Integer;
  sCardNo: string;
  OriginalCardID:Int64;  FillChar(buf, 5, 0);
  sCommPort := 'com6';
  iBaudRate := 9600;
  if StartReadCard(Application.Handle, PChar(sCommPort), iBaudRate, buf, iDateLen) then
     ........
  end ifVB中怎么写
Private Declare Function StartReadCard Lib "ReadCard.dll" (ByVal aHandle As Long, ByVal ComPort As String, ByVal BaudRate As Integer, ByRef Buffer As byte , ByRef DataLength As Long) As Boolean  Dim buf() As byte  
  Dim i As Integer
  Dim iDateLen As Long
  Dim sCommPort As String
  Dim iCommType As Integer
  Dim iIDReaderType As Byte
  Dim iBaudRate As Integer
  Dim sCardNo As String
  Dim OriginalCardID As Long  sCommPort = "com6"
  iBaudRate = 9600
  ReDim buf(4)
  For i = 0 To UBound(buf)
      buf(i) = "0"
  Next i
  If StartReadCard(App.hInstance, sCommPort, iBaudRate, buf(0), VarPtr(iDateLen)) Then
     ............
  end if到StartReadCard这,就报内存错误.
帮忙看下,哪有问题

解决方案 »

  1.   

    主要问题是不是在StartReadCard的最后第二个参数上,DELPHI的buf好像将数据的所有值都传了,但是VB只能传一个,怎么办?
      

  2.   

    主要问题是不是在StartReadCard的最后第二个参数上,DELPHI的buf好像将数组的所有值都传了,但是VB只能传一个,怎么办? 
      

  3.   

    DELPHI的buf值为(0,0,0,0,0)
    VB中怎么表示?
      

  4.   

    这个问题,没有DLL,光靠猜测是很不容易的。
      

  5.   


    Option Explicit
    'function StartReadCard(aHandle: THandle;
    '                       ComPort: PChar;
    '                       BaudRate: Integer;
    '                       var Buffer:array of Byte;
    '                       var DataLength: Integer): boolean; stdcall; external 'ReadCard.dll';
    Private Declare Function StartReadCard Lib "ReadCard.dll" (ByVal aHandle As Long, _
                                                               ByVal ComPort As String, _
                                                               ByVal BaudRate As Long, _
                                                               Buffer() As Byte, _
                                                               ByVal DataLength As Long) As Boolean
    Private Sub Form_Load()
        Dim buf() As Byte
        Dim i As Integer
        Dim iDateLen As Long
        Dim sCommPort As String
        Dim iCommType As Integer
        Dim iIDReaderType As Byte
        Dim iBaudRate As Integer
        Dim sCardNo As String
        Dim OriginalCardID As Long
        
        sCommPort = "COM6"
        iBaudRate = 9600
        ReDim buf(4)
        For i = 0 To UBound(buf)
            buf(i) = "0"
        Next i
        If StartReadCard(App.hInstance, sCommPort, iBaudRate, ByVal VarPtr(buf(0)), iDateLen) Then
        
        End If
    End Sub
      

  6.   

    发给我看看?
    [email protected]
      

  7.   

    iDateLen在DELPHI调试时,发现应该也是传的内存地址.但返回值好像是INTEGER
    是不是这个参数的原因啊?
      

  8.   

    现在改成这样了,还是不行,麻烦大家帮忙看看
    Private Declare Function StartReadCard Lib "ReadCard.dll" (ByVal aHandle As Long, ByVal ComPort As String, ByVal BaudRate As Integer, Buffer() As Byte, ByVal DataLength As Long) As BooleanPrivate Sub Command1_Click()
      Dim buf(4) As Byte
      Dim i As Integer
      Dim iDateLen As Long
      Dim sCommPort As String
      Dim iCommType As Integer
      Dim iIDReaderType As Byte
      Dim iBaudRate As Integer
      Dim sCardNo As String
      Dim OriginalCardID As Long
      
      sCommPort = "com6"
      iBaudRate = 9600 For i = 0 To 3
         buf(i) = "0"
     Next i
      If StartReadCard(App.hInstance, sCommPort, iBaudRate, buf, VarPtr(iDateLen)) Then
    ......
      end if
      

  9.   

    Private Declare Function StartReadCard Lib "ReadCard.dll" (ByVal aHandle As Long, ByVal ComPort As String, ByVal BaudRate As Integer, Buffer() As Byte, ByVal DataLength As integer) As BooleanPrivate Sub Command1_Click()
      Dim buf(4) As Byte
      Dim i As Integer
      Dim iDateLen As integer
      Dim sCommPort As String
      Dim iCommType As Integer
      Dim iIDReaderType As Byte
      Dim iBaudRate As Integer
      Dim sCardNo As String
      Dim OriginalCardID As Long
       
      sCommPort = "com6"
      iBaudRate = 9600 For i = 0 To 3
      buf(i) = "0"
     Next i
      If StartReadCard(App.hInstance, sCommPort, iBaudRate, buf, VarPtr(iDateLen)) Then
    ......
      end if
    改成这样,读卡不报内存错误,报溢出,是什么原因,为什么哪溢出?
      

  10.   

    用depends on看下DLL函数的参数类型,然后再决定如何声明。
      

  11.   

    Private Declare Function StartReadCard Lib "ReadCard.dll" (ByVal aHandle As Long, ByVal ComPort As String, ByVal BaudRate As Integer, Buffer() As Byte, ByVal DataLength As integer) As Booleandelphi中的integer是16位还是32位?VB中的integer是16位的哦,两字节.另外DataLength应该直接传入数值,而不是传入指针.所以,应该这样试试:If StartReadCard(App.hInstance, sCommPort, iBaudRate, buf, iDateLen) Then
      

  12.   

    If StartReadCard(App.hInstance, sCommPort, iBaudRate, buf, iDateLen) Then
    这样运行报内存错误;
    If StartReadCard(App.hInstance, sCommPort, iBaudRate, buf, varptr(iDateLen)) Then
    这样运行报溢出.
      

  13.   

    function StartReadCard(aHandle: THandle; ComPort: PChar; BaudRate: Integer; var Buffer: array of Byte; var DataLength: Integer): boolean; stdcall; external 'ReadCard.dll';试试改成这样:
    Private Declare Function StartReadCard Lib "ReadCard.dll" (ByVal aHandle As Long, ByVal ComPort As Byte, ByVal BaudRate As Long, ByRef Buffer As byte , ByRef DataLength As Long) As Boolean
    最后一个参数,byval,byref都试试看
      

  14.   

    ByVal ComPort As Byte, 改成这样,报类型错误
    最后下个参数byval,byref都一样,LONG内存错误,integer溢出.
      

  15.   

    FillChar(buf, 5, 0);
    在VB中怎么表示啊?
      

  16.   

    DataLength在DELPHI中调试时,是1242600,应该是内存地址,返值的是5,应该是个integer,可能问题还是出在最后一个参数上,麻烦大家帮忙想想办法.
      

  17.   

    Private Declare Function StartReadCard Lib "ReadCard.dll" (ByVal aHandle As Long, ByVal ComPort As string, ByVal BaudRate As Long, ByRef Buffer As byte , ByRef DataLength As Long) As BooleanComPort 改回ByVal ComPort As string呢?我不太清楚dephi的pchar是什么类型 
      

  18.   


    到dephi区去咨询一下各个参数的数据类型吧
      

  19.   

    怎么定义BYTE数组长度,让它大于4
      

  20.   

    Option ExplicitPrivate Declare Function StartReadCard Lib "ReadCard.dll" (ByVal aHandle As Long, ByVal ComPort As String, ByVal BaudRate As Long, Buffer() As Byte, ByVal DataLength As Long) As Boolean
    Private Declare Function GetActiveWindow Lib "user32" () As LongPrivate Sub Command1_Click()
      Dim buf(255) As Byte
      
      Dim i As Integer
      Dim iDateLen As Long
      Dim sCommPort As String  Dim iBaudRate As Long
      Dim sCardNo As String
      Dim OriginalCardID As Long
      
      sCommPort = "com6"
      iBaudRate = 9600  If StartReadCard(GetActiveWindow, sCommPort, iBaudRate, buf, iDateLen) Then
    ...
      End If
    End Sub现在改成这样了,但是报"接收卡号的BYTE数组长度必须大于4"
    我怎么指定长度?
      

  21.   

    If StartReadCard(GetActiveWindow, sCommPort, iBaudRate, buf, iDateLen) Then
    这样写法应该不对
    改成:
    If StartReadCard(GetActiveWindow, sCommPort, iBaudRate, buf(0), 256) Then
      

  22.   

    If StartReadCard(GetActiveWindow, sCommPort, iBaudRate, buf(0), 256) Then
     
    不报错误了,但读完以后又老样子了,内存错误.
      

  23.   

    Private Declare Function StartReadCard Lib "ReadCard.dll" (ByVal aHandle As Long, ByVal ComPort As String, ByVal BaudRate As Long, Byval pBuffer As Long, ByVal DataLength As Long) As Boolean
    然后调用时,pBuffer参数用指针传入:varptr(buf(0))最后一个参数直接传值试试看.另外,最好把DELPHI的数据长度弄到手,确定数据长度后再好搞.如果实在没有,那就直接用OD跟踪DELPHI版的程序调用,看看堆栈里到底压入了些什么信息.
      

  24.   

    试了,最后一直参数直接传值了,最后第二个参数照你的方法试了,一样,编译成EXE后,可以读出值,但报内存错误.
      

  25.   

    报内存错误,估计堆栈不平衡吧?你要注意一下到底DELPHI的数据类型的长度是多少,不能有差别.你确定DELPHI的Integer也是两字节么?