Exemple #1
0
        /// <inheritdoc />
        public ErrorCode SCardTransmit(IntPtr card, ref AbstractIoRequest sendPci, byte[] sendBuffer, uint sendSize, ref AbstractIoRequest recvPci, ref byte[] recvBuffer, ref uint recvSize)
        {
            ErrorCode ret;

            var ptrsendPci = Marshal.AllocHGlobal(Marshal.SizeOf(((IoRequest)sendPci).ScIoRequest));

            Marshal.StructureToPtr(((IoRequest)sendPci).ScIoRequest, ptrsendPci, true);
            var ptrrecvPci = Marshal.AllocHGlobal(Marshal.SizeOf(((IoRequest)recvPci).ScIoRequest));

            Marshal.StructureToPtr(((IoRequest)recvPci).ScIoRequest, ptrrecvPci, true);

            unsafe
            {
                if (recvSize == AutoAllocate)
                {
                    // winscard.dll supports SCARD_AUTOALLOCATE only since Windows vista; winscard.dll is able to propose l recvSize to be used with all versions (XP+)
                    // pcsclite does not support recvBuffer = null, so no automatic discovery of recvSize
                    // For more portability: Wrapper don't use the native winscard.dll's SCARD_AUTOALLOCATE
                    recvSize   = DefaultBufferSize;
                    recvBuffer = new byte[recvSize];
                    fixed(byte *psendBuffer = sendBuffer)
                    {
                        fixed(uint *precvSize = &recvSize)
                        {
                            fixed(byte *precvBuffer = recvBuffer)
                            {
                                ret = UnsafePrimitives.SCardTransmit(
                                    (void *)card,
                                    (void *)ptrsendPci,
                                    psendBuffer,
                                    sendSize,
                                    (void *)ptrrecvPci,
                                    precvBuffer,
                                    precvSize
                                    );
                            }
                        }
                    }

                    if (ret == ErrorCode.Success)
                    {
                        Array.Resize(ref recvBuffer, (int)recvSize);
                    }
                }
                else
                {
                    //TODO Seems to be problems with pcsclite in this case...
                    fixed(byte *psendBuffer = sendBuffer)
                    {
                        fixed(uint *precvSize = &recvSize)
                        {
                            fixed(byte *precvBuffer = recvBuffer)
                            {
                                ret = UnsafePrimitives.SCardTransmit(
                                    (void *)card,
                                    (void *)ptrsendPci,
                                    psendBuffer,
                                    sendSize,
                                    (void *)ptrrecvPci,
                                    precvBuffer,
                                    precvSize
                                    );
                            }
                        }
                    }
                }
            }

            Marshal.FreeHGlobal(ptrsendPci);
            Marshal.FreeHGlobal(ptrrecvPci);

            return(ret);
        }