Components All New MacOS Windows Linux iOS
Examples Mac & Win Server Client Guides Statistic FMM Blog Deprecated Old

SmartCard.Init

Initializes a smartcard context.

Component Version macOS Windows Linux Server iOS SDK
SmartCard 6.3 ✅ Yes ✅ Yes ❌ No ✅ Yes, on macOS and Windows ❌ No
MBS( "SmartCard.Init" )

Parameters

none

Result

Returns SmartCard Context ID or error.

Description

Initializes a smartcard context.
Returns the ID for other SmartCard functions.
Please call SmartCard.Release later.

Examples

List readers:

Set Variable [$c; Value:MBS( "SmartCard.Init" )]
Set Variable [$readers; Value:MBS( "SmartCard.ListReaders"; $c )]
Set Variable [$r; Value:MBS( "SmartCard.Release"; $c )]
Show Custom Dialog ["Readers"; $readers]

Connect and query value:

If [MBS( "SmartCard.Available" ) = 1]
    #Start a new session
    Set Variable [$SCContext; Value:MBS( "SmartCard.Init" )]
    If [MBS( "SmartCard.Valid"; $SCContext )]
        #Query list of readers and pick first
        Set Variable [$SCReader; Value:GetValue ( MBS( "SmartCard.ListReaders"; $SCContext) ; 1 )]
        If [MBS("IsError") = 0]
            # Connect to card
            Set Variable [$SCConnect; Value:MBS( "SmartCard.Connect"; $SCContext ; $SCReader ; "Shared"; "any")]
            Set Variable [$PRotocol; Value:MBS( "SmartCard.GetActiveProtocol"; $SCContext)]
            If [$SCConnect = "OK"]
                #Run a query
                Set Variable [$Result; Value:MBS( "SmartCard.Transmit"; $SCContext; "0200000008000000"; "00ca018202"; 512 )]
                #Result is 4 bytes. Starts with C901 for Version 5.0 or C903 for Version 5.3
                Show Custom Dialog ["Version"; $result]
                #Disconnect
                Set Variable [$SCDisconnect; Value:MBS( "SmartCard.Disconnect"; $SCContext; "Leave" )]
            Else
                Show Custom Dialog ["Error"; "No card available."]
            End If
        End If
        #End Card Session
        Set Variable [$SCRelease; Value:MBS( "SmartCard.Release" ; $SCContext )]
    Else
        Show Custom Dialog ["Error"; "Failed to initalize"]
    End If
Else
    Show Custom Dialog ["Error"; "No SmartCard API on this OS."]
End If

Query name from Swiss health card:

# Initialize smart card:
Set Variable [ $$SmartCard ; Value: MBS( "SmartCard.Init" ) ]
If [ MBS("IsError") ]
    Show Custom Dialog [ "Failed to initialize SmartCard" ; $$SmartCard ]
    Exit Script [ Text Result: ]
End If
#
# Connect to a reader
Set Variable [ $reader ; Value: "Identiv SCR3310 uTrust 2700 R" ]
Set Variable [ $r ; Value: MBS( "SmartCard.Connect"; $$SmartCard; $Reader; "Shared"; "any" ) ]
If [ MBS("IsError") ]
    Show Custom Dialog [ "Connect failed" ; $r ]
    Exit Script [ Text Result: ]
End If
#
# Read file from smartcard with APDU protocol.
#
# Read record with name
Set Variable [ $result ; Value: MBS( "SmartCard.ReadFile"; $$SmartCard; "2F06") ]
If [ MBS("IsError") ]
    Set Field [ Card::Status ; $result ]
    Show Custom Dialog [ "Error" ; $result ]
    Exit Script [ Text Result: ]
End If
# Pick result
Set Variable [ $filedata ; Value: Middle ( $result ; 5; Length($result) ) ]
#
# Split in values
Set Variable [ $json ; Value: MBS("SmartCard.SplitValues"; $FileData) ]
# Get item 128, which is full name
Set Variable [ $name ; Value: JSONGetElement ( $json ; "128" ) ]
Set Variable [ $NameList ; Value: Substitute($name; ", "; ¶) ]
Set Variable [ $LastName ; Value: GetValue($nameList; 1) ]
Set Variable [ $FirstName ; Value: GetValue($nameList; 2) ]
#
Show Custom Dialog [ "Name" ; $FirstName & " " & $LastName ]
Set Variable [ $r ; Value: MBS( "SmartCard.Disconnect"; $$SmartCard; "Eject") ]

See also

Example Databases

Blog Entries

This function checks for a license.

Created 13th June 2016, last changed 8th March 2019


SmartCard.HeaderTemplate - SmartCard.ListReaderGroups