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

📄 ins_rem.cpp

📁 Skeleton for implementing a Windows PC/SC Smartcard Reader.
💻 CPP
字号:
// =====[ins_rem.cpp]=======================================================================
//  Description:        Source file (ins_rem.cpp)
//  Compiled:           MS-VC++
//  Version:            .NET
//  Revisions:
//  REV         DATE                BY           DESCRIPTION
//  ------  --------------      ----------    --------------------------------------
//  1.01      2001/01/01           BDS           Initial version.
//    
//  This source file (ins_rem.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 INS_REM_CPP
#define INS_REM_CPP
// This programm is compiled ONLY for WIN32 and M_IX86 processor
#if defined(_WIN32) && defined(_M_IX86)

#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <Winscard.h>
#include <winerror.h>
#include "my_def.h"

#define NOTIFICATIONEVENT_NAME  L"SC_INSERT_REMOVE"
#define IOCTL_CREATE_EVENT 	SCARD_CTL_CODE( 3000 )

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

LONG            dwReturn           = 0;
DWORD           dwAP              = 0;
BYTE            bSend[128 + 5]    = {0};
BYTE            bRecv[130]        = {0}; // Buffer for exchange data with card
DWORD           dwRecv            = 0;
DWORD           dwSend            = 0;
SCARDCONTEXT    hSC               = NULL;
CHAR            chWork_Reader[]             ="COMRAD 000001 0";
SCARDHANDLE     hCardHandle       = NULL;
HANDLE          hEvent           = 0;
DWORD           dwReturnBytes     = 0;
CHAR            ch                = 0;
//===================================================================================
//================================== Main procedure ===========================
//===================================================================================

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(" <ins_rem> utility, ver. 1.3.5.8 Simulator of the virtual smartcard presence\n");
   printf(" Copyright (c) 1992 - 2003 Dmitry Basko www.dbasko.com\n");
   printf("----------------------------------------------------------------------------\n");
   
   if (((hEvent  = OpenEventW(EVENT_ALL_ACCESS, NULL, 
      NOTIFICATIONEVENT_NAME))) == NULL)
   {  //EVENT was not created yet. Let's do it 
      printf("Handle of EVENT, created in the pc/sc driver was not found\n \tLet's create it. \n");
      // Establish the context.
      if ((dwReturn = SCardEstablishContext(SCARD_SCOPE_USER, NULL, NULL, 
         &hSC)) == SCARD_S_SUCCESS)
      {
         // I will point our pseudo reader DIRECTLY
         // Our card support T0 ISO 7816 protocol ONLY
         if ((dwReturn = SCardConnect(hSC, (LPCSTR)&chWork_Reader, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0,
            &hCardHandle, &dwAP)) == SCARD_S_SUCCESS)
         {
            VASSERT(hCardHandle != NULL);
            if (dwAP == SCARD_PROTOCOL_T0)
            { 
               // Let's try to start CreateEvent via calling VENDOR_IOCTL code
               dwReturnBytes = 0;
               dwRecv =  sizeof(bRecv);
               ZeroMemory(bRecv, sizeof(bRecv));
               if ((dwReturn = SCardControl(hCardHandle, IOCTL_CREATE_EVENT, NULL, 0, bRecv, dwRecv, 
                  &dwReturnBytes)) == SCARD_S_SUCCESS)
               {
                  printf("Auxilary EVENT <SC_INSERT_REMOVE> was created with SCardControl reguest\n");
                  printf("pcsc_drv returned answer (%d bytes): %s\n",dwReturnBytes,(char*)bRecv);
                  if (((hEvent  = OpenEventW(EVENT_ALL_ACCESS, NULL, 
                     NOTIFICATIONEVENT_NAME))) == NULL)
                     printf("EVENT SC_INSERT_REMOVE was not opened, something wrong with pcsc_drv\n");
               }
               else
                  printf("Failed SCardControl, most likely EVENT was not created \n in the driver, error %#X\n",dwReturn);
            }
            else
               printf("Failed SCardConnect, unsupported protocol, error %#X\n",dwReturn);
            SCardDisconnect(hSC, SCARD_LEAVE_CARD);
         }
         else
         {
            if (dwReturn == SCARD_W_REMOVED_CARD)
               printf("Failed SCardConnect, virtual smartcard was removed \n from the virtual reader before, error %#X\n",
               dwReturn);
            if (dwReturn == SCARD_E_UNKNOWN_CARD) 
               printf(" SCardConnect returned error : SCARD_E_UNKNOWN_CARD\n");
            else 
               printf("Failed SCardConnect, error %#X\n",dwReturn);
         }
         if (hSC != NULL)  VAS_VERIFY(SCardReleaseContext(hSC) == SCARD_S_SUCCESS);
      }
      else
         printf("Failed SCardEstablishContext, error %#X\n",dwReturn);
   }
   if (hEvent != NULL)
   {
      printf("\n\tPlease press 1 to emulate sequentially ins. or rem state of virt. SC\n");
      printf("\tPlease press ESC to finish the program\n\n");
      while ((ch = LOBYTE(LOWORD(_getch()))) != 0x1B)
      {
         if (ch == '1')
         {
            if (hEvent == NULL)
            {
               if (((hEvent  = OpenEventW(EVENT_ALL_ACCESS, NULL, 
                  NOTIFICATIONEVENT_NAME))) == NULL)
               {
                  printf("EVENT SC_INSERT_REMOVE was not opened, something wrong with pcsc_drv\n");
                  break;
               }
            }
            VASSERT(SetEvent(hEvent) > 0);
            VAS_VERIFY(CloseHandle(hEvent) != 0);
            hEvent = NULL;
            printf("\t**********************************************************************\n");
            printf("\tThe state of reader COMRAD 000001 0 was changed.\n\tVirtual smartcard was inserted or removed (use sc_mon utility)\n");
            printf("\n\tPlease press 1 to emulate sequentially ins. or rem state of virt. SC\n");
            printf("\tPlease press ESC to finish the program\n\n");
            printf("\t**********************************************************************\n");
         }
         else
         {
            printf("The pressed key is unknown\n");
            printf("\n\tPlease press 1 to emulate sequentially ins. or rem state of virt. SC\n");
            printf("\tPlease press ESC to finish the program\n\n");
         }
      }
      if (hEvent != NULL) VAS_VERIFY(CloseHandle(hEvent) != 0);
   }
#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 // INS_REM_CPP

⌨️ 快捷键说明

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