Exemplo n.º 1
0
        void x_CardPresented(string reader, byte[] cardData)
        {
            try
            {
                var hex = SCARD.ToHex(cardData, "");
                var bin = hex2bin(hex.Substring(hex.Length - 8));
                bin = bin.Substring(bin.Length - 26);

                var f = Convert.ToInt32(bin.Substring(1, 8), 2);
                var c = Convert.ToInt32(bin.Substring(9, 16), 2);

                var raw = new BitArray(cardData);
                //var x = raw.Length - 25;

                //BitArray fac = new BitArray(8);
                //BitArray car = new BitArray(16);
                //for (int i = 0; i < 8; i++) { fac[i] = raw[x + i]; }
                //for (int f = 0; f < 16; f++) { car[f] = raw[x + 8 + f]; }
                //var facility = ToNumeral(fac);
                //var cardNum = ToNumeral(car);
            }
            catch (Exception ex)
            {
            }
        }
Exemplo n.º 2
0
        private List <string> GetReaderList(IntPtr hContext, string sGroup)
        {
            uint nStringLength = 0;
            uint result        = SCARD.ListReaders(hContext, sGroup, null, ref nStringLength);

            if (result != SCARD.S_SUCCESS)
            {
                return(null);
            }

            string sReaders = new string(' ', (int)nStringLength);

            result = SCARD.ListReaders(hContext, sGroup, sReaders, ref nStringLength);
            if (result != SCARD.S_SUCCESS)
            {
                return(null);
            }
            List <string> list = new List <string>(sReaders.Split('\0'));

            for (int i = 0; i < list.Count;)
            {
                if (list[i].Trim().Length > 0)
                {
                    i++;
                }
                else
                {
                    list.RemoveAt(i);
                }
            }
            return(list);
        }
Exemplo n.º 3
0
        private void Run()
        {
            IntPtr hContext = IntPtr.Zero;

            try
            {
                uint result = SCARD.EstablishContext(SCARD.SCOPE_SYSTEM, IntPtr.Zero, IntPtr.Zero, ref hContext);
                if (result != SCARD.S_SUCCESS)
                {
                    thread = null;
                    return;
                }

                uint notification_state = SCARD.STATE_UNAWARE;

                while (!bStopThread)    // loop 1 - build list, then iterate
                {
                    SCARD.ReaderState[] states = new SCARD.ReaderState[ReaderCount + 1];
                    states[0] = new SCARD.ReaderState(@"\\?PNP?\NOTIFICATION");
                    states[0].dwCurrentState = notification_state;

                    int iState = 0;
                    if (readerNames != null)
                    {
                        foreach (string s in readerNames)
                        {
                            iState++;
                            states[iState] = new SCARD.ReaderState(s);
                            states[iState].dwCurrentState = SCARD.STATE_UNAWARE;
                        }
                    }

                    while (!bStopThread)    // loop 2 - iterate over list built above
                    {
                        result = SCARD.GetStatusChange(hContext, 250, states, (uint)states.Length);
                        if (result == SCARD.E_TIMEOUT)
                        {
                            continue;
                        }
                        if (result != SCARD.S_SUCCESS)
                        {
                            break;
                        }

                        bool bReaderListChanged = false;
                        for (int i = 0; i < states.Length; i++)
                        {
                            if ((states[i].dwEventState & SCARD.STATE_CHANGED) != 0)
                            {
                                if (i == 0)
                                {
                                    // reader added or removed
                                    notification_state = states[0].dwEventState;

                                    // we want to replace the member in one step, rather than modifying it...
                                    List <string> tmp = GetReaderList(hContext, SCARD.GROUP_ALL_READERS);
                                    if (tmp == null)
                                    {
                                        readerNames.Clear();
                                    }
                                    else
                                    {
                                        readerNames = tmp;
                                    }

                                    if (ListChanged != null)
                                    {
                                        ListChanged();
                                    }
                                    bReaderListChanged = true;
                                }
                                else
                                {
                                    // card added or removed
                                    states[i].dwCurrentState = states[i].dwEventState;

                                    if ((states[i].dwEventState & SCARD.STATE_PRESENT) != 0)
                                    {
                                        byte[] cardData = new byte[states[i].cbATR];
                                        for (int j = 0; j < cardData.Length; j++)
                                        {
                                            cardData[j] = states[i].rgbATR[j];
                                        }
                                        string thisCard = SCARD.ToHex(cardData, "");
                                        string lastCard;
                                        lastCardFound.TryGetValue(states[i].szReader, out lastCard);
                                        if (thisCard != lastCard)
                                        {
                                            lastCardFound[states[i].szReader] = thisCard;
                                            if (CardPresented != null)
                                            {
                                                CardPresented(states[i].szReader, cardData);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        lastCardFound[states[i].szReader] = "";
                                    }
                                }
                            }
                        }

                        if (bReaderListChanged)
                        {
                            break;  // break out of loop 2, and re-build our 'states' list
                        }
                    } // end loop 2
                } // end loop 1
            }
            catch (Exception ex)
            {
                //TODO: error logging
            }
            finally
            {
                if (hContext != IntPtr.Zero)
                {
                    SCARD.ReleaseContext(hContext);
                }
                thread = null;
            }
        }