Exemplo n.º 1
0
        public static string StringFromConnectionType(ChipConnection connectionType)
        {
            switch (connectionType)
            {
            case ChipConnection.contact: return(scard_strings.SMARTCARD_PROTOCOL_T0_OR_T1);

            case ChipConnection.contactless: return(scard_strings.SMARTCARD_PROTOCOL_CONTACTLESS);
            }
            throw new Exception("expected one of contact or contactless");
        }
Exemplo n.º 2
0
        /// <summary>
        /// wrapper for the win32 ::SCardConnect() function.
        /// see http://msdn.microsoft.com/en-us/library/windows/desktop/aa379473(v=vs.85).aspx
        /// </summary>
        /// <param name="connectionType">
        /// one of SCard.contact, SCard.contactless
        /// </param>
        /// <param name="protocol">
        /// populated with one of these values:
        ///     SCARD_PROTOCOL_UNDEFINED = 0,
        ///     SCARD_PROTOCOL_T0 = 0x1,
        ///     SCARD_PROTOCOL_T1 = 0x2,
        ///     SCARD_PROTOCOL_RAW = 0x10000,
        ///     SCARD_PROTOCOL_Tx = 0x3,
        ///     SCARD_PROTOCOL_DEFAULT = 0x80000000,
        ///     SCARD_PROTOCOL_OPTIMAL = 0
        /// </param>
        /// <returns>a smartcard error code as defined in WinError.H; SCARD_S_SUCCESS if no error.</returns>
        public long SCardConnect(ChipConnection connectionType, ref uint protocol)
        {
            long scardResult = (long)scard_error.SCARD_F_UNKNOWN_ERROR;

            try {
                var connectionTypeString = StringFromConnectionType(connectionType);
                var xmlFormat            = strings.SMARTCARD_CONNECT_XML;
                var input     = string.Format(xmlFormat, connectionTypeString);
                var resultXml = _bidiSpl.SetPrinterData(strings.SMARTCARD_CONNECT, input);
                var smartcardResponseValues = Util.ParseSmartcardResponseXML(resultXml);

                protocol    = (uint)ProtocolFromString(smartcardResponseValues._protocol);
                scardResult = ErrorFromString(smartcardResponseValues._status);
            }
            catch (scardException e) {
                Console.WriteLine("{0}: ; hresult: 0x{0:x}", e.Message, e.hresult);
            }
            return(scardResult);
        }