コード例 #1
0
ファイル: PcscHelper.cs プロジェクト: vebin/NewPOS
        public static bool CardPresent(string readerName)
        {
            int    retCode;
            IntPtr hContext = new IntPtr();

            retCode = PcscProvider.SCardEstablishContext(PcscProvider.SCARD_SCOPE_USER, 0, 0, ref hContext);
            if (retCode != PcscProvider.SCARD_S_SUCCESS)
            {
                throw new Exception("Unable to establish context", new Exception(PcscProvider.GetScardErrMsg(retCode)));
            }

            PcscProvider.SCARD_READERSTATE state = new PcscProvider.SCARD_READERSTATE();
            state.szReader = readerName;

            retCode = PcscProvider.SCardGetStatusChange(hContext, 3000, ref state, 1);
            if (retCode != 0)
            {
                throw new Exception("Unable to get status", new Exception(PcscProvider.GetScardErrMsg(retCode)));
            }
            else
            {
                //state.dwCurrentState >>= 32;
                if (((Int32)(state.dwEventState) & PcscProvider.SCARD_STATE_PRESENT) == PcscProvider.SCARD_STATE_PRESENT)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
コード例 #2
0
        public void sendCardControl()
        {
            byte[] sendBuff, recvbuff;
            int    sendLen, recvLen, returnCode, actualLength = 0;

            PcscProvider.SCARD_IO_REQUEST ioRequest;

            ioRequest.dwProtocol  = pdwActiveProtocol_;
            ioRequest.cbPciLength = 8;

            if (apduCommand.data == null)
            {
                throw new Exception("No data specified");
            }

            sendBuff = new byte[apduCommand.data.Length];
            recvLen  = apduCommand.lengthExpected;

            Array.Copy(apduCommand.data, 0, sendBuff, 0, apduCommand.data.Length);

            sendLen = sendBuff.Length;

            apduCommand.statusWord = new byte[2];
            recvbuff = new byte[recvLen];

            sendCommandTriggerEvent(new TransmitApduEventArg(sendBuff));
            returnCode = PcscProvider.SCardControl(hCard_,
                                                   operationControlCode,
                                                   sendBuff,
                                                   sendLen,
                                                   recvbuff,
                                                   recvbuff.Length,
                                                   ref actualLength);

            if (returnCode == 0)
            {
                apduCommand.actualLengthReceived = actualLength;

                receivedCommandTriggerEvent(new TransmitApduEventArg(recvbuff.Take(actualLength).ToArray()));

                apduCommand.response = new byte[actualLength];
                Array.Copy(recvbuff, 0, apduCommand.response, 0, actualLength);

                if (actualLength > 1)
                {
                    Array.Copy(recvbuff, actualLength - 2, apduCommand.statusWord, 0, 2);
                }

                //if (apdu.actualLengthReceived >= 2)
                //{
                //    apdu.receiveData = new byte[actualLength - 2];
                //    Array.Copy(recvbuff, 0, apdu.receiveData, 0, actualLength - 2);
                //}
            }
            else
            {
                throw new PcscException(returnCode);
            }
        }
コード例 #3
0
        public void disconnect()
        {
            int returnValue = PcscProvider.SCardDisconnect(hCard_, PcscProvider.SCARD_UNPOWER_CARD);

            if (returnValue != PcscProvider.SCARD_S_SUCCESS)
            {
                throw new PcscException(returnValue);
            }
        }
コード例 #4
0
        void releaseContext()
        {
            int retCode = PcscProvider.SCardReleaseContext(hContext_);

            if (retCode != PcscProvider.SCARD_S_SUCCESS)
            {
                throw new PcscException(retCode);
            }
        }
コード例 #5
0
        void establishContext()
        {
            int retCode;

            retCode = PcscProvider.SCardEstablishContext(PcscProvider.SCARD_SCOPE_USER, 0, 0, ref hContext_);
            if (retCode != PcscProvider.SCARD_S_SUCCESS)
            {
                throw new Exception("Unable to establish context - " + PcscProvider.GetScardErrMsg(retCode));
            }
        }
コード例 #6
0
ファイル: PcscHelper.cs プロジェクト: vebin/NewPOS
        public static List <string> getAllReaders(string readerNameContains)
        {
            int ReaderCount = 255;

            byte[] retData;
            byte[] sReaderGroup = null;
            string readerStr    = string.Empty;

            string[]      readerList = null;
            List <string> finalReaderList;
            int           retCode;
            IntPtr        hContext = new IntPtr();

            retCode = PcscProvider.SCardEstablishContext(PcscProvider.SCARD_SCOPE_USER, 0, 0, ref hContext);
            if (retCode != PcscProvider.SCARD_S_SUCCESS)
            {
                throw new PcscException(retCode);
            }

            retCode = PcscProvider.SCardListReaders(hContext, null, null, ref ReaderCount);
            if (retCode != PcscProvider.SCARD_S_SUCCESS)
            {
                throw new PcscException(retCode);
            }

            retData = new byte[ReaderCount];

            retCode = PcscProvider.SCardListReaders(hContext, sReaderGroup, retData, ref ReaderCount);
            if (retCode != PcscProvider.SCARD_S_SUCCESS)
            {
                if (retCode != PcscProvider.SCARD_S_SUCCESS)
                {
                    throw new PcscException(retCode);
                }
            }


            readerStr  = System.Text.ASCIIEncoding.ASCII.GetString(retData).Trim('\0');
            readerList = readerStr.Split('\0');

            finalReaderList = new List <string>();
            foreach (string rdr in readerList)
            {
                if (!rdr.Contains(readerNameContains))
                {
                    continue;
                }

                finalReaderList.Add(rdr);
            }

            return(finalReaderList);
        }
コード例 #7
0
        public void connect(string readerName, int preferedProtocol, int shareMode)
        {
            int returnCode;

            returnCode = PcscProvider.SCardConnect(hContext_, readerName, shareMode, preferedProtocol, ref hCard_, ref pdwActiveProtocol_);
            if (returnCode != PcscProvider.SCARD_S_SUCCESS)
            {
                lastError = returnCode;
                throw new PcscException(returnCode);
            }


            shareMode_  = shareMode;
            pProtocol_  = preferedProtocol;
            _readerName = readerName;
        }
コード例 #8
0
ファイル: PcscHelper.cs プロジェクト: vebin/NewPOS
        public static byte[] getAtr(string readerName)
        {
            byte[] atr;
            int    retCode;
            IntPtr hContext = new IntPtr();

            try
            {
                PcscProvider.SCARD_READERSTATE state = new PcscProvider.SCARD_READERSTATE();
                state.szReader = readerName;

                retCode = PcscProvider.SCardEstablishContext(PcscProvider.SCARD_SCOPE_USER, 0, 0, ref hContext);
                if (retCode != PcscProvider.SCARD_S_SUCCESS)
                {
                    return(null);
                }

                retCode = PcscProvider.SCardGetStatusChange(hContext, 3000, ref state, 1);
                if (retCode != 0)
                {
                    atr = null;
                }
                else
                {
                    if ((state.dwEventState & PcscProvider.SCARD_STATE_PRESENT) == PcscProvider.SCARD_STATE_PRESENT)
                    {
                        atr = state.rgbAtr.Take(state.cbAtr).ToArray();
                    }
                    else
                    {
                        atr = null;
                    }
                }

                PcscProvider.SCardReleaseContext(hContext);

                return(atr);
            }
            catch (Exception)
            {
                return(null);
            }
        }
コード例 #9
0
ファイル: PcscHelper.cs プロジェクト: vebin/NewPOS
        public static string[] getAllReaders()
        {
            int ReaderCount = 255;

            byte[] retData;
            byte[] sReaderGroup = null;
            string readerStr    = string.Empty;

            string[] readerList = null;
            int      retCode;
            IntPtr   hContext = new IntPtr();

            retCode = PcscProvider.SCardEstablishContext(PcscProvider.SCARD_SCOPE_USER, 0, 0, ref hContext);
            if (retCode != PcscProvider.SCARD_S_SUCCESS)
            {
                throw new PcscException(retCode);
            }

            retCode = PcscProvider.SCardListReaders(hContext, null, null, ref ReaderCount);
            if (retCode != PcscProvider.SCARD_S_SUCCESS)
            {
                throw new PcscException(retCode);
            }

            retData = new byte[ReaderCount];

            retCode = PcscProvider.SCardListReaders(hContext, sReaderGroup, retData, ref ReaderCount);
            if (retCode != PcscProvider.SCARD_S_SUCCESS)
            {
                if (retCode != PcscProvider.SCARD_S_SUCCESS)
                {
                    throw new PcscException(retCode);
                }
            }


            readerStr  = System.Text.ASCIIEncoding.ASCII.GetString(retData).Trim('\0');
            readerList = readerStr.Split('\0');

            return(readerList);
        }
コード例 #10
0
        public string[] getReaderList()
        {
            byte[]   returnData;
            byte[]   sReaderGroup = null;
            string[] readerList   = new string[0];
            string   readerString = string.Empty;
            int      returnCode;
            IntPtr   hContext    = new IntPtr();
            int      readerCount = 255;

            returnCode = PcscProvider.SCardEstablishContext(PcscProvider.SCARD_SCOPE_USER, 0, 0, ref hContext);
            if (returnCode != PcscProvider.SCARD_S_SUCCESS)
            {
                lastError = returnCode;
                return(readerList);
            }

            returnCode = PcscProvider.SCardListReaders(hContext_, null, null, ref readerCount);
            if (returnCode != PcscProvider.SCARD_S_SUCCESS)
            {
                lastError = returnCode;
                return(readerList);
            }

            returnData = new byte[readerCount];

            returnCode = PcscProvider.SCardListReaders(hContext_, sReaderGroup, returnData, ref readerCount);
            if (returnCode != PcscProvider.SCARD_S_SUCCESS)
            {
                return(readerList);
            }


            readerString = System.Text.ASCIIEncoding.ASCII.GetString(returnData).Trim('\0');
            readerList   = readerString.Split('\0');

            return(readerList);
        }
コード例 #11
0
        public void sendCommand()
        {
            byte[] sendBuff, recvBuff;
            int    sendLen, recvLen, returnCode;

            PcscProvider.SCARD_IO_REQUEST ioRequest;

            ioRequest.dwProtocol  = pdwActiveProtocol_;
            ioRequest.cbPciLength = 8;

            if (apduCommand.data == null)
            {
                sendBuff = new byte[5];
            }
            else
            {
                sendBuff = new byte[5 + apduCommand.data.Length];
            }


            recvLen = apduCommand.lengthExpected + 2;


            Array.Copy(new byte[] { apduCommand.instructionClass, apduCommand.instructionCode, apduCommand.parameter1, apduCommand.parameter2, apduCommand.parameter3 }, sendBuff, 5);

            if (apduCommand.data != null)
            {
                Array.Copy(apduCommand.data, 0, sendBuff, 5, apduCommand.data.Length);
            }

            sendLen = sendBuff.Length;

            apduCommand.statusWord = new byte[2];
            recvBuff = new byte[recvLen];

            sendCommandTriggerEvent(new TransmitApduEventArg(sendBuff));
            returnCode = PcscProvider.SCardTransmit(hCard_,
                                                    ref ioRequest,
                                                    sendBuff,
                                                    sendLen,
                                                    ref ioRequest,
                                                    recvBuff,
                                                    ref recvLen);
            if (returnCode == 0)
            {
                receivedCommandTriggerEvent(new TransmitApduEventArg(recvBuff.Take(recvLen).ToArray()));
                if (recvLen > 1)
                {
                    Array.Copy(recvBuff, recvLen - 2, apduCommand.statusWord, 0, 2);
                }

                if (recvLen > 2)
                {
                    apduCommand.response = new byte[recvLen - 2];
                    Array.Copy(recvBuff, 0, apduCommand.response, 0, recvLen - 2);
                }
            }
            else
            {
                throw new PcscException(returnCode);
            }
        }