Esempio n. 1
0
        /// <inheritdoc />
        public ErrorCode SCardListReaders(IntPtr context, string groups, ref IntPtr readers, ref uint size)
        {
            ErrorCode err;

            unsafe
            {
                fixed(uint *psize = &size)
                {
                    fixed(char *pgroups = groups)
                    {
                        if (size == AutoAllocate)
                        {
                            char *preaders;
                            err = UnsafePrimitives.SCardListReaders(
                                (void *)context,
                                pgroups,
                                (char *)&preaders,
                                psize
                                );
                            if (err == ErrorCode.Success)
                            {
                                readers = UnsafePrimitives.CharPointerToIntPtr(preaders, *psize);
                                UnsafePrimitives.SCardFreeMemory((void *)context, preaders);
                            }
                        }
                        else
                        {
                            err = UnsafePrimitives.SCardListReaders(
                                (void *)context,
                                pgroups,
                                (char *)readers,
                                psize
                                );
                        }
                        size = *psize;
                    }
                }
            }

            return(err);
        }
Esempio n. 2
0
        /// <inheritdoc />
        public ErrorCode SCardStatus(IntPtr card, ref IntPtr readerName, ref uint readerNameSize, ref State status, ref Protocol protocol, ref IntPtr atr, ref uint atrSize)
        {
            ErrorCode ret;

            unsafe
            {
                var ustatus   = (uint)status;
                var uprotocol = (uint)protocol;
                fixed(uint *preaderNameSize = &readerNameSize)
                {
                    fixed(uint *patrSize = &atrSize)
                    {
                        if (readerNameSize == AutoAllocate && atrSize == AutoAllocate)
                        {
                            char *preaderName;
                            byte *patr;
                            ret = UnsafePrimitives.SCardStatus(
                                (void *)card,
                                (char *)&preaderName,
                                preaderNameSize,
                                &ustatus,
                                &uprotocol,
                                (byte *)&patr,
                                patrSize
                                );
                            if (ret == ErrorCode.Success)
                            {
                                readerName = UnsafePrimitives.CharPointerToIntPtr(preaderName, *preaderNameSize);
                                UnsafePrimitives.SCardFreeMemory((void *)ActiveContext, preaderName);
                                atr = UnsafePrimitives.BytePointerToIntPtr(patr, *patrSize);
                                UnsafePrimitives.SCardFreeMemory((void *)ActiveContext, patr);
                            }
                        }
                        else if (readerNameSize == AutoAllocate && atrSize != AutoAllocate)
                        {
                            char *preaderName;
                            ret = UnsafePrimitives.SCardStatus(
                                (void *)card,
                                (char *)&preaderName,
                                preaderNameSize,
                                &ustatus,
                                &uprotocol,
                                (byte *)atr,
                                patrSize
                                );
                            if (ret == ErrorCode.Success)
                            {
                                readerName = UnsafePrimitives.CharPointerToIntPtr(preaderName, *preaderNameSize);
                                UnsafePrimitives.SCardFreeMemory((void *)ActiveContext, preaderName);
                            }
                        }
                        else if (readerNameSize != AutoAllocate && atrSize == AutoAllocate)
                        {
                            byte *patr;
                            ret = UnsafePrimitives.SCardStatus(
                                (void *)card,
                                (char *)readerName,
                                preaderNameSize,
                                &ustatus,
                                &uprotocol,
                                (byte *)&patr,
                                patrSize
                                );
                            atr = UnsafePrimitives.BytePointerToIntPtr(patr, *patrSize);
                            UnsafePrimitives.SCardFreeMemory((void *)ActiveContext, patr);
                        }
                        else
                        {
                            ret = UnsafePrimitives.SCardStatus(
                                (void *)card,
                                (char *)readerName,
                                (uint *)readerNameSize,
                                &ustatus,
                                &uprotocol,
                                (byte *)atr,
                                (uint *)atrSize
                                );
                        }
                        status         = (State)ustatus;
                        protocol       = (Protocol)uprotocol;
                        readerNameSize = *preaderNameSize;
                        atrSize        = *patrSize;
                    }
                }
            }
            return(ret);
        }