C#을 서명
[StructLayout(LayoutKind.Sequential)]
public struct SCARD_IO_REQUEST
{
public int dwProtocol;
public int cbPciLength;
}
[DllImport("winscard.dll")]
public static extern int SCardTransmit(int hCard, ref SCARD_IO_REQUEST pioSendRequest, ref byte SendBuff, int SendBuffLen, ref SCARD_IO_REQUEST pioRecvRequest,
ref byte RecvBuff, ref int RecvBuffLen);
코드 예 MIFARE 카드
private SmartcardErrorCode GetUID(ref byte[] UID)
{
byte[] receivedUID = new byte[10];
UnsafeNativeMethods.SCARD_IO_REQUEST request = new UnsafeNativeMethods.SCARD_IO_REQUEST();
request.dwProtocol = 1; //SCARD_PROTOCOL_T1);
request.cbPciLength = System.Runtime.InteropServices.Marshal.SizeOf(typeof(UnsafeNativeMethods.SCARD_IO_REQUEST));
byte[] sendBytes = new byte[] { 0xFF, 0xCA, 0x00, 0x00, 0x04 }; //get UID command for Mifare cards
int outBytes = receivedUID.Length;
int status = SCardTransmit(_hCard, ref request, ref sendBytes[0], sendBytes.Length, ref request, ref receivedUID[0], ref outBytes);
UID = receivedUID.Take(8).ToArray();
return status;
}
표시된 GetUID 방법은에 반환 된 상태의 캐스트가 누락의 읽기 UID에 대한 실제 반환 유형입니다. _hCard 변수에 대한 확인되지 않은 참조도 있습니다. –
몇 가지 문제를 해결 한 후 작동합니다. https://github.com/amree/omnikey-mifare-samples-library/blob/master/Include/winsmcrd.h에 따르면 : SCARD_PROTOCOL_T1 = 2, 1을 2로 변경했습니다. , http://www.pinvoke.net/default.aspx/winscard.scardestablishcontext에서 예제를 사용하여 _hCard (
cardInserted = true;
바로 위에 코드 위에 삽입 됨)를 얻습니다. – Jens