Ejemplo n.º 1
0
        public bool ConnectReader()
        {
            // Connect to selected reader using hContext handle and obtain hCard handle
            if (connActive)
            {
                retCode = ModWinsCard.SCardDisconnect(hCard, ModWinsCard.SCARD_UNPOWER_CARD);
            }

            // Shared Connection


            retCode = ModWinsCard.SCardConnect(hContext, cbReaderText, ModWinsCard.SCARD_SHARE_EXCLUSIVE, ModWinsCard.SCARD_PROTOCOL_T1, ref hCard, ref Protocol);
            if (retCode == ModWinsCard.SCARD_S_SUCCESS)
            {
                // displayOut(0,0,"Successful connection to " + cbReader.Text);
                connActive = true;
                return(true);
            }
            else
            {
                // displayOut(0, 0, ModWinsCard.GetScardErrMsg(retCode));
                connActive = false;
                return(false);
            }
        }
Ejemplo n.º 2
0
        public bool InitReader()
        {
            string ReaderList = "" + Convert.ToChar(0);
            int    indx;
            int    pcchReaders = 0;
            string rName       = "";

            // 1. Establish Context
            retCode = ModWinsCard.SCardEstablishContext(ModWinsCard.SCARD_SCOPE_USER, 0, 0, ref hContext);

            if (retCode != ModWinsCard.SCARD_S_SUCCESS)
            {
                //               displayOut(1, retCode, "");

                return(false);
            }

            // 2. List PC/SC card readers installed in the system
            retCode = ModWinsCard.SCardListReaders(this.hContext, null, null, ref pcchReaders);

            if (retCode != ModWinsCard.SCARD_S_SUCCESS)
            {
                //             displayOut(1, retCode, "");

                return(false);
            }

            byte[] ReadersList = new byte[pcchReaders];

            // Fill reader list
            retCode = ModWinsCard.SCardListReaders(this.hContext, null, ReadersList, ref pcchReaders);

            if (retCode != ModWinsCard.SCARD_S_SUCCESS)
            {
                //          displayOut(0, 0, "SCardListReaders Error: " + ModWinsCard.GetScardErrMsg(retCode));
                return(false);
            }
            else
            {
                //       displayOut(0, 0, " ");
            }

            rName = "";
            indx  = 0;

            // Convert reader buffer to string
            while (ReadersList[indx] != 0)
            {
                while (ReadersList[indx] != 0)
                {
                    rName = rName + (char)ReadersList[indx];
                    indx  = indx + 1;
                }

                // Add reader name to list
                if (rName == cbReaderText)
                {
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 3
0
        private int SendAPDUandDisplay(int reqType)
        {
            int    indx;
            string tmpStr;

            pioSendRequest.dwProtocol  = Aprotocol;
            pioSendRequest.cbPciLength = 8;

            // Display Apdu In
            tmpStr = "";
            for (indx = 0; indx <= SendLen - 1; indx++)
            {
                tmpStr = tmpStr + " " + string.Format("{0:X2}", SendBuff[indx]);
            }

            //        displayOut(2, 0, tmpStr);
            retCode = ModWinsCard.SCardTransmit(hCard, ref pioSendRequest, SendBuff, SendLen, ref pioSendRequest, RecvBuff, ref RecvLen);

            if (retCode != ModWinsCard.SCARD_S_SUCCESS)
            {
                //     displayOut(1, retCode, "");
                return(retCode);
            }

            else
            {
                tmpStr = "";
                switch (reqType)
                {
                case 0:
                {
                    for (indx = (RecvLen - 2); indx <= (RecvLen - 1); indx++)
                    {
                        tmpStr = tmpStr + " " + string.Format("{0:X2}", RecvBuff[indx]);
                    }


                    if ((tmpStr).Trim() != "90 00")
                    {
                        //             displayOut(4, 0, "Return bytes are not acceptable.");
                    }

                    break;
                }

                case 1:
                {
                    for (indx = (RecvLen - 2); indx <= (RecvLen - 1); indx++)
                    {
                        tmpStr = tmpStr + string.Format("{0:X2}", RecvBuff[indx]);
                    }


                    if (tmpStr.Trim() != "90 00")
                    {
                        tmpStr = tmpStr + " " + string.Format("{0:X2}", RecvBuff[indx]);
                    }

                    else
                    {
                        tmpStr = "ATR : ";
                        for (indx = 0; indx <= (RecvLen - 3); indx++)
                        {
                            tmpStr = tmpStr + " " + string.Format("{0:X2}", RecvBuff[indx]);
                        }
                    }

                    break;
                }

                case 2:
                {
                    for (indx = 0; indx <= (RecvLen - 1); indx++)
                    {
                        tmpStr = tmpStr + " " + string.Format("{0:X2}", RecvBuff[indx]);
                    }

                    break;
                }
                }

                //  displayOut(3, 0, tmpStr.Trim() + "\r\n");
            }
            return(retCode);
        }