Пример #1
0
        /// <inheritdoc />
        public ErrorCode SCardListReaders(IntPtr context, string groups, ref IntPtr readers, ref uint size)
        {
            ErrorCode err;

            unsafe
            {
                // PC/SC lite does not support unicode, only utf8
                // Ensure that encoding of groups if utf8
                var def        = Encoding.Default.GetBytes(groups);
                var utf8Groups = Encoding.Convert(Encoding.Default, Encoding.UTF8, def);

                fixed(uint *psize = &size)
                {
                    fixed(byte *pgroups = utf8Groups)
                    {
                        if (size == AutoAllocate)
                        {
                            err = UnsafePrimitives.SCardListReaders(
                                (void *)context,
                                (char *)pgroups,
                                (char *)readers,
                                psize
                                );
                            if (err == ErrorCode.Success)
                            {
                                var creaders = new char[*psize];
                                fixed(char *pcreaders = creaders)
                                {
                                    err = UnsafePrimitives.SCardListReaders(
                                        (void *)context,
                                        (char *)pgroups,
                                        pcreaders,
                                        psize
                                        );
                                    readers = (IntPtr)pcreaders;
                                }
                            }
                        }
                        else
                        {
                            err = UnsafePrimitives.SCardListReaders(
                                (void *)context,
                                (char *)pgroups,
                                (char *)readers,
                                psize
                                );
                        }
                        size = *psize;
                    }
                }
            }

            return(err);
        }