Question
My Project structure
myApp
-->WCFSerLibrary
-->ClassLib1
-->ClassLib2 // This one uses winscard.dll
I wanted to communicate with a Smartcard reaer attached.
Four steps I follow for that are
- SCardEstablishContext
- Get the reader name via SCardListReaders
- SCardConnect
- Read with SCardTransmit
The first three steps execute succesfully but when i try to do a SCardTransmit it gives back me response code of 87.
Second side, The above mentioned scenario is when I call it from WCF service. Now I use the same ClassLib2 in a Windows FOrm application it communicates properly.
retCode = ModWinsCard.SCardTransmit(hCard, ref pioSendRequest, ref SendBuff[0], sendLen, ref pioSendRequest, ref RecvBuff[0], ref recvLen);
where as in ModWinsCard class
[DllImport("winscard.dll"l)]
public static extern Int64 SCardTransmit(UInt64 hCard, ref SCARD_IO_REQUEST pioSendRequest, ref byte SendBuff, int SendBuffLen, ref SCARD_IO_REQUEST pioRecvRequest, ref byte RecvBuff, ref int RecvBuffLen);
Solution
87 is invalid parameter. The method returns an int not a long. Also you are passing in only the first item in the in and out buffers. Should use IntPtr for the card handle.
LONG WINAPI SCardTransmit(
_In_ SCARDHANDLE hCard,
_In_ LPCSCARD_IO_REQUEST pioSendPci,
_In_ LPCBYTE pbSendBuffer,
_In_ DWORD cbSendLength,
_Inout_opt_ LPSCARD_IO_REQUEST pioRecvPci,
_Out_ LPBYTE pbRecvBuffer,
_Inout_ LPDWORD pcbRecvLength
);
Maybe try the pinvoke signature below instead?
[DllImport("winscard.dll"l)]
public static extern int SCardTransmit(
IntPtr hCard,
ref SCARD_IO_REQUEST pioSendRequest,
byte[] SendBuff,
int SendBuffLen,
ref SCARD_IO_REQUEST pioRecvRequest,
IntPtr RecvBuff, // Copy with marshal.copy to a managed array after call.
ref int RecvBuffLen);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow