读书人

VB有关指针字符的有关问题

发布时间: 2011-12-30 23:30:45 作者: rapoo

VB有关指针字符的问题.
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 if

VB中怎么写
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这,就报内存错误.
帮忙看下,哪有问题

[解决办法]

VB code
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 BooleanPrivate 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 IfEnd Sub
[解决办法]
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都试试看

[解决办法]
头痛的问题。。。

读书人网 >VB

热点推荐