-
April 23rd, 2013, 05:51 AM
#1
[RESOLVED] ScardTransmit returning junk value
Hi,
I am trying to read data from a smartcard using scardTransmit.
But the data read is always 6D 00 CC CC (Be it for name, expiry date or any other data, always the same.)
This is probably some junk data? Am I missing something or have I done something incorrectly? Please help me.
My code would read as below :
Code:
#include "stdafx.h"
#include <winscard.h>
#include <stdio.h>
#pragma comment(lib, "winscard.lib")
bool transmitFunction(SCARDHANDLE hCard, LPCBYTE pbSend)
{
bool rv = false;
int i = 0;
long Return;
BYTE pbRead[2];
DWORD dwLenR = sizeof(pbRead);
lReturn = SCardTransmit(hCard,
SCARD_PCI_T0,
pbSend,
sizeof(pbSend),
NULL,
(LPBYTE)pbRead,
&dwLenR );
if ( SCARD_S_SUCCESS != lReturn ){
rv = false;
}
else{
while (i < sizeof(pbRead)){
printf("\n pbRecv %02X",pbRead[i]);
i++;
}
rv = true;
}
return rv;
}
bool selectCPRDFEF(SCARDHANDLE hCard)
{
bool rv = FALSE;
BYTE pbSlctBidAp[] = {*********}; //Select BID Applet
rv = transmitFunction(hCard,pbSlctBidAp);
BYTE pbSlctBidDF[] = { *****8}; //Select BID DF
rv = transmitFunction(hCard,pbSlctBidAp);
BYTE pbSlctCPR1EF[] = { ******}; //Select card # 1 EF
rv = transmitFunction(hCard,pbSlctCPR1EF);
return rv;
}
bool getCPRFirstNameEn(SCARDHANDLE hCard)
{
bool rv = FALSE;
int i = 0;
BYTE pbGetFirstNameEn[] = { *******}; // Get English First Name
BYTE pbRead[34] ;
DWORD dwLenR = sizeof(pbRead);
long lReturn = SCardTransmit(hCard, SCARD_PCI_T0,pbGetFirstNameEn,
sizeof(pbGetFirstNameEn),NULL,pbRead,&dwLenR );
if ( SCARD_S_SUCCESS != lReturn )
{
rv = false;
}
else
{
while (i < sizeof(pbRead))
{
printf("B pbRecv %02X ",pbRead[i]);
i++;
}
rv = true;
}
return rv;
}
bool status(SCARDHANDLE hCard,SCARDCONTEXT hSC)
{
long lReturn;
BYTE bAttr[32];
DWORD dwState, dwProtocol;
bool rv = TRUE;
LPCTSTR szReaderOut = NULL;
/*Determine the Status of the smart card*/
lReturn = SCardStatus(hCard,
(LPWSTR)szReaderOut,
NULL,
&dwState,
&dwProtocol,
(LPBYTE)&bAttr,
NULL);
if ( SCARD_S_SUCCESS != lReturn ){
rv = FALSE;
}
else
{
// Examine retrieved status elements.
switch ( dwState ){
case SCARD_ABSENT:
case SCARD_POWERED:
case SCARD_PRESENT:
case SCARD_SWALLOWED:
printf("Card can't be used. Releasing context.");
lReturn = SCardReleaseContext(hSC);
rv = FALSE;
break;
case SCARD_SPECIFIC:
printf("Card has specific communication protocols set");
break;
default:
printf("Unknown or unexpected card state");
rv = FALSE;
break;
}}
return rv;
}
DWORD WINAPI ReaderUSBThread(LPVOID lpParameter) {
SCARDCONTEXT hSC = NULL;
SCARDHANDLE hCard= NULL;
bool fret = true;
LPCTSTR szReader=L"CASTLES EZ100PU 0";
LONG lReturn;
DWORD dwAP;
HRESULT hr = CoInitialize(NULL);
try
{
/*Establishes the Context*/
lReturn = SCardEstablishContext(SCARD_SCOPE_USER,NULL,NULL,&hSC );
if ( SCARD_S_SUCCESS != lReturn )
printf("\nNOT SCARD_S_SUCCESS");
/*Establish connection with Smart Card*/
lReturn = SCardConnect( hSC,
(LPCTSTR)szReader,
SCARD_SHARE_SHARED,
SCARD_PROTOCOL_T0 ,
&hCard,
&dwAP );
if ( SCARD_S_SUCCESS != lReturn ){
if (lReturn == SCARD_W_REMOVED_CARD || lReturn == SCARD_E_NO_SMARTCARD) {
printf("\nSmart Card not inserted or it has been removed.");
lReturn = SCardDisconnect(hCard,SCARD_RESET_CARD);
return false;
}
}
bool rv = status(hCard,hSC);
if(TRUE != rv){
return rv;
}
selectCPRDFEF(hCard);
getCPRFirstNameEn(hCard);
// Terminates the connection
lReturn = SCardDisconnect(hCard,SCARD_RESET_CARD);
if (lReturn != SCARD_S_SUCCESS)
{ // do some error handling
}
} catch(...) {
printf("\nStream ends with error");
}
lReturn = SCardReleaseContext(hSC);
if (lReturn != SCARD_S_SUCCESS) {
printf("\nRelease Context Failure- %ld ",lReturn);
}
return 0;
}
int main()
{
DWORD dwThreadId = 0;
CreateThread(NULL, NULL, ReaderUSBThread,NULL ,NULL,&dwThreadId );
return 0;
}
SCardEstablishContext and SCardConnect are succesful.
Prints for pbRead in getCPRFirstNameEn() is : 6D 00 CC CC
Note : I have edited the above to include actual code.
I have also tried sending different APDU commands (for getting second name, expiry date etc) , but the print for all is the same.
Last edited by RomP; January 14th, 2014 at 09:24 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|