Ivan Arturo Valle
Ivan Arturo Valle

Reputation: 1

Can't read native C++ BYTE * in VB.NET

I'm trying to read the passport information (MRZ) in a reader that uses Win CE 6. NET. vendor's API is written in C + +

//[C + +]
typedef int (* CRXCALLBACK) (BYTE *pRecvBuff, nDataLen int);
int FAR PASCAL EXPORT CRX_Open (CRXCALLBACK lpDataCallback);

This is my VB.NET Implementation

'[VB.NET]
Public Class CRX
  Public Delegate Function CRXCALLBACK (ByVal pRecvBuff As System.IntPtr, ByVal nDataLen As   Integer) As Integer
  <DllImport("Mrz.dll", EntryPoint:="CRX_Open", SetLastError:=True)> _
  Public Shared Function CRX_Open(ByVal lpDataCallback As CRXCALLBACK) As Integer
  End Function
End Class

In a form

Private Sub CRX_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 If CRX.CRX_Open(AddressOf MrzReaderCallback) <> CRX_ERR_SUCCESS Then
   MessageBox.Show("CRX_Open Failed")
 End Sub
Private Function MrzReaderCallback(ByVal pRecvBuff As System.IntPtr, ByVal nDataLen As Integer) As Integer
        Try
            Dim str As String = Marshal.PtrToStringUni(pRecvBuff)
            MsgBox(str)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
        End Try
        Return CRX_ERR_SUCCESS

but when I receive the pRecvBuff content using Marshal.PtrToStringUni (pRecvBuff, nDataLen) returns me unreadable characters

Any suggestions for me, what am I doing wrong?

regards

Ivan

Upvotes: 0

Views: 258

Answers (2)

Ivan Arturo Valle
Ivan Arturo Valle

Reputation: 1

Hello i solve using this code

 Private Function MrzReaderCallback(ByVal pRecvBuff As System.IntPtr, ByVal nDataLen As Integer) As Integer
        Dim b As Byte
        Dim c As Char
        Dim s As String = ""
        Try
            For x As Integer = 8 To nDataLen
                b = Marshal.ReadByte(pRecvBuff, x)
                If b = &H0 Or b = &H1 Or b = &H2 Or b = &H3 Or b = &H60 Then
                    b = &H20
                ElseIf b <> &HD Then
                    c = Convert.ToChar(b)
                    s &= c.ToString
                End If
            Next
            SetText(s, txtMRZ)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error ex")
        End Try
        Return mi3api.MachineReadableDataProcessingAPI.CRX_ERR_SUCCESS
    End Function

Upvotes: 0

Reed Copsey
Reed Copsey

Reputation: 564413

The Marhsal.PtrToStringUni expects the buffer (IntPtr) to contain a byte array comprising a unicode string.

If the C++ API wasn't using Unicode, you may need Marshal.PtrToStringAnsi or Marshal.PtrToStringBSTR instead.

Upvotes: 1

Related Questions