Ejemplo n.º 1
0
        // Database Functions

        /// <summary>
        /// List the reader groups.
        /// </summary>
        /// <param name="aReaderGroupsList">List of reader groups</param>
        /// <returns></returns>
        public int ListReaderGroups(IList aReaderGroupsList)
        {
            // Sanity Check
            if (this.m_fDisposed)
            {
                throw new ObjectDisposedException(base.GetType().FullName);
            }
            // Sanity Check
            if (this.m_hContext == IntPtr.Zero)
            {
                throw new InvalidOperationException();
            }

            int    nRet       = 0;
            IntPtr ptrRMgrMem = IntPtr.Zero;
            uint   nMax       = uint.MaxValue;
            uint   nErrCode   = WinSCard.SCardListReaderGroups(this.m_hContext, ref ptrRMgrMem, ref nMax);

            // Success
            //if (nErrCode == 0)
            if (nErrCode == (uint)SCardReturnValues.SCARD_S_SUCCESS)
            {
                string text = Marshal.PtrToStringAuto(ptrRMgrMem, (int)nMax);
                nRet     = this._SplitMultiString(text, aReaderGroupsList);
                nErrCode = WinSCard.SCardFreeMemory(this.m_hContext, ptrRMgrMem);
                return(nRet);
            }

            // Error
            //if (nErrCode == 2148532270)
            if (nErrCode == (uint)SCardReturnValues.SCARD_E_NO_READERS_AVAILABLE)
            {
                return(0);
            }

            // Exception
            //SCardException.RaiseWin32ResponseCode(nErrCode);
            return(nRet);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// List the installed readers within the given list of groups.
        /// </summary>
        /// <param name="vsGroups">List of reader groups</param>
        /// <param name="aReadersList">Provided list object</param>
        /// <returns></returns>
        public int ListReaders(string[] vsGroups, IList aReadersList)
        {
            // Sanity Check
            if (this.m_fDisposed)
            {
                throw new ObjectDisposedException(base.GetType().FullName);
            }

            // A handle to the context had to be established prior to this.
            // (i.e. by calling EstablishContext)
            if (this.m_hContext == IntPtr.Zero)
            {
                throw new InvalidOperationException();
            }

            // Assume no readers yet
            int    nReaders = 0;
            string text     = null;

            // If a list of readers groups was provided then concatenate them
            // into a multi-string.  If not then all reader groups will be read.
            if ((vsGroups != null) && (vsGroups.Length > 0))
            {
                string[] textArray = vsGroups;
                for (int nIter = 0; nIter < textArray.Length; nIter++)
                {
                    string strTemp = textArray[nIter];
                    text = text + strTemp;
                    text = text + '\0';
                }
                text = text + '\0';
            }

            // NULL pointer - indicates to allocate the buffer internally.
            IntPtr ptrMem = IntPtr.Zero;

            // Autoallocate: SCARD_AUTOALLOCATE (which is -1).  See Winscard.h within the DDK.
            uint nMax = uint.MaxValue;

            // List the reader names within the given groups.
            //
            // If pointer to the readers list (3rd parm) is NULL, SCardListReaders ignores the buffer length supplied
            // in the 4th parm, writes the length of the buffer that would have been returned if this parameter had not
            // been NULL to that passed in pointer, and returns a success code.
            //
            // The 4th parameter receives the actual length of the multi-string structure, including all trailing null
            // characters. If the buffer length is specified as SCARD_AUTOALLOCATE, then mszReaders is converted to
            // a pointer to a byte pointer, and receives the address of a block of memory containing the multi-string
            // structure. This block of memory must be deallocated with SCardFreeMemory.
            uint nErrCode = WinSCard.SCardListReaders(this.m_hContext, text, ref ptrMem, ref nMax);

            // If the call returned OK then can interpret the results.
            if (nErrCode == (uint)SCardReturnValues.SCARD_S_SUCCESS)
            {
                // Marshall the list of readers to a string
                string sReaders = Marshal.PtrToStringAuto(ptrMem, (int)nMax);

                // Split the string into the ArrayList object provided by caller.
                nReaders = this._SplitMultiString(sReaders, aReadersList);

                // Free Autoallocate
                nErrCode = WinSCard.SCardFreeMemory(this.m_hContext, ptrMem);

                // Return the number of items in the ArrayList.
                return(nReaders);
            }

            // Not successfully read
            if (nErrCode == (uint)SCardReturnValues.SCARD_E_NO_READERS_AVAILABLE)
            {
                return(0);
            }

            // Report Error
            //SCardException.RaiseWin32ResponseCode(nErrCode);
            return(nReaders);
        }