www.pudn.com > contactlessdemovc.zip > contactlessdemovcdlg.cpp, change:2008-06-26,size:67960b


// ContactlessDemoVCDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "ContactlessDemoVC.h" 
#include "ContactlessDemoVCDlg.h" 
 
#ifdef _DEBUG 
   #define new DEBUG_NEW 
   #undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
#define Card_Absent  0x00 
#define Card_Present 0x01 
#define Card_Changed 0x02 
/*****************************************************************************/ 
/*           ISO standard CS(CCCC****) 
******************************************************************************/ 
#define ISO14443A_123	      0x01 
#define ISO14443A_1234		   0x02 
#define ISO15693_12ICL			0x03 
#define ISO15693_123 			0x04 
#define ISO14443B_12STM 		0x05 
#define ISO14443B_123   		0x06 
#define ISO14443B_1234 			0x07 
#define ISO15693_12ICODE1  	0x08 
#define ISO15693_12ICODEEPC  	0x09 
 
 
/*****************************************************************************/ 
/*           Chip Type CS(****SSSS) 
******************************************************************************/ 
#define UNKNOWN_CHIP				0x00 
//ISO 14443A_123 
#define MIFARE_ST1K				0x10 
#define MIFARE_ST4K				0x20 
#define MIFARE_ULIT				0x30 
#define SLE55R_XXXX				0x70	 
 
//ISO14443A_1234 
#define MIFARE_DESF				0x40 
#define MIFARE_PRX				0x50 
#define SLE66C_320P				0x60	 
 
//ISO14443B_123 
#define AT88RF020				0x10 
#define AT88SC6416CRF			0x20 
//ISO14443B_12 
#define SR176					0xE0 
#define SRIX4K					0xF0 
 
//ISO15693_123 
#define SRF55V10P				0x10 
#define SRF55V02P				0x20 
#define SRF55V10S				0x30 
#define SRF55V02S				0x40 
#define TAG_IT					0x90 
#define LRI512					0xA0 
#define ICODESLI				0xB0 
#define SRF55XXX				0xC0 
#define TEMPSENS				0xD0 
 
 
//ISO15693_12ICL 
#define INSIDECL_XXXX			0x80	 
 
 
//ICODE1 Card 
#define ICODE1CARD				0x10 
 
 
 
///////////////////////////////////////////////////////////////////////////// 
// CAboutDlg dialog used for App About 
 
class CAboutDlg : public CDialog 
   { 
public: 
   CAboutDlg(); 
 
// Dialog Data 
   //{{AFX_DATA(CAboutDlg) 
   enum 
      { 
      IDD = IDD_ABOUTBOX 
      }; 
   //}}AFX_DATA 
 
   // ClassWizard generated virtual function overrides 
   //{{AFX_VIRTUAL(CAboutDlg) 
protected: 
   virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support 
   //}}AFX_VIRTUAL 
 
// Implementation 
protected: 
   //{{AFX_MSG(CAboutDlg) 
   //}}AFX_MSG 
   DECLARE_MESSAGE_MAP() 
   }; 
 
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 
   { 
   //{{AFX_DATA_INIT(CAboutDlg) 
   //}}AFX_DATA_INIT 
   } 
 
void CAboutDlg::DoDataExchange(CDataExchange* pDX) 
   { 
   CDialog::DoDataExchange(pDX); 
   //{{AFX_DATA_MAP(CAboutDlg) 
   //}}AFX_DATA_MAP 
   } 
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
//{{AFX_MSG_MAP(CAboutDlg) 
// No message handlers 
//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CContactlessDemoVCDlg dialog 
 
CContactlessDemoVCDlg::CContactlessDemoVCDlg(CWnd* pParent /*=NULL*/) 
: CDialog(CContactlessDemoVCDlg::IDD, pParent) 
   { 
   //{{AFX_DATA_INIT(CContactlessDemoVCDlg) 
   m_strReaderName = _T(""); 
   m_strMifKeyToReader = _T(""); 
   m_longBlockNr = 0; 
   m_strAuthentKey = _T(""); 
   m_intAuthentMode = -1; 
   m_intAuthentKeyOption = -1; 
   m_intAccessOption = -1; 
   m_strKeyNrToReader = _T(""); 
   m_strMifAuthentKeyNr = _T(""); 
   m_strDataRead = _T(""); 
   m_strDataToWrite = _T(""); 
   m_strIncrementValue = _T(""); 
   m_strDecrementValue = _T(""); 
   m_strOutPutList = _T(""); 
   m_intMifTrOption = -1; 
   m_strMifTrKeyNr = _T(""); 
   m_strTrKeyNoToReader = _T(""); 
   m_intTrKeyOptionS = -1; 
   m_strTrTrKeyNo = _T(""); 
   m_strTrKeyToReader = _T(""); 
   m_dwNByteSend = 0; 
   m_strByteSend = _T(""); 
   m_strByteReceive = _T(""); 
   m_dwNByteReceive = 0; 
   m_nWriteByte = 0; 
   //}}AFX_DATA_INIT 
   // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
   m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
   } 
 
void CContactlessDemoVCDlg::DoDataExchange(CDataExchange* pDX) 
   { 
   CDialog::DoDataExchange(pDX); 
   //{{AFX_DATA_MAP(CContactlessDemoVCDlg) 
   DDX_Control(pDX, IDC_LEDERROR, m_ledError); 
   DDX_Control(pDX, IDC_LEDSUCCESS, m_ledSuccess); 
   DDX_Control(pDX, IDC_ATRLIST, m_ctrAtrList); 
   DDX_Control(pDX, IDC_CARDNAMELIST, m_ctrCardNameList); 
   DDX_Control(pDX, IDC_UIDLIST, m_ctrUidList); 
   DDX_Control(pDX, IDC_LISTOUTPUT, m_ctrOutput); 
   DDX_Control(pDX, IDC_LISTREADER, m_ctrReaderName); 
   DDX_LBString(pDX, IDC_LISTREADER, m_strReaderName); 
   DDX_Text(pDX, IDC_MIFKEYTOREADER, m_strMifKeyToReader); 
   DDV_MaxChars(pDX, m_strMifKeyToReader, 16); 
   DDX_Text(pDX, IDC_AUTHENTBLOCKNR, m_longBlockNr); 
   DDV_MinMaxLong(pDX, m_longBlockNr, 0, 255); 
   DDX_Text(pDX, IDC_AUTHENTKEY, m_strAuthentKey); 
   DDV_MaxChars(pDX, m_strAuthentKey, 12); 
   DDX_Radio(pDX, IDC_MODEA, m_intAuthentMode); 
   DDX_Radio(pDX, IDC_ACCESSOPTIONKEYNR, m_intAccessOption); 
   DDX_CBString(pDX, IDC_KEYNRTOREADER, m_strKeyNrToReader); 
   DDX_CBString(pDX, IDC_MIFAUTHENTKEYNR, m_strMifAuthentKeyNr); 
   DDX_Text(pDX, IDC_DATAREAD, m_strDataRead); 
   DDX_Text(pDX, IDC_DATATOWRITE, m_strDataToWrite); 
   DDV_MaxChars(pDX, m_strDataToWrite, 32); 
   DDX_Text(pDX, IDC_INCREMENTVAL, m_strIncrementValue); 
   DDV_MaxChars(pDX, m_strIncrementValue, 8); 
   DDX_Text(pDX, IDC_DECREMENTVAL, m_strDecrementValue); 
   DDV_MaxChars(pDX, m_strDecrementValue, 8); 
   DDX_LBString(pDX, IDC_LISTOUTPUT, m_strOutPutList); 
   DDX_Radio(pDX, IDC_MIFKEYOPTIONS, m_intMifTrOption); 
   DDX_CBString(pDX, IDC_MIFTRKEYNR, m_strMifTrKeyNr); 
   DDX_CBString(pDX, IDC_TRKEYNRTOREADER, m_strTrKeyNoToReader); 
   DDX_Radio(pDX, IDC_TRKEYOPTIONS, m_intTrKeyOptionS); 
   DDX_CBString(pDX, IDC_TRTRKEYNR, m_strTrTrKeyNo); 
   DDX_Text(pDX, IDC_TRKEYTOREADER, m_strTrKeyToReader); 
   DDV_MaxChars(pDX, m_strTrKeyToReader, 32); 
   DDX_Text(pDX, IDC_NBYTESEND, m_dwNByteSend); 
   DDV_MinMaxDWord(pDX, m_dwNByteSend, 0, 253); 
   DDX_Text(pDX, IDC_BYTESEND, m_strByteSend); 
   DDX_Text(pDX, IDC_BYTERECEIVED, m_strByteReceive); 
   DDX_Text(pDX, IDC_NBYTERECEIVED, m_dwNByteReceive); 
   DDX_Text(pDX, IDC_WRITEBYTE, m_nWriteByte); 
   //}}AFX_DATA_MAP 
   } 
 
BEGIN_MESSAGE_MAP(CContactlessDemoVCDlg, CDialog) 
//{{AFX_MSG_MAP(CContactlessDemoVCDlg) 
ON_WM_SYSCOMMAND() 
ON_WM_PAINT() 
ON_WM_QUERYDRAGICON() 
ON_BN_CLICKED(IDC_WRITEMIFKEY, OnWritemifkey) 
ON_LBN_SELCHANGE(IDC_LISTREADER, OnSelchangeListreader) 
ON_BN_CLICKED(IDC_REFOUTPUTSC, OnRefoutputsc) 
ON_BN_CLICKED(IDC_MIFAUTHENT, OnMifauthent) 
ON_BN_CLICKED(IDC_READ, OnRead) 
ON_BN_CLICKED(IDC_WRITE, OnWrite) 
ON_BN_CLICKED(IDC_INCREMENT, OnIncrement) 
ON_BN_CLICKED(IDC_DECREMENT, OnDecrement) 
ON_BN_CLICKED(IDOK, OnExit) 
ON_BN_CLICKED(IDC_ACCESSOPTIONKEYNR, OnAccessoptionkeynr) 
ON_BN_CLICKED(IDC_ACCESSOPTIONKEY, OnAccessoptionkey) 
ON_BN_CLICKED(IDC_MIFKEYOPTIONS, OnMifkeyoptions) 
ON_BN_CLICKED(IDC_MIFKEYOPTIONP, OnMifkeyoptionp) 
ON_BN_CLICKED(IDC_WRITETRKEYTOREADER, OnWritetrkeytoreader) 
ON_BN_CLICKED(IDC_TRKEYOPTIONS, OnTrkeyoptions) 
ON_BN_CLICKED(IDC_TRKEYOPTIONP, OnTrkeyoptionp) 
ON_BN_CLICKED(IDC_TRANSMIT, OnTransmit) 
ON_EN_CHANGE(IDC_BYTESEND, OnChangeBytesend) 
ON_EN_CHANGE(IDC_DATATOWRITE, OnChangeDatatowrite) 
	ON_EN_CHANGE(IDC_TRKEYTOREADER, OnChangeTrkeytoreader) 
	ON_EN_CHANGE(IDC_MIFKEYTOREADER, OnChangeMifkeytoreader) 
	ON_EN_CHANGE(IDC_AUTHENTKEY, OnChangeAuthentkey) 
	ON_EN_CHANGE(IDC_INCREMENTVAL, OnChangeIncrementval) 
	ON_EN_CHANGE(IDC_DECREMENTVAL, OnChangeDecrementval) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CContactlessDemoVCDlg message handlers 
 
BOOL CContactlessDemoVCDlg::OnInitDialog() 
   { 
   CDialog::OnInitDialog(); 
   // Add "About..." menu item to system menu. 
   DWORD i = 0, z = 0, j =0; 
   CHAR mszReaders [1024]; 
   DWORD dwLength = sizeof(mszReaders); 
   CHAR  szReader [10] [100]; 
   hCard = 0x00000000; 
   hContext = 0x00000000; 
   fCM5121Selected = FALSE; 
   char str[80] = ""; 
   ulUIDBufLen = 12; 
   fCardConnected = FALSE; 
   memset(ucATR,sizeof(ucATR),0); 
   ulATRLen = sizeof(ucATR); 
   m_strKeyNrToReader = "00"; 
   m_strTrKeyNoToReader ="00"; 
   // IDM_ABOUTBOX must be in the system command range. 
   ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 
   ASSERT(IDM_ABOUTBOX < 0xF000); 
 
   CMenu* pSysMenu = GetSystemMenu(FALSE); 
   if (pSysMenu != NULL) 
      { 
      CString strAboutMenu; 
      strAboutMenu.LoadString(IDS_ABOUTBOX); 
      if (!strAboutMenu.IsEmpty()) 
         { 
         pSysMenu->AppendMenu(MF_SEPARATOR); 
         pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 
         } 
      } 
 
   // Set the icon for this dialog.  The framework does this automatically 
   //  when the application's main window is not a dialog 
   SetIcon(m_hIcon, TRUE);       // Set big icon 
   SetIcon(m_hIcon, FALSE);      // Set small icon 
 
   // TODO: Add extra initialization here 
 
 
 
   iMode = CLed::LED_DISABLED; 
   iColor = CLed::LED_COLOR_RED; 
   m_ledError.SetLed(iColor,iMode,CLed::LED_SQUARE); 
 
   iMode = CLed::LED_DISABLED; 
   iColor = CLed::LED_COLOR_GREEN; 
   m_ledSuccess.SetLed(iColor,iMode,CLed::LED_SQUARE); 
 
 
 
 
 
 
   m_ctrOutput.AddString("CM5121 Demo Application, please see the help file of Synchronous API for detail functionality"); 
 
 
   SCard_Status = SCardEstablishContext( 
                                       SCARD_SCOPE_USER, 
                                       NULL, 
                                       NULL, 
                                       &hContext); 
 
 
 
   if ( SCard_Status == SCARD_S_SUCCESS) 
      { 
 
      for (i=0;i<10;i++) 
         { 
         memset(&szReader[i][0],0x00,sizeof(&szReader[i][0])); 
         } 
 
 
 
 
      SCard_Status = SCardListReaders(hContext, 
                                      NULL, 
                                      mszReaders, 
                                      &dwLength); 
 
 
      if ( SCard_Status == SCARD_S_SUCCESS) 
         { 
 
         if (dwLength > 2) // \0\0 
            { 
            z=0; 
            j=0; 
            for (i=0;i<dwLength;i++) 
               { 
               if (mszReaders[i] == '\0') 
                  { 
                  szReader [j] [z] = mszReaders[i]; 
                  z=0; 
                  j++; 
                  } 
               else 
                  { 
                  szReader [j] [z] = mszReaders[i]; 
                  z++; 
                  } 
 
               } 
            } 
 
 
         for (j=0;j < 10;j++) 
            { 
            if (szReader[j] [0] != 0x00) 
               { 
               m_ctrReaderName.AddString(szReader[j]); 
 
 
               if (strcmp( szReader[j],"OMNIKEY CardMan 5121 0")==0) 
                  { 
                  m_strReaderName = "OMNIKEY CardMan 5121 0"; 
                  m_ctrOutput.AddString("Selected Reader is :"); 
                  m_ctrOutput.AddString(m_strReaderName); 
                  fCM5121Selected = TRUE; 
                  } 
               } 
            } 
 
 
         } 
      else 
         { 
         m_ctrOutput.AddString("Error in SCardListReaders, may be no reader available"); 
         TurnOnErrorLed(); 
 
 
         } 
 
      } 
   else 
      { 
      m_ctrOutput.AddString("Error in SCardEstablishContext"); 
      TurnOnErrorLed(); 
      } 
 
   //If a card is present connect the card and show the ATR, UID, CardName 
   //If not present start the thread to auto detection of the card 
 
   UpdateData(FALSE); 
   DisableAllFields(); 
   GetDlgItem(IDC_MIFTRKEYNR)->EnableWindow(FALSE); 
   GetDlgItem(IDC_MIFAUTHENTKEYNR)->EnableWindow(FALSE); 
   GetDlgItem(IDC_AUTHENTKEY)->EnableWindow(FALSE); 
   GetDlgItem(IDC_TRTRKEYNR)->EnableWindow(FALSE); 
 
 
   TrackCard(); 
   ShowCursorOutScreen(); 
 
   return TRUE;  // return TRUE  unless you set the focus to a control 
   } 
 
void CContactlessDemoVCDlg::OnSysCommand(UINT nID, LPARAM lParam) 
   { 
   if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
      { 
      CAboutDlg dlgAbout; 
      dlgAbout.DoModal(); 
      } 
   else 
      { 
      CDialog::OnSysCommand(nID, lParam); 
      } 
   } 
 
// If you add a minimize button to your dialog, you will need the code below 
//  to draw the icon.  For MFC applications using the document/view model, 
//  this is automatically done for you by the framework. 
 
void CContactlessDemoVCDlg::OnPaint() 
   { 
   if (IsIconic()) 
      { 
      CPaintDC dc(this); // device context for painting 
 
      SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 
 
      // Center icon in client rectangle 
      int cxIcon = GetSystemMetrics(SM_CXICON); 
      int cyIcon = GetSystemMetrics(SM_CYICON); 
      CRect rect; 
      GetClientRect(&rect); 
      int x = (rect.Width() - cxIcon + 1) / 2; 
      int y = (rect.Height() - cyIcon + 1) / 2; 
 
      // Draw the icon 
      dc.DrawIcon(x, y, m_hIcon); 
      } 
   else 
      { 
      CDialog::OnPaint(); 
      } 
   } 
 
// The system calls this to obtain the cursor to display while the user drags 
//  the minimized window. 
HCURSOR CContactlessDemoVCDlg::OnQueryDragIcon() 
   { 
   return(HCURSOR) m_hIcon; 
   } 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
void CContactlessDemoVCDlg::OnWritemifkey() 
   { 
   UCHAR ucMifareKey[8] = {0}; 
   ULONG ulMifareKeyLen = 0; 
   char Reader[80]=" "; 
   UCHAR ucKeyNr[1]={0}; 
   ULONG ulKeyNrLen = 0; 
   ULONG ulTrKeyNr = 0; 
   BOOLEAN fSecuredTransmission = FALSE; 
 
 
   UpdateData(TRUE); 
 
   if (m_intMifTrOption == 1) 
      { 
      fSecuredTransmission = FALSE; 
      if (m_strMifKeyToReader.GetLength() != 12) 
         { 
         m_ctrOutput.AddString("The Key must be of 6 bytes"); 
         TurnOnErrorLed(); 
         goto EXIT; 
 
 
         } 
 
 
      } 
   else 
      if (m_intMifTrOption == 0) 
      { 
      fSecuredTransmission = TRUE; 
      if (m_strMifKeyToReader.GetLength() != 16) 
         { 
         m_ctrOutput.AddString("The Key must be of 8 bytes"); 
         TurnOnErrorLed(); 
         goto EXIT; 
 
         } 
 
      CStringToUchar(m_strMifTrKeyNr,ucKeyNr,&ulKeyNrLen); 
      ulTrKeyNr = ucKeyNr[0]; 
 
 
      } 
   else 
      { 
      m_ctrOutput.AddString("One transmission option must be selected"); 
      TurnOnErrorLed(); 
      goto EXIT; 
 
 
      } 
 
 
   CStringToUchar(m_strMifKeyToReader,ucMifareKey,&ulMifareKeyLen); 
 
   CStringToUchar(m_strKeyNrToReader,ucKeyNr,&ulKeyNrLen); 
   ulMifKeyToReaderNr = ucKeyNr[0]; 
 
   if (fCM5121Selected == TRUE) 
      { 
 
      strcpy(Reader,(LPCTSTR) m_strReaderName); 
 
      if (fCardConnected != TRUE)hCard = 0x00000000; 
 
      GetSystemTime(&startTime); 
 
      lErr = SCardCLWriteMifareKeyToReader(hCard,hContext,Reader,ulMifKeyToReaderNr,ulMifareKeyLen, 
                                           ucMifareKey,fSecuredTransmission,ulTrKeyNr); 
 
      GetSystemTime(&stopTime); 
 
      if (lErr == NO_ERROR) 
         { 
 
         m_ctrOutput.AddString("The key has been written succesfully"); 
         ShowTimeRequired(); 
 
         } 
      else 
         { 
         ShowErrorMessage(lErr); 
         } 
      } 
   else 
      { 
 
      m_ctrOutput.AddString("For any operation one reader must be selected"); 
      TurnOnErrorLed(); 
      } 
 
   EXIT: 
   ShowCursorOutScreen(); 
   m_strMifKeyToReader.Empty(); 
   UpdateData(FALSE); 
 
   } 
 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
void CContactlessDemoVCDlg::OnMifauthent() 
   { 
   UCHAR ucMifareAuthMode; 
   UCHAR ucMifareKey[6] = {0}; 
   ULONG ulMifareKeyLen = 0; 
   UCHAR ucMifareAccessType; 
   UCHAR ucMifareKeyNr; 
 
   UpdateData(TRUE); 
 
   if (m_intAuthentMode == 0)ucMifareAuthMode = MIFARE_AUTHENT1A; 
   else 
      if (m_intAuthentMode == 1)ucMifareAuthMode = MIFARE_AUTHENT1B; 
   else 
      { 
      TurnOnErrorLed(); 
      m_ctrOutput.AddString("One Authentication mode must be selected"); 
      goto EXIT; 
      } 
 
   if (m_intAccessOption == 1) 
      { 
      ucMifareAccessType = MIFARE_KEY_INPUT; 
      if (m_strAuthentKey.GetLength() != 12) 
         { 
         TurnOnErrorLed(); 
         m_ctrOutput.AddString("Key length must be 6"); 
         goto EXIT; 
         } 
      CStringToUchar(m_strAuthentKey,ucMifareKey,&ulMifareKeyLen); 
 
      } 
   else 
      if (m_intAccessOption == 0) 
      { 
 
      CStringToUchar(m_strMifAuthentKeyNr,ucMifareKey,&ulMifareKeyLen); 
      ucMifareKeyNr = ucMifareKey[0]; 
      ucMifareAccessType = MIFARE_KEYNR_INPUT; 
 
      } 
   else 
      { 
      TurnOnErrorLed(); 
      m_ctrOutput.AddString("One Key option must be selected"); 
      goto EXIT; 
      } 
 
 
   if (fCardConnected == TRUE) 
      { 
      if (m_strCardName == "MIFARE_ST1K" && m_longBlockNr >63) 
         { 
 
         TurnOnErrorLed(); 
         m_ctrOutput.AddString("The Block number is not valid"); 
         goto EXIT; 
         } 
 
      if (m_strCardName == "MIFARE_ST4K" && m_longBlockNr >255) 
         { 
 
         TurnOnErrorLed(); 
         m_ctrOutput.AddString("The Block number is not valid"); 
         goto EXIT; 
         } 
 
      GetSystemTime(&startTime); 
 
      lErr = SCardCLMifareStdAuthent(hCard,m_longBlockNr,ucMifareAuthMode,ucMifareAccessType,ucMifareKeyNr, 
                                     ucMifareKey, ulMifareKeyLen); 
      GetSystemTime(&stopTime); 
 
      if (lErr==NO_ERROR) 
         { 
         m_ctrOutput.AddString("Authentication has been done successfully"); 
         ulAuthenticatedBlockNr= m_longBlockNr; 
         ShowTimeRequired(); 
         } 
      else 
         { 
         m_ctrOutput.AddString("Error in Authentication, may be the key is not correct"); 
         ShowErrorMessage(lErr); 
         } 
 
      } 
   else 
      { 
      TurnOnErrorLed(); 
      m_ctrOutput.AddString("The Card is not connected"); 
      } 
 
 
   EXIT: 
   ShowCursorOutScreen(); 
   m_strAuthentKey.Empty(); 
 
   UpdateData(FALSE); 
 
   } 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
void CContactlessDemoVCDlg::OnRead() 
   { 
   UCHAR  ucMifareDataRead[16] ={0}; 
   ULONG  ulMifareNumOfDataRead = 0; 
   char str[80] = ""; 
   m_strDataRead.Empty(); 
 
   UpdateData(TRUE); 
 
   if (fCardConnected == TRUE) 
      { 
      if (m_strCardName == "MIFARE_ST1K" || m_strCardName ==  "MIFARE_ST4K" ) 
         { 
 
         if (m_longBlockNr<128) 
            { 
            if (ulAuthenticatedBlockNr/4 != ((ULONG)m_longBlockNr)/4) 
               { 
 
               TurnOnErrorLed(); 
               m_ctrOutput.AddString("The selected block sector is not authenticated"); 
               goto EXIT; 
 
               } 
 
 
            } 
         else 
            { 
            if (ulAuthenticatedBlockNr /16 != ((ULONG)m_longBlockNr)/16) 
               { 
 
               TurnOnErrorLed(); 
               m_ctrOutput.AddString("The selected block sector is not authenticated"); 
               goto EXIT; 
 
               } 
 
            } 
 
 
 
 
         } 
 
 
      GetSystemTime(&startTime); 
 
      lErr = SCardCLMifareStdRead(hCard,m_longBlockNr,ucMifareDataRead,16,&ulMifareNumOfDataRead); 
 
      GetSystemTime(&stopTime); 
 
      if (lErr == NO_ERROR) 
         { 
         UcharToStr(ucMifareDataRead,ulMifareNumOfDataRead,str); 
         m_strDataRead = str; 
         m_ctrOutput.AddString("Reading has been done successfully"); 
         ShowTimeRequired(); 
         } 
      else 
         { 
         ShowErrorMessage(lErr); 
         } 
 
      } 
   else 
      { 
      TurnOnErrorLed(); 
      m_ctrOutput.AddString("The Card is not connected"); 
      } 
 
   EXIT: 
   ShowCursorOutScreen(); 
   UpdateData(FALSE); 
 
 
 
 
   } 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
void CContactlessDemoVCDlg::OnWrite() 
   { 
 
   ULONG ulBufferLen = 0; 
   UCHAR ucDataToWrite[16]={0}; 
   SectorTrailerDlg hSectorTrailerDlg; 
   m_strDataToWrite.Empty(); 
 
   UpdateData(TRUE); 
 
   if (fCardConnected == TRUE) 
      { 
 
      if (m_strCardName == "MIFARE_ST1K" || m_strCardName ==  "MIFARE_ST4K" ) 
         { 
 
         if (m_longBlockNr == 0) 
            { 
 
            TurnOnErrorLed(); 
            m_ctrOutput.AddString("Block 0 can not be written"); 
            goto EXIT; 
            } 
 
 
         if (m_longBlockNr<128) 
            { 
            if (ulAuthenticatedBlockNr/4 != ((ULONG)m_longBlockNr)/4) 
               { 
 
               TurnOnErrorLed(); 
               m_ctrOutput.AddString("The selected block sector is not authenticated"); 
               goto EXIT; 
 
               } 
 
 
            } 
         else 
            { 
            if (ulAuthenticatedBlockNr /16 != ((ULONG)m_longBlockNr)/16) 
               { 
 
               TurnOnErrorLed(); 
               m_ctrOutput.AddString("The selected block sector is not authenticated"); 
               goto EXIT; 
 
               } 
 
            } 
 
         if (m_longBlockNr < 128) 
            { 
            if (((m_longBlockNr + 1)%4)==0) 
               { 
               if (hSectorTrailerDlg.DoModal()!= IDOK) 
                  goto EXIT; 
               } 
            } 
         else 
            if (m_longBlockNr> 128) 
            { 
            if (((m_longBlockNr + 1)%16)==0) 
               { 
               if (hSectorTrailerDlg.DoModal()!= IDOK) 
                  goto EXIT; 
               } 
            } 
         } 
 
 
      if (m_strCardName == "MIFARE_ULIT") 
         { 
 
         if (m_longBlockNr == 0 || m_longBlockNr == 1) 
            { 
            TurnOnErrorLed(); 
            m_ctrOutput.AddString("Block 0 and 1 can not be written"); 
            goto EXIT; 
            } 
 
 
         if (m_longBlockNr == 2) 
            { 
 
            TurnOnErrorLed(); 
            m_ctrOutput.AddString("Block 2 internal and lock block, modifying lock block will lock the pages as Read Only, block 2 is  not allowed to write by this application "); 
            goto EXIT; 
            } 
 
         if (m_longBlockNr == 3) 
            { 
            TurnOnErrorLed(); 
            m_ctrOutput.AddString("Block 3 is OTP, block 3 is  not allowed to write by this application"); 
            goto EXIT; 
            } 
 
 
         } 
 
      CStringToUchar(m_strDataToWrite,ucDataToWrite,&ulBufferLen); 
      if (ulBufferLen != 16) 
         { 
         TurnOnErrorLed(); 
         m_ctrOutput.AddString("The length of the data to be written must be 16"); 
         goto EXIT; 
         } 
 
 
      GetSystemTime(&startTime); 
 
      lErr = SCardCLMifareStdWrite(hCard, m_longBlockNr,ucDataToWrite,ulBufferLen); 
      GetSystemTime(&stopTime); 
 
      if (lErr != NO_ERROR) 
         { 
         ShowErrorMessage(lErr); 
         } 
      else 
         { 
         m_ctrOutput.AddString("Writing has been done successfully"); 
         ShowTimeRequired(); 
         } 
 
      } 
   else 
      { 
      TurnOnErrorLed(); 
      m_ctrOutput.AddString("The Card is not connected"); 
      } 
 
 
   EXIT: 
   ShowCursorOutScreen(); 
   UpdateData(FALSE); 
   } 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
void CContactlessDemoVCDlg::OnIncrement() 
   { 
   UCHAR ucMifareIncrementValue[4] = {0}; 
   ULONG ulMifareIncrementValueBufLen = 4; 
 
   m_strIncrementValue.Empty(); 
 
   UpdateData(TRUE); 
   if (fCardConnected == TRUE) 
      { 
 
      if (m_strCardName == "MIFARE_ST1K" || m_strCardName ==  "MIFARE_ST4K" ) 
         { 
 
         if (m_longBlockNr == 0) 
            { 
 
            TurnOnErrorLed(); 
            m_ctrOutput.AddString("Block 0 can not be incremented"); 
            goto EXIT; 
            } 
 
 
         if (m_longBlockNr<128) 
            { 
            if (ulAuthenticatedBlockNr/4 != ((ULONG)m_longBlockNr)/4) 
               { 
 
               TurnOnErrorLed(); 
               m_ctrOutput.AddString("The selected block sector is not authenticated"); 
               goto EXIT; 
 
               } 
 
 
            } 
         else 
            { 
            if (ulAuthenticatedBlockNr /16 != ((ULONG)m_longBlockNr)/16) 
               { 
 
               TurnOnErrorLed(); 
               m_ctrOutput.AddString("The selected block sector is not authenticated"); 
               goto EXIT; 
 
               } 
 
            } 
 
 
         if (m_longBlockNr < 128) 
            { 
            if (((m_longBlockNr + 1)%4)==0) 
               { 
               TurnOnErrorLed(); 
               m_ctrOutput.AddString("Trailer block cannot be incremented"); 
               goto EXIT; 
               } 
            } 
         else 
            { 
            if (m_longBlockNr> 128) 
               if (((m_longBlockNr + 1)%16)==0) 
                  { 
                  TurnOnErrorLed(); 
                  m_ctrOutput.AddString("Trailer block cannot be incremented"); 
 
                  } 
            } 
 
 
 
         CStringToUchar(m_strIncrementValue,ucMifareIncrementValue,&ulMifareIncrementValueBufLen); 
         if (ulMifareIncrementValueBufLen != 4) 
            { 
            TurnOnErrorLed(); 
            m_ctrOutput.AddString("The length of the data by incremented must be 4"); 
            goto EXIT; 
            } 
         GetSystemTime(&startTime); 
 
         lErr = SCardCLMifareStdIncrementVal(hCard,m_longBlockNr,ucMifareIncrementValue,ulMifareIncrementValueBufLen); 
         GetSystemTime(&stopTime); 
 
         if (lErr== NO_ERROR) 
            { 
            m_ctrOutput.AddString("Increment value has been done successfully"); 
            ShowTimeRequired(); 
 
            } 
         else 
            { 
            ShowErrorMessage(lErr); 
            m_ctrOutput.AddString("May be the block is not a value block"); 
 
            } 
         } 
      else 
         { 
         TurnOnErrorLed(); 
         m_ctrOutput.AddString("Except Mifare 1K or Mifare4K, other cards do not support Mifare std increment"); 
         } 
      } 
 
   else 
      { 
      TurnOnErrorLed(); 
      m_ctrOutput.AddString("The Card is not connected"); 
      } 
   EXIT: 
   ShowCursorOutScreen(); 
   UpdateData(FALSE); 
 
 
   } 
 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
void CContactlessDemoVCDlg::OnDecrement() 
   { 
 
   UCHAR ucMifareDecrementValue[4] = {0}; 
   ULONG ulMifareDecrementValueBufLen = 4; 
   m_strDecrementValue.Empty(); 
 
   UpdateData(TRUE); 
   if (fCardConnected == TRUE) 
      { 
 
      if (m_strCardName == "MIFARE_ST1K" || m_strCardName ==  "MIFARE_ST4K" ) 
         { 
 
         if (m_longBlockNr == 0) 
            { 
 
            TurnOnErrorLed(); 
            m_ctrOutput.AddString("Block 0 can not be decremented"); 
            goto EXIT; 
            } 
 
 
         if (m_longBlockNr<128) 
            { 
            if (ulAuthenticatedBlockNr/4 != ((ULONG)m_longBlockNr)/4) 
               { 
 
               TurnOnErrorLed(); 
               m_ctrOutput.AddString("The selected block sector is not authenticated"); 
               goto EXIT; 
 
               } 
 
 
            } 
         else 
            { 
            if (ulAuthenticatedBlockNr /16 != ((ULONG)m_longBlockNr)/16) 
               { 
 
               TurnOnErrorLed(); 
               m_ctrOutput.AddString("The selected block sector is not authenticated"); 
               goto EXIT; 
 
               } 
 
            } 
 
 
         if (m_longBlockNr < 128) 
            { 
            if (((m_longBlockNr + 1)%4)==0) 
               { 
               TurnOnErrorLed(); 
               m_ctrOutput.AddString("Trailer block cannot be decremented"); 
               goto EXIT; 
               } 
            } 
         else 
            { 
            if (m_longBlockNr> 128) 
               if (((m_longBlockNr + 1)%16)==0) 
                  { 
                  TurnOnErrorLed(); 
                  m_ctrOutput.AddString("Trailer block cannot be decremented"); 
 
                  } 
            } 
 
 
 
         CStringToUchar(m_strDecrementValue,ucMifareDecrementValue,&ulMifareDecrementValueBufLen); 
         if (ulMifareDecrementValueBufLen != 4) 
            { 
            TurnOnErrorLed(); 
            m_ctrOutput.AddString("The length of the data by decremented must be 4"); 
            goto EXIT; 
            } 
         GetSystemTime(&startTime); 
         lErr = SCardCLMifareStdDecrementVal(hCard,m_longBlockNr,ucMifareDecrementValue,ulMifareDecrementValueBufLen); 
         GetSystemTime(&stopTime); 
 
 
         if (lErr == NO_ERROR) 
            { 
            m_ctrOutput.AddString("Decrement value has been done successfully"); 
            ShowTimeRequired(); 
 
            } 
         else 
            { 
            ShowErrorMessage(lErr); 
            m_ctrOutput.AddString("May be the block is not a value block"); 
 
            } 
 
         } 
      else 
         { 
         TurnOnErrorLed(); 
         m_ctrOutput.AddString("Except Mifare 1K or Mifare4K, other cards do not support Mifare std decrement"); 
         } 
      } 
 
   else 
      { 
      TurnOnErrorLed(); 
      m_ctrOutput.AddString("The Card is not connected"); 
      } 
 
   EXIT: 
   ShowCursorOutScreen(); 
   UpdateData(FALSE); 
 
   } 
 
 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
void CContactlessDemoVCDlg::TrackCard() 
   { 
 
   UpdateData(TRUE); 
   m_ctrAtrList.ResetContent(); 
   m_ctrUidList.ResetContent(); 
   m_ctrCardNameList.ResetContent(); 
 
   THREADSTRUCT *_param = new THREADSTRUCT; 
   _param->_this = this; 
   AfxBeginThread (StartCardThread, _param); 
   UpdateData(FALSE); 
 
 
   } 
 
 
 
 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
UINT CContactlessDemoVCDlg::StartCardThread (LPVOID param) 
   { 
   THREADSTRUCT* ts = (THREADSTRUCT*)param; 
   char str[80] = ""; 
   UCHAR PresentCardState = Card_Absent ; 
   UCHAR PreviousCardState = Card_Absent ; 
   UCHAR hATR[64] = {0}; 
 
   //here is the time-consuming process which interacts with your dialog 
 
   while (ts->_this->fCM5121Selected == TRUE) 
      { 
 
      ts->_this->sReaderState.szReader = strdup(ts->_this->m_strReaderName); 
      ts->_this->sReaderState.dwCurrentState = SCARD_STATE_EMPTY; 
      ts->_this->sReaderState.dwEventState = SCARD_STATE_EMPTY; 
 
      //Look for the card state 
      SCardGetStatusChange(ts->_this->hContext,500,&(ts->_this->sReaderState),1); 
      if (ts->_this->sReaderState.dwEventState & SCARD_STATE_PRESENT) 
         { 
         PresentCardState = Card_Present ; //Card is present 
 
         //Here find if the ATR has been changed	 
         memcpy(hATR,ts->_this->sReaderState.rgbAtr,ts->_this->sReaderState.cbAtr); 
 
         if (memcmp(hATR,ts->_this->ucATR,64) != 0) 
            PresentCardState = Card_Changed; 
 
         if (PreviousCardState == Card_Absent || PresentCardState == Card_Changed ) 
            { 
            if (SCardConnect(ts->_this->hContext,ts->_this->m_strReaderName, 
                             SCARD_SHARE_SHARED,SCARD_PROTOCOL_T0|SCARD_PROTOCOL_T1,&(ts->_this->hCard),&(ts->_this->dwActiveProtocols)) 
                == SCARD_S_SUCCESS) 
               { 
               PresentCardState =  Card_Present; 
 
               ts->_this->fCardConnected = TRUE; 
 
               memset(ts->_this->ucATR,0,64); 
               ts->_this->ulATRLen = ts->_this->sReaderState.cbAtr; 
               memcpy(ts->_this->ucATR,hATR,ts->_this->ulATRLen); 
               ts->_this->UcharToStr(ts->_this->ucATR,ts->_this->ulATRLen,str); 
               ts->_this->m_strATR = str; 
 
               if (ts->_this->GiveCardName(ts->_this->ucATR,ts->_this->ulATRLen,str)==TRUE) 
                  { 
                  memset(ts->_this->ucUID,12,0); 
                  ts->_this->ulnByteUID = 12; 
                  if (SCardCLGetUID(ts->_this->hCard,ts->_this->ucUID,ts->_this->ulUIDBufLen, &ts->_this->ulnByteUID)== 
                      NO_ERROR) 
                     { 
                     ts->_this->UcharToStr(ts->_this->ucUID,ts->_this->ulnByteUID,str); 
                     ts->_this->m_strUID = str; 
                     } 
                  } 
               else 
                  ts->_this->m_strUID = ""; 
 
 
               } 
            else 
               { 
               ts->_this->m_strUID = ""; 
               ts->_this->m_strATR = ""; 
               ts->_this->m_strCardName =""; 
               } 
 
            } 
 
         ts->_this->m_ctrAtrList.ResetContent(); 
         ts->_this->m_ctrUidList.ResetContent(); 
         ts->_this->m_ctrCardNameList.ResetContent(); 
 
         ts->_this->m_ctrAtrList.AddString(ts->_this->m_strATR); 
         ts->_this->m_ctrUidList.AddString(ts->_this->m_strUID ); 
         ts->_this->m_ctrCardNameList.AddString(ts->_this->m_strCardName); 
 
         } 
      else 
         { 
         PresentCardState = Card_Absent; //Card is not present 
         ts->_this->m_ctrAtrList.ResetContent(); 
         ts->_this->m_ctrUidList.ResetContent(); 
         ts->_this->m_ctrCardNameList.ResetContent(); 
         ts->_this->DisableAllFields(); 
         ts->_this->ulAuthenticatedBlockNr = 257; 
 
 
         } 
 
 
 
      if (PreviousCardState != PresentCardState) 
         { 
         if (PresentCardState == Card_Present) 
            { 
            ts->_this->m_ctrOutput.AddString("Card is present"); 
 
            } 
         else if (PresentCardState == Card_Absent) 
            { 
            ts->_this->m_ctrOutput.AddString("Card is not present"); 
            ts->_this->hCard = 0; 
 
 
            } 
         } 
 
 
      ts->_this->ShowCursorOutScreen(); 
      PreviousCardState = PresentCardState; 
      Sleep(500); 
 
      } //while loop 
 
 
   AfxEndThread(0); 
   return 1; 
   } 
 
 
 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
void CContactlessDemoVCDlg::ShowErrorMessage(long eError) 
   { 
 
   TurnOnErrorLed(); 
 
   switch (eError) 
      { 
 
      case SCARD_E_CANCELLED: 
         m_ctrOutput.AddString( "SCARD_E_CANCELLED:\r\n   The action was canceled by an SCardCancel request."); 
         break; 
 
      case SCARD_E_CANT_DISPOSE: 
         m_ctrOutput.AddString( "SCARD_E_CANT_DISPOSE:\r\n   The system could not dispose of the media in the requested manner."); 
         break; 
 
      case SCARD_E_CARD_UNSUPPORTED: 
         m_ctrOutput.AddString( "SCARD_E_CARD_UNSUPPORTED:\r\n   The smart card does not meet minimal requirements for support."); 
         break; 
 
      case SCARD_E_DUPLICATE_READER: 
         m_ctrOutput.AddString( "SCARD_E_DUPLICATE_READER:\r\n   The reader driver didn't produce a unique reader name."); 
         break; 
 
 
      case SCARD_E_INSUFFICIENT_BUFFER: 
         m_ctrOutput.AddString( "SCARD_E_INSUFFICIENT_BUFFER:\r\n   The data buffer for returned data is too small for the returned data."); 
         break; 
 
      case SCARD_E_INVALID_ATR: 
         m_ctrOutput.AddString( "SCARD_E_INVALID_ATR:\r\n   An ATR string obtained from the registry is not a valid ATR string."); 
         break; 
 
 
      case SCARD_E_INVALID_HANDLE: 
         m_ctrOutput.AddString( "SCARD_E_INVALID_HANDLE:\r\n   The supplied handle was invalid."); 
         break; 
 
      case SCARD_E_INVALID_PARAMETER: 
         m_ctrOutput.AddString( "SCARD_E_INVALID_PARAMETER:\r\n   One or more of the supplied parameters could not be properly interpreted."); 
         break; 
 
      case SCARD_E_INVALID_TARGET: 
         m_ctrOutput.AddString( "SCARD_E_INVALID_TARGET:\r\n   Registry startup information is missing or invalid."); 
         break; 
 
      case SCARD_E_INVALID_VALUE: 
         m_ctrOutput.AddString( "SCARD_E_INVALID_VALUE:\r\n   One or more of the supplied parameter values could not be properly interpreted."); 
         break; 
 
 
      case SCARD_E_NO_MEMORY: 
         m_ctrOutput.AddString( "SCARD_E_NO_MEMORY:\r\n   Not enough memory available to complete this command."); 
         break; 
 
 
      case SCARD_E_NO_SERVICE: 
         m_ctrOutput.AddString( "SCARD_E_NO_SERVICE:\r\n   The smart card resource manager is not running."); 
         break; 
 
      case SCARD_E_NO_SMARTCARD: 
         m_ctrOutput.AddString( "SCARD_E_NO_SMARTCARD:\r\n   The operation requires a smart card,\r\n\tbut no smart card is currently in the device."); 
         break; 
 
 
      case SCARD_E_NOT_READY: 
         m_ctrOutput.AddString( "SCARD_E_NOT_READY:\r\n   The reader or card is not ready to accept commands."); 
         break; 
 
      case SCARD_E_NOT_TRANSACTED: 
         m_ctrOutput.AddString( "SCARD_E_NOT_TRANSACTED:\r\n   An attempt was made to end a non-existent transaction."); 
         break; 
 
      case SCARD_E_PCI_TOO_SMALL: 
         m_ctrOutput.AddString( "SCARD_E_PCI_TOO_SMALL:\r\n   The PCI receive buffer was too small."); 
         break; 
 
      case SCARD_E_PROTO_MISMATCH: 
         m_ctrOutput.AddString( "SCARD_E_PROTO_MISMATCH:\r\n   The requested protocols are incompatible with the protocol\r\n\tcurrently in use with the card."); 
         break; 
 
      case SCARD_E_READER_UNAVAILABLE: 
         m_ctrOutput.AddString( "SCARD_E_READER_UNAVAILABLE:\r\n   The specified reader is not currently available for use."); 
         break; 
 
      case SCARD_E_READER_UNSUPPORTED: 
         m_ctrOutput.AddString( "SCARD_E_READER_UNSUPPORTED:\r\n   The reader driver does not meet minimal requirements for support."); 
         break; 
 
      case SCARD_E_SERVICE_STOPPED: 
         m_ctrOutput.AddString( "SCARD_E_SERVICE_STOPPED:\r\n   The smart card resource manager has shut down."); 
         break; 
 
      case SCARD_E_SHARING_VIOLATION: 
         m_ctrOutput.AddString( "SCARD_E_SHARING_VIOLATION:\r\n   The smart card cannot be accessed because of other outstanding connections."); 
         break; 
 
      case SCARD_E_SYSTEM_CANCELLED: 
         m_ctrOutput.AddString( "SCARD_E_SYSTEM_CANCELLED:\r\n   The action was canceled by the system, presumably to log off or shut down."); 
         break; 
 
      case SCARD_E_TIMEOUT: 
         m_ctrOutput.AddString( "SCARD_E_TIMEOUT:\r\n   The user-specified timeout value has expired."); 
         break; 
 
 
      case SCARD_E_UNKNOWN_CARD: 
         m_ctrOutput.AddString( "SCARD_E_UNKNOWN_CARD:\r\n   The specified smart card name is not recognized."); 
         break; 
 
      case SCARD_E_UNKNOWN_READER: 
         m_ctrOutput.AddString( "SCARD_E_UNKNOWN_READER:\r\n   The specified reader name is not recognized."); 
         break; 
 
 
      case SCARD_F_COMM_ERROR: 
         m_ctrOutput.AddString( "SCARD_F_COMM_ERROR:\r\n   An internal communications error has been detected."); 
         break; 
 
      case SCARD_F_INTERNAL_ERROR: 
         m_ctrOutput.AddString( "SCARD_F_INTERNAL_ERROR:\r\n   An internal consistency check failed."); 
         break; 
 
      case SCARD_F_UNKNOWN_ERROR: 
         m_ctrOutput.AddString( "SCARD_F_UNKNOWN_ERROR:\r\n   An internal error has been detected, but the source is unknown."); 
         break; 
 
      case SCARD_F_WAITED_TOO_LONG: 
         m_ctrOutput.AddString( "SCARD_F_WAITED_TOO_LONG:\r\n   An internal consistency timer has expired."); 
         break; 
 
      case SCARD_P_SHUTDOWN: 
         m_ctrOutput.AddString( "SCARD_P_SHUTDOWN:\r\n   The operation has been aborted to allow the server application to exit."); 
         break; 
 
      case SCARD_S_SUCCESS: 
         m_ctrOutput.AddString( "SCARD_S_SUCCESS:\r\n   No error was encountered."); 
         break; 
 
 
      case SCARD_W_REMOVED_CARD: 
         m_ctrOutput.AddString( "SCARD_W_REMOVED_CARD:\r\n   The smart card has been removed, so that further communication is not possible."); 
         break; 
 
      case SCARD_W_RESET_CARD: 
         m_ctrOutput.AddString( "SCARD_W_RESET_CARD:\r\n   The smart card has been reset, so any shared state information is invalid."); 
         break; 
 
 
      case SCARD_W_UNPOWERED_CARD: 
         m_ctrOutput.AddString( "SCARD_W_UNPOWERED_CARD:\r\n   Power has been removed from the smart card, so that further communication is not possible."); 
         break; 
 
      case SCARD_W_UNRESPONSIVE_CARD: 
         m_ctrOutput.AddString( "SCARD_W_UNRESPONSIVE_CARD:\r\n   The smart card is not responding to a reset."); 
         break; 
 
      case SCARD_W_UNSUPPORTED_CARD: 
         m_ctrOutput.AddString( "SCARD_W_UNSUPPORTED_CARD:\r\n   The reader cannot communicate with the card, due to ATR string configuration conflicts."); 
         break; 
 
      default: 
         m_ctrOutput.AddString(" There was error in this operation, No error string available"); 
         break; 
      } 
 
   iCount = m_ctrOutput. GetCount( ); 
   for (int iUnsel = 0; iUnsel<iCount - 2;iUnsel++) 
      m_ctrOutput.SetSel(iUnsel,  FALSE ); 
 
   m_ctrOutput.SetSel(iCount - 1,  TRUE ); 
 
 
   } 
 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
void CContactlessDemoVCDlg::CStringToUchar(CString str, UCHAR *ucBuffer, ULONG *ulBufferLen) 
   { 
   int Length = 0; 
   int DataLength = 0; 
   char cstr[] =""; 
   char strcstring[512] =""; 
   byte hexval=0x00; 
   int i = 0; 
 
   Length = str.GetLength(); 
 
   for (i = 0; i<Length; i++) 
      strcstring[i] = str.GetAt(i); 
 
   DataLength = Length / 2; 
   for (i = 0; i<DataLength; i++) 
      { 
      cstr[0] = strcstring[2*i]; 
      cstr[1] = strcstring[2*i+1]; 
      sscanf( cstr, "%02x", &hexval ); 
      ucBuffer[i]=hexval; 
 
      } 
 
   *ulBufferLen = DataLength; 
 
   } 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
void CContactlessDemoVCDlg::UcharToStr(UCHAR *ucDataBuffer, ULONG ulDataBufLen, char *str) 
   { 
   ULONG i = 0; 
   char bytetochar[3] =""; 
   char Temp[1024] =""; 
 
 
   for (i=0;i<ulDataBufLen;i++) 
      { 
      _itoa(ucDataBuffer[i],bytetochar,16); 
      if (ucDataBuffer[i]<=0x0F)strcat(Temp,"0"); 
      strcat(Temp,bytetochar); 
      } 
 
   memcpy(str,Temp,ulDataBufLen*2); 
   str[ulDataBufLen*2] = 0; 
 
   } 
 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
void CContactlessDemoVCDlg::OnSelchangeListreader() 
   { 
   // TODO: Add your control notification handler code here 
   UpdateData(TRUE); 
 
   m_ctrOutput.AddString("Selected Reader is :"); 
   m_ctrOutput.AddString(m_strReaderName); 
   TrackCard(); 
 
   } 
 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
 
BOOLEAN CContactlessDemoVCDlg::GiveCardName(UCHAR *ucATR, ULONG ATRLen, char *CardName) 
   { 
   char strCardName[80] =""; 
   if (ATRLen == 17 && ucATR[1] == 0x0F && ucATR[2] == 0xFF) //Contactless Memory card 
      { 
 
      switch (ucATR[16] & 0x0F) 
         { 
         case ISO14443A_123: 
            switch (ucATR[16] & 0xF0) 
               { 
               case MIFARE_ST1K: 
                  strcat(strCardName,"MIFARE_ST1K"); 
                  m_strMifAuthentKeyNr = "00"; 
                  EnableAllFields(); 
                  break; 
               case MIFARE_ST4K: 
                  strcat(strCardName,"MIFARE_ST4K"); 
                  m_strMifAuthentKeyNr = "00"; 
                  EnableAllFields(); 
                  break; 
               case MIFARE_ULIT: 
                  strcat(strCardName,"MIFARE_ULIT"); 
                  GetDlgItem(IDC_AUTHENTBLOCKNR)->EnableWindow(TRUE); 
                  GetDlgItem(IDC_READ)->EnableWindow(TRUE); 
                  GetDlgItem(IDC_DATAREAD)->EnableWindow(TRUE); 
                  GetDlgItem(IDC_WRITE)->EnableWindow(TRUE); 
                  GetDlgItem(IDC_DATATOWRITE)->EnableWindow(TRUE); 
                  break; 
               case SLE55R_XXXX: 
                  strcat(strCardName,"SLE55R_XXXX"); 
                  break; 
               default: 
                  strcat(strCardName,"ISO14443A Card"); 
                  break; 
               } 
 
            break; 
         case ISO15693_12ICL: 
            GetDlgItem(IDC_TRANSMIT)->EnableWindow(TRUE); 
            GetDlgItem(IDC_BYTESEND)->EnableWindow(TRUE); 
            GetDlgItem(IDC_NBYTESEND)->EnableWindow(TRUE); 
            GetDlgItem(IDC_NBYTERECEIVED)->EnableWindow(TRUE); 
 
 
            switch (ucATR[16] & 0xF0) 
               { 
               case 0xC0: 
                  strcat(strCardName,"iClass 2K"); 
                  break; 
 
               case 0x90: 
                  strcat(strCardName,"iClass 2KS"); 
                  break; 
 
               case 0xD0: 
                  strcat(strCardName,"iClass 16K"); 
                  break; 
 
               case 0xA0: 
                  strcat(strCardName,"iClass 16KS"); 
                  break; 
 
               case 0xE0: 
                  strcat(strCardName,"iClass 8x2K"); 
                  break; 
 
               case 0xB0: 
                  strcat(strCardName,"iClass 8x2KS"); 
                  break; 
 
               default: 
                  strcat(strCardName,"iClass Card"); 
                  break; 
               } 
            break; 
         case ISO15693_123: 
            switch (ucATR[16] & 0xF0) 
               { 
               case SRF55V10P: 
                  strcat(strCardName,"SRF55V10P"); 
                  break; 
               case SRF55V02P: 
                  strcat(strCardName,"SRF55V02P"); 
                  break; 
               case SRF55V10S: 
                  strcat(strCardName,"SRF55V10S"); 
                  break; 
               case SRF55V02S: 
                  strcat(strCardName,"SRF55V02S"); 
                  break; 
               case TAG_IT: 
                  strcat(strCardName,"TAG_IT"); 
                  break; 
               case LRI512: 
                  strcat(strCardName,"LRI512"); 
                  break; 
               case ICODESLI: 
                  strcat(strCardName,"ICODESLI"); 
                  break; 
               case SRF55XXX: 
                  strcat(strCardName,"SRF55XXX"); 
                  break; 
               case TEMPSENS: 
                  strcat(strCardName,"TEMPSENS"); 
                  break; 
               default: 
                  strcat(strCardName,"ISO 15693 Card"); 
                  break; 
 
               } 
            break; 
         case ISO14443B_12STM : 
            switch (ucATR[16] & 0xF0) 
               { 
               case SR176: 
                  strcat(strCardName,"SR176"); 
                  break; 
               case SRIX4K: 
                  strcat(strCardName,"SRIX4K"); 
                  break; 
 
               } 
            break; 
         case ISO14443B_123 : 
            switch (ucATR[16] & 0xF0) 
               { 
               case AT88RF020: 
                  strcat(strCardName,"AT88RF020"); 
                  break; 
               case AT88SC6416CRF: 
                  strcat(strCardName,"AT88RF020"); 
                  break; 
               default: 
                  break; 
 
               } 
            break; 
         case ISO15693_12ICODE1: 
            strcat(strCardName,"ICODE1"); 
            break; 
 
         default: 
            break; 
 
 
         } 
      m_strCardName = strCardName; 
      return TRUE; 
      } 
   else 
      { 
 
      GetDlgItem(IDC_TRANSMIT)->EnableWindow(TRUE); 
      GetDlgItem(IDC_BYTESEND)->EnableWindow(TRUE); 
      GetDlgItem(IDC_NBYTESEND)->EnableWindow(TRUE); 
      GetDlgItem(IDC_NBYTERECEIVED)->EnableWindow(TRUE); 
 
 
      if (ATRLen == 0x06 && ucATR[4] == 0x80 && ucATR[5] == 0x80 ) 
         { 
         strcat(strCardName,"DESFire Card"); 
         m_strCardName = strCardName; 
         return TRUE; 
         } 
 
      else if ((ucATR[1] & 0x80) == 0x80 && ucATR[2] == 0x80 && ucATR[3] == 0x01) 
         { 
         strcat(strCardName,"T=CL Card"); 
         m_strCardName = strCardName; 
         return TRUE; 
         } 
      else 
         { 
         strcat(strCardName,"May be Contact Card"); 
         m_strCardName = strCardName; 
         return FALSE; 
 
         } 
      } 
 
 
   } 
 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
void CContactlessDemoVCDlg::OnRefoutputsc() 
   { 
   m_ctrOutput.ResetContent(); 
   m_ctrOutput.AddString("CM5121 Demo Application, please see the help file of Synchronous API for detail functionality"); 
   } 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
void CContactlessDemoVCDlg::ShowTimeRequired() 
   { 
   char bytetochar[10]={'0'}; 
   char strFortime[80]=""; 
 
   SystemTimeToFileTime(&startTime, &ftimetmp); 
   ulistartTime.LowPart=ftimetmp.dwLowDateTime; 
   ulistartTime.HighPart=ftimetmp.dwHighDateTime; 
   SystemTimeToFileTime(&stopTime, &ftimetmp); 
   ulistopTime.LowPart=ftimetmp.dwLowDateTime; 
   ulistopTime.HighPart=ftimetmp.dwHighDateTime; 
   ulirunTime.QuadPart=ulistopTime.QuadPart-ulistartTime.QuadPart; 
   ftimetmp.dwHighDateTime=ulirunTime.HighPart; 
   ftimetmp.dwLowDateTime=ulirunTime.LowPart; 
   FileTimeToSystemTime(&ftimetmp, &runTime); 
 
 
   _itoa(runTime.wSecond,bytetochar,10); 
   if (runTime.wSecond<=9)strncat(strFortime,"0",19); 
   strncat(strFortime,bytetochar,19); 
   strncat(strFortime,":",19); 
 
   _itoa((runTime.wMilliseconds/10),bytetochar,10); 
   if ((runTime.wMilliseconds/10)<=9)strncat(strFortime,"0",19); 
   strncat(strFortime,bytetochar,19); 
 
   _itoa((runTime.wMilliseconds - (runTime.wMilliseconds/10)*10) ,bytetochar,10); 
   strncat(strFortime,bytetochar,19); 
 
   m_ctrOutput.AddString("Currently executed operation took :  " +  CString(strFortime) + " Sec"); 
 
   startTime.wMilliseconds = 0; 
   startTime.wSecond = 0; 
   stopTime.wMilliseconds = 0; 
   stopTime.wSecond = 0; 
   runTime.wMilliseconds = 0; 
   runTime.wSecond = 0; 
 
   iMode = CLed::LED_DISABLED; 
   iColor = CLed::LED_COLOR_RED; 
   m_ledError.SetLed(iColor,iMode,CLed::LED_SQUARE); 
 
   iMode = CLed::LED_ON; 
   iColor = CLed::LED_COLOR_GREEN; 
   m_ledSuccess.SetLed(iColor,iMode,CLed::LED_SQUARE); 
 
 
   } 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
void CContactlessDemoVCDlg::DisableAllFields() 
   { 
 
   GetDlgItem(IDC_MIFAUTHENT)->EnableWindow(FALSE); 
   GetDlgItem(IDC_AUTHENTBLOCKNR)->EnableWindow(FALSE); 
   GetDlgItem(IDC_ACCESSOPTIONKEYNR)->EnableWindow(FALSE); 
   GetDlgItem(IDC_MIFAUTHENTKEYNR)->EnableWindow(FALSE); 
   GetDlgItem(IDC_ACCESSOPTIONKEY)->EnableWindow(FALSE); 
   GetDlgItem(IDC_AUTHENTKEY)->EnableWindow(FALSE); 
   GetDlgItem(IDC_MODEA)->EnableWindow(FALSE); 
   GetDlgItem(IDC_MODEB)->EnableWindow(FALSE); 
   GetDlgItem(IDC_READ)->EnableWindow(FALSE); 
   GetDlgItem(IDC_DATAREAD)->EnableWindow(FALSE); 
   GetDlgItem(IDC_WRITE)->EnableWindow(FALSE); 
   GetDlgItem(IDC_DATATOWRITE)->EnableWindow(FALSE); 
   GetDlgItem(IDC_INCREMENT)->EnableWindow(FALSE); 
   GetDlgItem(IDC_INCREMENTVAL)->EnableWindow(FALSE); 
   GetDlgItem(IDC_DECREMENT)->EnableWindow(FALSE); 
   GetDlgItem(IDC_DECREMENTVAL)->EnableWindow(FALSE); 
   GetDlgItem(IDC_TRANSMIT)->EnableWindow(FALSE); 
   GetDlgItem(IDC_BYTESEND)->EnableWindow(FALSE); 
   GetDlgItem(IDC_NBYTESEND)->EnableWindow(FALSE); 
   GetDlgItem(IDC_NBYTERECEIVED)->EnableWindow(FALSE); 
 
   } 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
void CContactlessDemoVCDlg::EnableAllFields() 
   { 
 
   GetDlgItem(IDC_MIFAUTHENT)->EnableWindow(TRUE); 
   GetDlgItem(IDC_AUTHENTBLOCKNR)->EnableWindow(TRUE); 
   GetDlgItem(IDC_ACCESSOPTIONKEYNR)->EnableWindow(TRUE); 
   GetDlgItem(IDC_ACCESSOPTIONKEY)->EnableWindow(TRUE); 
   // GetDlgItem(IDC_MIFAUTHENTKEYNR)->EnableWindow(TRUE); 
   // GetDlgItem(IDC_AUTHENTKEY)->EnableWindow(TRUE); 
   GetDlgItem(IDC_MODEA)->EnableWindow(TRUE); 
   GetDlgItem(IDC_MODEB)->EnableWindow(TRUE); 
   GetDlgItem(IDC_READ)->EnableWindow(TRUE); 
   GetDlgItem(IDC_DATAREAD)->EnableWindow(TRUE); 
   GetDlgItem(IDC_WRITE)->EnableWindow(TRUE); 
   GetDlgItem(IDC_DATATOWRITE)->EnableWindow(TRUE); 
   GetDlgItem(IDC_INCREMENT)->EnableWindow(TRUE); 
   GetDlgItem(IDC_INCREMENTVAL)->EnableWindow(TRUE); 
   GetDlgItem(IDC_DECREMENT)->EnableWindow(TRUE); 
   GetDlgItem(IDC_DECREMENTVAL)->EnableWindow(TRUE); 
 
 
   } 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
void CContactlessDemoVCDlg::OnExit() 
   { 
 
   fCM5121Selected = FALSE; 
   SCardDisconnect(hCard,SCARD_LEAVE_CARD ); 
   SCardReleaseContext(hContext); 
   exit(0); 
   } 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
void CContactlessDemoVCDlg::ShowCursorOutScreen() 
   { 
 
   int iCount = m_ctrOutput. GetCount( ); 
   for (int iUnsel = 0; iUnsel<iCount - 1;iUnsel++) 
      m_ctrOutput.SetSel(iUnsel,  FALSE ); 
   m_ctrOutput.SetSel(iCount - 1,  TRUE ); 
 
   } 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
void CContactlessDemoVCDlg::TurnOnErrorLed() 
   { 
 
   iMode = CLed::LED_ON; 
   iColor = CLed::LED_COLOR_RED; 
   m_ledError.SetLed(iColor,iMode,CLed::LED_SQUARE); 
 
   iMode = CLed::LED_DISABLED; 
   iColor = CLed::LED_COLOR_RED; 
   m_ledSuccess.SetLed(iColor,iMode,CLed::LED_SQUARE); 
 
 
   } 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
void CContactlessDemoVCDlg::OnAccessoptionkeynr() 
   { 
   m_strMifAuthentKeyNr ="00"; 
   m_intAccessOption = 0 ; 
   GetDlgItem(IDC_MIFAUTHENTKEYNR)->EnableWindow(TRUE); 
   GetDlgItem(IDC_AUTHENTKEY)->EnableWindow(FALSE); 
   UpdateData(FALSE); 
 
   } 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
void CContactlessDemoVCDlg::OnAccessoptionkey() 
   { 
   m_strMifAuthentKeyNr =""; 
   m_intAccessOption = 1 ; 
   GetDlgItem(IDC_MIFAUTHENTKEYNR)->EnableWindow(FALSE); 
   GetDlgItem(IDC_AUTHENTKEY)->EnableWindow(TRUE); 
   UpdateData(FALSE); 
 
   } 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
void CContactlessDemoVCDlg::OnMifkeyoptions() 
   { 
   GetDlgItem(IDC_MIFTRKEYNR)->EnableWindow(TRUE); 
   m_strMifTrKeyNr = "00"; 
   m_intMifTrOption = 0; 
   UpdateData(FALSE); 
 
   } 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
void CContactlessDemoVCDlg::OnMifkeyoptionp() 
   { 
   GetDlgItem(IDC_MIFTRKEYNR)->EnableWindow(FALSE); 
   m_intMifTrOption = 1; 
   } 
 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
void CContactlessDemoVCDlg::OnWritetrkeytoreader() 
   { 
 
 
   UCHAR ucTransKey[16] = {0}; 
   ULONG ulTransKeyLen = 0; 
   ULONG ulTransKeyNr = 0; 
   ULONG ulTrTrKeyNr = 0; 
   BOOLEAN fSecuredTransmission = FALSE; 
   char Reader[80]=" "; 
   UCHAR ucKeyNr[1]={0}; 
   ULONG ulTrKeyNrLen = 0; 
 
 
   m_strTrKeyToReader.Empty(); 
   UpdateData(TRUE); 
 
   CStringToUchar( m_strTrKeyToReader,ucTransKey,&ulTransKeyLen); 
   if (ulTransKeyLen != 16) 
      { 
 
      m_ctrOutput.AddString("The Key must be of 16 bytes"); 
      TurnOnErrorLed(); 
      goto EXIT; 
 
 
      } 
 
   CStringToUchar( m_strTrKeyNoToReader,ucKeyNr,&ulTrKeyNrLen); 
   ulTransKeyNr = ucKeyNr[0]; 
 
   if (m_intTrKeyOptionS == 0) 
      { 
      fSecuredTransmission = TRUE; 
      CStringToUchar(m_strTrTrKeyNo,ucKeyNr,&ulTrKeyNrLen); 
      ulTrTrKeyNr = ucKeyNr[0]; 
      } 
   else if (m_intTrKeyOptionS == 1) 
      { 
      fSecuredTransmission = FALSE; 
 
      } 
   else 
      { 
      m_ctrOutput.AddString("One security option must be selected"); 
      TurnOnErrorLed(); 
      goto EXIT; 
      } 
 
 
 
 
   if (fCM5121Selected == TRUE) 
      { 
 
      strcpy(Reader,(LPCTSTR) m_strReaderName); 
 
      if (fCardConnected != TRUE)hCard = 0x00000000; 
 
      GetSystemTime(&startTime); 
 
//      lErr = SCardCLWriteTransmissionKeyToReader(hCard,hContext,Reader,ulTransKeyNr,ulTransKeyLen, 
//                                                 ucTransKey,fSecuredTransmission,ulTrTrKeyNr); 
 
      GetSystemTime(&stopTime); 
 
      if (lErr == NO_ERROR) 
         { 
 
         m_ctrOutput.AddString("The key has been written succesfully"); 
         ShowTimeRequired(); 
 
         } 
      else 
         { 
         ShowErrorMessage(lErr); 
         } 
      } 
   else 
      { 
 
      m_ctrOutput.AddString("For any operation one reader must be selected"); 
      TurnOnErrorLed(); 
      } 
 
 
 
   EXIT: 
   ShowCursorOutScreen(); 
   m_strMifKeyToReader.Empty(); 
   UpdateData(FALSE); 
 
 
 
   } 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
void CContactlessDemoVCDlg::OnTrkeyoptions() 
   { 
   GetDlgItem(IDC_TRTRKEYNR)->EnableWindow(TRUE); 
   m_intTrKeyOptionS = 0; 
   m_strTrTrKeyNo = "00"; 
   UpdateData(FALSE); 
 
   } 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
void CContactlessDemoVCDlg::OnTrkeyoptionp() 
   { 
   GetDlgItem(IDC_TRTRKEYNR)->EnableWindow(FALSE); 
   m_intTrKeyOptionS = 1; 
   } 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
void CContactlessDemoVCDlg::OnTransmit() 
   { 
   UCHAR ucByteSend[260] = {0}; 
   UCHAR ucByteReceive[260] = {0}; 
   ULONG ulnByteSend = 0; 
   DWORD dwRecvLength = 260; 
   char str[1024] = ""; 
   SCARD_IO_REQUEST pioSendPci; 
 
 
   if (dwActiveProtocols == SCARD_PROTOCOL_T0) 
      { 
      pioSendPci.dwProtocol= SCARD_PCI_T0->dwProtocol; 
      pioSendPci.cbPciLength= SCARD_PCI_T0->cbPciLength; 
      } 
   else 
      if (dwActiveProtocols == SCARD_PROTOCOL_T1) 
      { 
      pioSendPci.dwProtocol= SCARD_PCI_T1->dwProtocol; 
      pioSendPci.cbPciLength= SCARD_PCI_T1->cbPciLength; 
 
      } 
 
 
   UpdateData(TRUE); 
 
 
   CStringToUchar(m_strByteSend,ucByteSend,&ulnByteSend); 
 
   if (ulnByteSend != m_dwNByteSend) 
      { 
      MessageBox(" Problem in length of Data send and number of bytes send"); 
      TurnOnErrorLed(); 
      goto EXIT; 
      } 
 
   GetSystemTime(&startTime); 
 
 
   if(m_strCardName.Find("iClass") != -1) 
   { 
    
    SCard_Status = SCardCLICCTransmit(hCard,ucByteSend,m_dwNByteSend,ucByteReceive,&dwRecvLength); 
 
    
   } 
   else 
   { 
 
   SCard_Status = SCardTransmit (hCard, 
                                 &pioSendPci, 
                                 ucByteSend, 
                                 m_dwNByteSend, 
                                 NULL, 
                                 ucByteReceive, 
                                 &dwRecvLength); 
   } 
 
   GetSystemTime(&stopTime); 
 
   if (SCard_Status == 0) 
      { 
      m_ctrOutput.AddString("Transmission has been done succesfully"); 
      m_dwNByteReceive = dwRecvLength; 
      UcharToStr(ucByteReceive,dwRecvLength,str); 
      m_strByteReceive = str; 
      ShowTimeRequired(); 
      } 
   else 
      { 
      ShowErrorMessage(SCard_Status); 
      m_strByteReceive = ""; 
      TurnOnErrorLed(); 
      } 
 
 
   UpdateData(FALSE); 
   EXIT: 
   ; 
   } 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
void CContactlessDemoVCDlg::OnChangeBytesend() 
   { 
   UpdateData(TRUE); 
      if (CheckLetter(m_strByteSend)!= TRUE) 
      { 
      m_strByteSend.Remove(m_strByteSend.GetAt(m_strByteSend.GetLength()-1)); 
      } 
 
   m_dwNByteSend = m_strByteSend.GetLength()/2; 
 
   UpdateData(FALSE); 
 
   } 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
void CContactlessDemoVCDlg::OnChangeDatatowrite() 
   { 
   UpdateData(TRUE); 
         if (CheckLetter(m_strDataToWrite)!= TRUE) 
      { 
      m_strDataToWrite.Remove(m_strDataToWrite.GetAt(m_strDataToWrite.GetLength()-1)); 
      } 
 
   m_nWriteByte = m_strDataToWrite.GetLength()/2; 
   UpdateData(FALSE); 
 
   } 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
BOOLEAN CContactlessDemoVCDlg::CheckLetter(CString String) 
   { 
   int i = 0; 
   int k = 0; 
   for (i = 0;i<String.GetLength();i++) 
      { 
      if ((String.GetAt(i) >= '0' && String.GetAt(i) <= '9') ||(String.GetAt(i) >= 'A' && String.GetAt(i) <= 'F')) 
         k = 0; 
      else 
         { 
 
         MessageBox("The values must be a hex value"); 
         return FALSE; 
         } 
      } 
   return TRUE; 
   } 
 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
void CContactlessDemoVCDlg::OnChangeTrkeytoreader()  
{    
	UpdateData(TRUE); 
 
      if (CheckLetter(m_strTrKeyToReader)!= TRUE) 
      { 
      m_strTrKeyToReader.Remove(m_strTrKeyToReader.GetAt(m_strTrKeyToReader.GetLength()-1)); 
      } 
 
	 UpdateData(FALSE); 
	 
} 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
void CContactlessDemoVCDlg::OnChangeMifkeytoreader()  
{ 
	UpdateData(TRUE); 
 
      if (CheckLetter(m_strMifKeyToReader)!= TRUE) 
      { 
      m_strMifKeyToReader.Remove(m_strMifKeyToReader.GetAt(m_strMifKeyToReader.GetLength()-1)); 
      } 
 
	 UpdateData(FALSE); 
	 
	 
} 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
void CContactlessDemoVCDlg::OnChangeAuthentkey()  
{ 
	UpdateData(TRUE); 
 
      if (CheckLetter(m_strAuthentKey)!= TRUE) 
      { 
      m_strAuthentKey.Remove(m_strAuthentKey.GetAt(m_strAuthentKey.GetLength()-1)); 
      } 
 
	 UpdateData(FALSE); 
	 
} 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
void CContactlessDemoVCDlg::OnChangeIncrementval()  
{ 
	UpdateData(TRUE); 
 
      if (CheckLetter(m_strIncrementValue)!= TRUE) 
      { 
      m_strIncrementValue.Remove(m_strIncrementValue.GetAt(m_strIncrementValue.GetLength()-1)); 
      } 
 
	 UpdateData(FALSE); 
	 
} 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
void CContactlessDemoVCDlg::OnChangeDecrementval()  
{ 
	UpdateData(TRUE); 
 
      if (CheckLetter(m_strDecrementValue)!= TRUE) 
      { 
      m_strDecrementValue.Remove(m_strDecrementValue.GetAt(m_strDecrementValue.GetLength()-1)); 
      } 
 
	 UpdateData(FALSE); 
}