Can't get sCardTransmit to work in VB2010

CCKram
CCKram used Ask the Experts™
on
I have attahched the code.
I get the error on the scardtransmit saying invalid_parameter.
Useing a SCM 331-DI reader.

I just want to read a MiFare Classic UID.
Imports Microsoft.Win32
Imports Microsoft.VisualBasic
Imports System
Imports System.Runtime.InteropServices
Imports System.Text
Imports System.IO
Imports System.Windows.Forms
Public Class Form1
    Public Structure SCARD_IO_REQUEST
        Public dwProtocol As Long
        Public cbPciLength As Long
    End Structure

    Public Enum CurrentState
        ''' <summary>This value implies the driver is unaware of the current state of the reader.</summary>
        SCARD_UNKNOWN = 0
        ''' <summary> This value implies there is no card in the reader.</summary>
        SCARD_ABSENT = 1
        ''' <summary>This value implies there is a card is present in the reader, but that it has
        ''' not been moved into position for use.</summary>
        SCARD_PRESENT = 2
        ''' <summary>This value implies there is a card in the reader in position for use. The card is
        '''not powered.</summary>
        SCARD_SWALLOWED = 3
        ''' <summary>This value implies there is power is being provided to the card, but the
        '''Reader Driver is unaware of the mode ofthe card.</summary>
        SCARD_POWERED = 4
        ''' <summary>This value implies the card has been reset and is awaiting PTS negotiation.</summary>
        SCARD_NEGOTIABLE = 5
        ''' <summary>This value implies the card has been reset and specific communication
        '''protocols have been established.</summary>
        SCARD_SPECIFIC = 6
    End Enum

    ' The flags attribute means the values are used in combination - like the Anchor property on a form.
    <Flags()> _
    Public Enum Protocols
        ''' <summary>There is no active protocol.</summary>
        SCARD_PROTOCOL_UNDEFINED = 0
        ''' <summary>T=0 is the active protocol.</summary>
        SCARD_PROTOCOL_T0 = 1
        ''' <summary>T=1 is the active protocol.</summary>
        SCARD_PROTOCOL_T1 = 2
        ''' <summary>Raw is the active protocol.</summary>
        SCARD_PROTOCOL_RAW = &H10000
    End Enum

    <DllImport("winscard.dll")> _
    Public Shared Function SCardStatus( _
ByVal hCard As IntPtr, _
ByVal szReaderName As StringBuilder, _
ByRef pcchReaderLen As Integer, _
ByRef pdwState As CurrentState, _
ByRef pdwProtocol As Protocols, _
ByVal pbAtr() As Byte, _
ByRef pcbAtrLen As Integer) As Integer
    End Function


    <DllImport("Winscard.dll")> Public Shared Function SCardTransmit( _
        ByVal hCard As Integer, _
        ByRef pioSendRequest As IntPtr, _
        ByVal SendBuff As Byte(), _
        ByVal SendBuffLen As Integer, _
        <Out()> ByRef pioRecvRequest As IntPtr, _
        <Out()> ByVal RecvBuff As Byte(), _
        <Out()> ByRef RecvBuffLen As Integer) As Integer
    End Function
    'Public Declare Ansi Function SCardTransmit Lib "winscard.dll" (ByVal hCard As Integer, ByVal pioSendRequest As IntPtr, ByVal SendBuff As Byte(), ByVal SendBuffLen As Integer, ByVal pioRecvRequest As IntPtr, <Out()> ByRef RecvBuff As Byte(), <Out()> ByRef RecvBuffLen As Integer) As Integer


    <DllImport("winscard.dll", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function SCardEstablishContext( _
                        ByVal dwScope As Int32, _
                        ByVal pvReserved1 As Int32, _
                        ByVal pvReserved2 As Int32, _
                        <Out()> ByRef phContext As Int32) As Integer
    End Function
    Public Declare Function SCardConnect Lib "WinScard.dll" Alias "SCardConnectA" (ByVal hContext As Integer, ByVal szReaderName As String, ByVal dwShareMode As Integer, ByVal dwPrefProtocol As Integer, ByRef hCard As Integer, ByRef ActiveProtocol As Integer) As Integer
    Public Declare Function SCardDisconnect Lib "WinScard.dll" (ByVal hCard As Integer, ByVal Disposistion As Integer) As Integer
    Public Declare Function SCardReleaseContext Lib "WinScard.dll" (ByVal hContext As Integer) As Integer




    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        On Error GoTo ErrCode
        REM Tjek at der er en læser til stede og at der kan connectes til den.
        Dim hContext As System.Int32 = 0
        Dim lResult As System.Int32 = 0
        Dim lState As Int32 = 0
        Dim lAtrLen As Int32 = 255
        Dim bAtr() As Byte = Nothing 'Array = BytArray.CreateInstance 'BytArray.CreateInstance()

        Dim lMode As Int32 = 2
        Dim lProtocol As Int32 = 3
        Dim hCard As IntPtr = 0
        Dim lActiveProtocol As Int32 = 0
        Dim szReaderList(256) As String
        REM Dim szpcchReaders As UIntPtr
        Dim OMessage As String = ""
        Dim SnoDec As String = ""
        Dim SnoHex As String = ""
        Dim FieldID As Integer = 0
        Dim FieldIDSuB As Integer = 0
        Dim TempString As String = ""
        Dim TempStringSUB As String = ""
        Dim szReaderLEN As Integer = 200
        Dim szReader As New StringBuilder(szReaderLEN)
        ' You would get a Handle using SCardConnect - your signature for that might need updating
        Dim cardHandle As IntPtr = 256
        ' Then allocate enough room in the stringbuilder, the sample code on msdn uses 200 WCHARS...
        Dim readerLength As Integer = 256
        Dim readerName As New StringBuilder(readerLength) ' sets the capacity
        ' Similar for the byte array:
        Dim byteBufferSize As Integer = 32
        ' VB.Net arrays are declared using the upper bound, not the capacity...
        Dim byteBuffer(byteBufferSize - 1) As Byte
        Dim bytCommand(4) As Byte

        ' And we need variables to store the other returned info:
        Dim state As CurrentState = Nothing
        Dim protocol As Protocols = Nothing


        Dim szReaderName As String = "SCM Microsystems Inc. SCR331-DI USB ContactlessReader 0"
        lResult = SCardEstablishContext(0, 0, 0, hContext)

        If lResult = 0 Then
            REM lResult = SCardListReaders(hContext, Nothing, szReaderList, szpcchReaders)
            lResult = SCardConnect(hContext, szReaderName, lMode, lProtocol, hCard, lActiveProtocol)
            'lResult = SCardConnect(hContext, szReaderName, lMode, lProtocol, hCard, lActiveProtocol)
        End If

        REM Læs ATR fra Chip
        If lResult = 0 Then
            ' You should always do something with the return value from an API call, even if you just assign
            ' it to a variable. So sayeth the FXCOP.
            lResult = SCardStatus(hCard, readerName, readerLength, state, lProtocol, byteBuffer, byteBufferSize)
        End If

        If lResult = 0 Then
            Dim i As Integer
            Dim ssvar As SCARD_IO_REQUEST
            ssvar.dwProtocol = lProtocol
            ssvar.cbPciLength = Len(ssvar)
            'FC 20 60 00 06 FF FF FF FF FF FF
            Dim command As Integer
            Dim iReturnlength As Integer
            Dim byteReadBuffer(601) As Byte
            command = 1

            'If command = 2 Then
            '    bytCommand(0) = &HFC
            '    bytCommand(1) = &H20
            '    bytCommand(2) = &H60
            '    bytCommand(3) = &H0
            '    bytCommand(4) = &H6
            '    bytCommand(5) = &HFF
            '    bytCommand(6) = &HFF
            '    bytCommand(7) = &HFF
            '    bytCommand(8) = &HFF
            '    bytCommand(9) = &HFF
            '    bytCommand(10) = &HFF
            '    i = 11
            'End If

            If command = 1 Then
                bytCommand(0) = &HFF 'CLA
                bytCommand(1) = &HB0 'INS
                bytCommand(2) = &H0  'P1
                bytCommand(3) = &H0  'P2
                bytCommand(4) = &H10 'le
                i = 5
            End If

            If command = 3 Then ' SCM siger denne henter UID
                bytCommand(0) = &HFF 'FC
                bytCommand(1) = &HCA 'B0
                bytCommand(2) = &H0
                bytCommand(3) = &H0
                bytCommand(4) = &H0
                i = 5
            End If
            'If command = 4 Then
            ' bytCommand(0) = &HFF 'FC
            ' bytCommand(1) = &HF3 'B0
            ' bytCommand(2) = &H0
            ' bytCommand(3) = &H0
            ' bytCommand(4) = &H4
            ' i = 5
            'End If
            iReturnlength = i + 2
            'lResult = NativeMethods.SCardtransmit(hCard, 0, bytCommand, 5, 0, byteReadBuffer, iReturnlength)
            lResult = SCardTransmit(hCard, IntPtr.Zero, bytCommand, i, IntPtr.Zero, byteReadBuffer, iReturnlength)
        End If
        lResult = SCardDisconnect(hCard, 0)
        lResult = SCardReleaseContext(hContext)
        GoTo TheEnd
ErrCode:
        Select Case Err()
            Case Else
                MsgBox(Err.Number + vbCrLf + Err.Description)
        End Select
TheEnd:
    End Sub
End Class

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
I have also tried this (just in case you would ask;)
Public Declare Function SCardTransmit Lib "winscard.dll" ( _
    ByVal m_hCard As Integer, _
    ByRef pioSendRequest As SCARD_IO_REQUEST, _
    ByRef SendBuff As Byte, _
    ByVal SendBuffLen As Integer, _
    ByRef pioRecvRequest As SCARD_IO_REQUEST, _
    ByRef RecvBuff As Byte, _
    ByRef RecvBuffLen As Integer _
) As Integer

returncode = SCardTransmit(m_hCard, SendRequest, SendBuff(0), SendBuffLen, RecvRequest, RecvBuff(0), RecvBuffLen)

Open in new window

Most Valuable Expert 2012
Top Expert 2014

Commented:

Author

Commented:
Thanks for getting back to me.
Ok, so this is what i have now to the declare:
 
Public Declare Function SCardTransmit Lib "winscard.dll" (ByVal hCard As IntPtr, ByVal pioSendRequest As IntPtr, ByVal SendBuff As Byte(), ByVal SendBuffLen As Integer, ByVal pioRecvRequest As IntPtr, <Out()> ByRef RecvBuff As Byte(), <Out()> ByRef RecvBuffLen As Integer) As Integer

Open in new window

And this is the code:
 
Public Sub DoTransmit(ByVal hCard As IntPtr)
        Dim lResult As System.Int32 = 0
        Dim bytCommand(4) As Byte
        Dim byteReadBuffer(601) As Byte
        Dim iReturnlength As Integer
        Dim i As Integer = 5

        bytCommand(0) = &HFF 'FC
        bytCommand(1) = &HCA 'B0
        bytCommand(2) = &H0
        bytCommand(3) = &H0
        bytCommand(4) = &H0

        lResult = SCardTransmit(hCard, IntPtr.Zero, bytCommand, i, IntPtr.Zero, byteReadBuffer, iReturnlength)
    End Sub

Open in new window


This give me the lresult = 0. Thats good!
But i'm not getting any responds in byteReadBuffer. It's just blank.
Most Valuable Expert 2012
Top Expert 2014

Commented:
What are you passing into hcard?

Author

Commented:
From:
 
lResult = SCardEstablishContext(0, 0, 0, hContext)
lResult = SCardConnect(hContext, szReaderName, lMode, lProtocol, hCard, lActiveProtocol)
lResult = SCardStatus(hCard, readerName, readerLength, state, lProtocol, byteBuffer, byteBufferSize)

Open in new window


Those 3 all results in lResult=0.
I also the the correct ATR from SCardStatus, so i know i'm talking to the MiFare classic card.

The declares of these are:
 
<DllImport("winscard.dll", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function SCardEstablishContext( _
                        ByVal dwScope As Int32, _
                        ByVal pvReserved1 As Int32, _
                        ByVal pvReserved2 As Int32, _
                        <Out()> ByRef phContext As Int32) As Integer
    End Function
    Public Declare Function SCardConnect Lib "WinScard.dll" Alias "SCardConnectA" (ByVal hContext As Integer, ByVal szReaderName As String, ByVal dwShareMode As Integer, ByVal dwPrefProtocol As Integer, ByRef hCard As Integer, ByRef ActiveProtocol As Integer) As Integer
    Public Declare Function SCardDisconnect Lib "WinScard.dll" (ByVal hCard As Integer, ByVal Disposistion As Integer) As Integer
    Public Declare Function SCardReleaseContext Lib "WinScard.dll" (ByVal hContext As Integer) As Integer

Open in new window

Most Valuable Expert 2012
Top Expert 2014

Commented:
I have never done something like this myself but my understanding is that you need to pass some sort of handle in the hcard parameter. Are you assigning it an handle?

Author

Commented:
The way this works is that first i run the SCardEstablishContext.
This allows me to to run the SCardConnect with the hContext handle. This provides me with the hCard information i need.
Then i can run the SCardStatus to check if it is the correct card i need to speak to, in this case it's a MiFare Classic 1k.
Then ill run the SCardtransit with the hCard as i just used in SCardStatus. And this is where i get the problems.
Most Valuable Expert 2012
Top Expert 2014
Commented:
You are still using Integer instead of IntPtr in many places such as

Public Declare Function SCardConnect Lib "WinScard.dll" Alias "SCardConnectA" (ByVal hContext As Integer, ByVal szReaderName As String, ByVal dwShareMode As Integer, ByVal dwPrefProtocol As Integer, ByRef hCard As Integer, ByRef ActiveProtocol As Integer) As Integer


And I think hContext would need to be a IntPtr as well instead of Integer.

Author

Commented:
Yes, i noticed that.
So i changed it all. After i have done this my lResult = 1783 at the SCardTransit.
I'm trying to find out what that is, read the docs.
This is the updated (and cleaned) code im working with:
 
Imports Microsoft.Win32
Imports Microsoft.VisualBasic
Imports System
Imports System.Runtime.InteropServices
Imports System.Text
Imports System.IO
Imports System.Windows.Forms
Public Class Form1
    Public Structure SCARD_IO_REQUEST
        Public dwProtocol As Integer
        Public cbPciLength As Integer
    End Structure
    Public Structure APDURec
        Public bCLA As Byte
        Public bINS As Byte
        Public bP1 As Byte
        Public bP2 As Byte
        Public bP3 As Byte
        <VBFixedArray(255)> Public Data() As Byte
        Public IsSend As Boolean
        Public Sub Initialize()
            ReDim Data(255)
        End Sub
    End Structure


    Public Enum CurrentState
        ''' <summary>This value implies the driver is unaware of the current state of the reader.</summary>
        SCARD_UNKNOWN = 0
        ''' <summary> This value implies there is no card in the reader.</summary>
        SCARD_ABSENT = 1
        ''' <summary>This value implies there is a card is present in the reader, but that it has
        ''' not been moved into position for use.</summary>
        SCARD_PRESENT = 2
        ''' <summary>This value implies there is a card in the reader in position for use. The card is
        '''not powered.</summary>
        SCARD_SWALLOWED = 3
        ''' <summary>This value implies there is power is being provided to the card, but the
        '''Reader Driver is unaware of the mode ofthe card.</summary>
        SCARD_POWERED = 4
        ''' <summary>This value implies the card has been reset and is awaiting PTS negotiation.</summary>
        SCARD_NEGOTIABLE = 5
        ''' <summary>This value implies the card has been reset and specific communication
        '''protocols have been established.</summary>
        SCARD_SPECIFIC = 6
    End Enum

    ' The flags attribute means the values are used in combination - like the Anchor property on a form.
    <Flags()> _
    Public Enum Protocols
        ''' <summary>There is no active protocol.</summary>
        SCARD_PROTOCOL_UNDEFINED = 0
        ''' <summary>T=0 is the active protocol.</summary>
        SCARD_PROTOCOL_T0 = 1
        ''' <summary>T=1 is the active protocol.</summary>
        SCARD_PROTOCOL_T1 = 2
        ''' <summary>Raw is the active protocol.</summary>
        SCARD_PROTOCOL_RAW = &H10000
    End Enum

    <DllImport("winscard.dll")> _
    Public Shared Function SCardStatus( _
        ByVal hCard As IntPtr, _
        ByVal szReaderName As StringBuilder, _
        ByRef pcchReaderLen As Integer, _
        ByRef pdwState As CurrentState, _
        ByRef pdwProtocol As Protocols, _
        ByVal pbAtr() As Byte, _
        ByRef pcbAtrLen As Integer) As Integer
    End Function


    <DllImport("Winscard.dll")> _
    Public Shared Function SCardTransmit( _
        ByVal hCard As IntPtr, _
        ByRef pioSendRequest As SCARD_IO_REQUEST, _
        ByVal SendBuff() As Byte, _
        ByVal SendBuffLen As Integer, _
        ByVal pioRecvRequest As IntPtr, _
        ByVal RecvBuff() As Byte, _
        ByRef RecvBuffLen As Integer) As Integer
    End Function

    <DllImport("winscard.dll", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function SCardEstablishContext( _
        ByVal dwScope As Int32, _
        ByVal pvReserved1 As Int32, _
        ByVal pvReserved2 As Int32, _
        <Out()> ByRef phContext As IntPtr) As Integer
    End Function
    Public Declare Function SCardConnect Lib "WinScard.dll" Alias "SCardConnectA" ( _
        ByVal hContext As IntPtr, _
        ByVal szReaderName As String, _
        ByVal dwShareMode As Integer, _
        ByVal dwPrefProtocol As Integer, _
        ByRef hCard As IntPtr, _
        ByRef ActiveProtocol As Integer) As Integer

    Public Declare Function SCardDisconnect Lib "WinScard.dll" ( _
        ByVal hCard As IntPtr, _
        ByVal Disposistion As Integer) As Integer

    Public Declare Function SCardReleaseContext Lib "WinScard.dll" (ByVal hContext As IntPtr) As Integer




    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        On Error GoTo ErrCode
        Dim hContext As IntPtr
        Dim lResult As System.Int32 = 0
        Dim lState As Int32 = 0
        Dim lAtrLen As Int32 = 255
        Dim bAtr() As Byte = Nothing
        Dim lMode As Int32 = 2
        Dim lProtocol As Int32 = 3
        Dim hCard As IntPtr = Marshal.AllocHGlobal(32)
        Dim lActiveProtocol As Int32 = 0
        Dim readerLength As Integer = 256
        Dim readerName As New StringBuilder(readerLength)
        Dim byteBufferSize As Integer = 32
        Dim byteBuffer(byteBufferSize - 1) As Byte

        Dim state As CurrentState = Nothing

        Dim szReaderName As String = "SCM Microsystems Inc. SCR331-DI USB ContactlessReader 0"
        lResult = SCardEstablishContext(0, 0, 0, hContext)

        If lResult = 0 Then
            lResult = SCardConnect(hContext, szReaderName, lMode, lProtocol, hCard, lActiveProtocol)
        End If

        If lResult = 0 Then
            lResult = SCardStatus(hCard, readerName, readerLength, state, lProtocol, byteBuffer, byteBufferSize)
        End If

        If lResult = 0 Then
            DoTransmit(hCard)
        End If
        lResult = SCardDisconnect(hCard, 0)
        lResult = SCardReleaseContext(hContext)
        GoTo TheEnd
ErrCode:
        Select Case Err()
            Case Else
                MsgBox(Err.Number + vbCrLf + Err.Description)
        End Select
TheEnd:
    End Sub
    Public Sub DoTransmit(ByVal hCard As IntPtr)

        Dim lResult As System.Int32 = 0
        Dim bytCommand(4) As Byte
        Dim byteReadBuffer(255) As Byte
        Dim iReturnlength As Integer = &H10
        Dim RecvRequest As SCARD_IO_REQUEST
        Dim SendRequest As SCARD_IO_REQUEST
        Dim i As Integer = 5

        bytCommand(0) = &HFF 'FC
        bytCommand(1) = &HCA 'B0
        bytCommand(2) = &H0
        bytCommand(3) = &H0
        bytCommand(4) = &H0


        SendRequest.dwProtocol = 3
        SendRequest.cbPciLength = Len(SendRequest)

        RecvRequest.dwProtocol = 3
        RecvRequest.cbPciLength = Len(RecvRequest)

        lResult = SCardTransmit(hCard, SendRequest, bytCommand, i, IntPtr.Zero, byteReadBuffer, iReturnlength)
    End Sub


End Class

Open in new window

Author

Commented:
Okay,

1783 is:
 6F7       1783 The stub received bad data
And in this case the stub must be the SCardTransmit. So i must be sending bad data to it.
Ill check it again and get back to you.
Still any advise would be appriciated.

Author

Commented:
Ok, the error was in the selected protocol. I corrected that and my lReulst = 0. This is good.
But the responds i get back is still no good.
 
Imports Microsoft.Win32
Imports Microsoft.VisualBasic
Imports System
Imports System.Runtime.InteropServices
Imports System.Text
Imports System.IO
Imports System.Windows.Forms
Public Class Form1
    Public Structure SCARD_IO_REQUEST
        Public dwProtocol As UInt32
        Public cbPciLength As UInt32
    End Structure
    Public Structure APDURec
        Public bCLA As Byte
        Public bINS As Byte
        Public bP1 As Byte
        Public bP2 As Byte
        Public bP3 As Byte
        <VBFixedArray(255)> Public Data() As Byte
        Public IsSend As Boolean
        Public Sub Initialize()
            ReDim Data(255)
        End Sub
    End Structure


    Public Enum CurrentState
        ''' <summary>This value implies the driver is unaware of the current state of the reader.</summary>
        SCARD_UNKNOWN = 0
        ''' <summary> This value implies there is no card in the reader.</summary>
        SCARD_ABSENT = 1
        ''' <summary>This value implies there is a card is present in the reader, but that it has
        ''' not been moved into position for use.</summary>
        SCARD_PRESENT = 2
        ''' <summary>This value implies there is a card in the reader in position for use. The card is
        '''not powered.</summary>
        SCARD_SWALLOWED = 3
        ''' <summary>This value implies there is power is being provided to the card, but the
        '''Reader Driver is unaware of the mode ofthe card.</summary>
        SCARD_POWERED = 4
        ''' <summary>This value implies the card has been reset and is awaiting PTS negotiation.</summary>
        SCARD_NEGOTIABLE = 5
        ''' <summary>This value implies the card has been reset and specific communication
        '''protocols have been established.</summary>
        SCARD_SPECIFIC = 6
    End Enum

    ' The flags attribute means the values are used in combination - like the Anchor property on a form.
    <Flags()> _
    Public Enum Protocols
        ''' <summary>There is no active protocol.</summary>
        SCARD_PROTOCOL_UNDEFINED = 0
        ''' <summary>T=0 is the active protocol.</summary>
        SCARD_PROTOCOL_T0 = 1
        ''' <summary>T=1 is the active protocol.</summary>
        SCARD_PROTOCOL_T1 = 2
        ''' <summary>Raw is the active protocol.</summary>
        SCARD_PROTOCOL_RAW = &H10000
    End Enum

    <DllImport("winscard.dll")> _
    Public Shared Function SCardStatus( _
        ByVal hCard As IntPtr, _
        ByVal szReaderName As StringBuilder, _
        ByRef pcchReaderLen As Integer, _
        ByRef pdwState As CurrentState, _
        ByRef pdwProtocol As Protocols, _
        ByVal pbAtr() As Byte, _
        ByRef pcbAtrLen As Integer) As Integer
    End Function


    <DllImport("Winscard.dll")> _
    Public Shared Function SCardTransmit( _
        ByVal hCard As IntPtr, _
        ByRef pioSendRequest As SCARD_IO_REQUEST, _
        ByVal SendBuff() As Byte, _
        ByVal SendBuffLen As Integer, _
        ByVal pioRecvRequest As IntPtr, _
        ByVal RecvBuff() As Byte, _
        ByRef RecvBuffLen As Integer) As Integer
    End Function

    <DllImport("winscard.dll", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function SCardEstablishContext( _
        ByVal dwScope As Int32, _
        ByVal pvReserved1 As Int32, _
        ByVal pvReserved2 As Int32, _
        <Out()> ByRef phContext As IntPtr) As Integer
    End Function
    Public Declare Function SCardConnect Lib "WinScard.dll" Alias "SCardConnectA" ( _
        ByVal hContext As IntPtr, _
        ByVal szReaderName As String, _
        ByVal dwShareMode As Integer, _
        ByVal dwPrefProtocol As Integer, _
        ByRef hCard As IntPtr, _
        ByRef ActiveProtocol As Integer) As Integer

    Public Declare Function SCardDisconnect Lib "WinScard.dll" ( _
        ByVal hCard As IntPtr, _
        ByVal Disposistion As Integer) As Integer

    Public Declare Function SCardReleaseContext Lib "WinScard.dll" (ByVal hContext As IntPtr) As Integer




    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        On Error GoTo ErrCode
        Dim hContext As IntPtr
        Dim lResult As System.Int32 = 0
        Dim lState As Int32 = 0
        Dim lAtrLen As Int32 = 255
        Dim bAtr() As Byte = Nothing
        Dim lMode As Int32 = 2
        Dim lProtocol As Int32 = 3
        Dim hCard As IntPtr = Marshal.AllocHGlobal(32)
        Dim lActiveProtocol As Int32 = 0
        Dim readerLength As Integer = 256
        Dim readerName As New StringBuilder(readerLength)
        Dim byteBufferSize As Integer = 32
        Dim byteBuffer(byteBufferSize - 1) As Byte

        Dim state As CurrentState = Nothing

        Dim szReaderName As String = "SCM Microsystems Inc. SCR331-DI USB ContactlessReader 0"
        lResult = SCardEstablishContext(0, 0, 0, hContext)

        If lResult = 0 Then
            lResult = SCardConnect(hContext, szReaderName, lMode, lProtocol, hCard, lActiveProtocol)
        End If

        If lResult = 0 Then
            lResult = SCardStatus(hCard, readerName, readerLength, state, lProtocol, byteBuffer, byteBufferSize)
        End If

        If lResult = 0 Then
            DoTransmit(hCard)
        Else
            ListBox1.Items.Clear()
            ListBox2.Items.Clear()
            ListBox1.Items.Add("ERROR" & lResult)
        End If
        lResult = SCardDisconnect(hCard, 0)
        lResult = SCardReleaseContext(hContext)
        GoTo TheEnd
ErrCode:
        Select Case Err()
            Case Else
                MsgBox(Err.Number + vbCrLf + Err.Description)
        End Select
TheEnd:
    End Sub
    Public Sub DoTransmit(ByVal hCard As IntPtr)

        Dim lResult As System.Int32 = 0
        Dim bytCommand(4) As Byte
        Dim byteReadBuffer(255) As Byte
        Dim iReturnlength As IntPtr
        Dim RecvRequest As SCARD_IO_REQUEST
        Dim SendRequest As SCARD_IO_REQUEST
        Dim i As Integer

        bytCommand(0) = TextBox1.Text '&HFF 'FC
        bytCommand(1) = TextBox2.Text '&HCA 'B0
        bytCommand(2) = TextBox3.Text '&H0
        bytCommand(3) = TextBox4.Text '&H0
        bytCommand(4) = TextBox5.Text '&H0


        SendRequest.dwProtocol = 2
        SendRequest.cbPciLength = 8

        RecvRequest.dwProtocol = 2
        RecvRequest.cbPciLength = 8

        iReturnlength = byteReadBuffer.Length
        ListBox1.Items.Clear()
        ListBox2.Items.Clear()
        lResult = SCardTransmit(hCard, SendRequest, bytCommand, bytCommand.Length, IntPtr.Zero, byteReadBuffer, iReturnlength)
        ListBox1.Items.Add(TimeOfDay())
        ListBox2.Items.Add(lResult)
        For i = 0 To 4
            ListBox2.Items.Add(Hex(bytCommand(i)) & "-" & bytCommand(i))
        Next
        For i = 0 To 5
            ListBox1.Items.Add(i + 1 & "-" & byteReadBuffer(i) & "-" & Hex(byteReadBuffer(i)))
        Next

    End Sub


End Class

Open in new window

The SCardTransmit changes the iReturnLength to what length comes back from the command.
In this case it changes to 2 and the byteReadBuffer is "109" = "6D".

Anyone knows why i only get one byte back?
Most Valuable Expert 2012
Top Expert 2014

Commented:
I am unable to offer any assistance with that. I guess you have to keep trying by changing different settings/parameters.

Author

Commented:
You are right.
Thanks for your help CodeCruiser
Most Valuable Expert 2012
Top Expert 2014

Commented:
Glad to help :-)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial