⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 Skeleton for implementing a Windows PC/SC Smartcard Reader.
💻 CPP
字号:
// =====[main.cpp]=======================================================================
//  Description:        Source file (main.cpp)
//  Compiled:           MS-VC++
//  Version:            .NET
//  Revisions:
//  REV         DATE                BY           DESCRIPTION
//  ------  --------------      ----------    --------------------------------------
//  1.01      2001/01/01           BDS           Initial version.
//    
//  This source file (main.cpp) is  Copyright(c) 2001 Dmitry Basko. 
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that: 
//  1.  source code distributions retain the above copyright notice and this 
//      paragraph in its entirety;
//  2.  distributions including binary code include the above copyright notice
//      and this paragraph in its entirety in the documentation or other materials
//      provided with the distribution;
//  3.  all advertising materials mentioning features or use of this software 
//      display the following acknowledgement;
//  "This product includes software developed by Dmitry Basko."
//  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
//  WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
//  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
// ========================================================================================= 
#pragma once
#ifndef MAIN_CPP
#define MAIN_CPP
// This programm is compiled ONLY for WIN32 and M_IX86 processor
#if defined(_WIN32) && defined(_M_IX86)
#include <stdio.h>
#include <winscard.h>
#include <tchar.h>
#include <conio.h>
#include "my_def.h"

HRESULT           hr                 = S_OK;
LPTSTR            szReaders, szRdr;
DWORD             cchReaders         = SCARD_AUTOALLOCATE;
DWORD             dwI, dwRdrCount;
SCARD_READERSTATE rgscState[MAXIMUM_SMARTCARD_READERS];
TCHAR             szCard[MAX_PATH];
SCARDCONTEXT      hSC;
LONG              dwReturn;
BOOL              bCardInserted     = false;

#ifdef _CRT_DEBUG
#ifdef   _DEBUG
_CrtMemState s1,s2,s3; 
#endif
#endif

void main()
{
#ifdef _CRT_DEBUG
   _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );
   _CrtSetReportFile( _CRT_WARN,  _CRTDBG_FILE_STDOUT);
   _CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE );
   _CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDOUT );
   _CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_FILE );
   _CrtSetReportFile( _CRT_ASSERT, _CRTDBG_FILE_STDOUT );
   SET_CRT_DEBUG_FIELD( _CRTDBG_DELAY_FREE_MEM_DF );
   SET_CRT_DEBUG_FIELD( _CRTDBG_ALLOC_MEM_DF );
   SET_CRT_DEBUG_FIELD( _CRTDBG_LEAK_CHECK_DF );
   //_CrtMemDumpAllObjectsSince( NULL );
   _CrtMemCheckpoint( &s1 );
#endif
   printf("------------------------------------------------------------\n");
   printf(" <sc_mon> utility, ver. 1.3.5.8 Monitor of virtual smartcard\n");
   printf(" Copyright (c) 1992 - 2003  Dmitry Basko www.dbasko.com\n");
   printf("------------------------------------------------------------\n");
   // Let's establish the card to watch for. Multiple cards can be looked for, but
   // this sample looks for only one card.
   VAS_VERIFY(lstrcpy(szCard, TEXT("XXXX_card")) != NULL);
   szCard[lstrlen(szCard) + 1] = 0;  // Double trailing zero.
   if ((dwReturn = SCardEstablishContext(SCARD_SCOPE_USER,NULL,NULL,&hSC)) == SCARD_S_SUCCESS)
   {
      if ((dwReturn = SCardListReaders(hSC,NULL,(LPTSTR)&szReaders,&cchReaders)) == SCARD_S_SUCCESS)
      {
         // Place the readers into the state array.
         szRdr = szReaders;
         for (dwI = 0; dwI < MAXIMUM_SMARTCARD_READERS; dwI++)
         {
            if (0 == *szRdr) break;
            rgscState[dwI].szReader = szRdr;
            printf("Reader %s was found in the system\n",szRdr);
            rgscState[dwI].dwCurrentState = SCARD_STATE_UNAWARE;
            szRdr += lstrlen(szRdr) + 1;
         }
         dwRdrCount = dwI;
         // If any readers are available, proceed.
         if (dwRdrCount != 0)
         {
            for (;dwReturn == SCARD_S_SUCCESS;)
            { 
               // Look for the card.
               if ((dwReturn = SCardLocateCards(hSC,szCard,rgscState,dwRdrCount)) == SCARD_S_SUCCESS)
               {
                  // Look through the array of readers.
                  for (dwI = 0; dwI < dwRdrCount; dwI++)
                  {
                     if ((SCARD_STATE_ATRMATCH & rgscState[dwI].dwEventState) != 0)
                     {
                        // Update the state.
                        _tprintf(TEXT("Virtual smartcard '%s' was found in the virtual reader '%s'.\n"),
                           szCard,rgscState[dwI].szReader);
                        VAS_VERIFY(SCardFreeMemory(hSC,szReaders) == SCARD_S_SUCCESS);
                        bCardInserted = true;
                        dwReturn = 1;
                        break;
                     }
                     rgscState[dwI].dwCurrentState = rgscState[dwI].dwEventState;
                  }
                  if (!bCardInserted)
                  {
                     // Card not found yet; wait until there is a change.
                     printf("Virtual smartcard WAS NOT found in reader .\n Please Insert XXXX_card, utility is waithing ...\n");
                     if ((dwReturn = SCardGetStatusChange(hSC,INFINITE,rgscState,dwRdrCount)) != SCARD_S_SUCCESS)
                        printf("Failed SCardGetStatusChange\n");
                  }
               }
               else
                  printf("Failed SCardLocateCards, error %#X\n",dwReturn);
            }  
         }
         else
            printf("There is no readers, properly installed\n");
         // Release the context.
         if ((dwReturn = SCardReleaseContext(hSC)) != SCARD_S_SUCCESS)
            printf("Failed SCardReleaseContext, error %#X\n",dwReturn);
      }
      else
         printf("Failed SCardListReaders, error %#X\n",dwReturn);
   }
   else
      printf("Failed SCardEstablishContext, error %#X\n",dwReturn);
   if (szReaders) SCardFreeMemory(hSC, szReaders);
   if (hSC) SCardReleaseContext(hSC);
#ifdef _DEBUG
   printf("Press Enter for exit..\n");
   while (!_getch());
#endif
#ifdef _CRT_DEBUG
   _CrtMemCheckpoint( &s2 );
   if ( _CrtMemDifference( &s3, &s1, &s2) ) 
      _CrtMemDumpStatistics( &s3 );
   //_CrtMemDumpAllObjectsSince( NULL );
   _CrtDumpMemoryLeaks( );
#endif
}
#else
#error Modification required for this type of OS
#endif
//----------------------------------------------------------------------------------------------
#endif // MAIN_CPP

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -