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

📄 test.cpp

📁 Skeleton for implementing a Windows PC/SC Smartcard Reader.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// =====[test_sc.cpp]=======================================================================
//  Description:        Source file (test_sc.cpp)
//  Compiled:           MS-VC++
//  Version:            .NET
//  Revisions:
//  REV         DATE                BY           DESCRIPTION
//  ------  --------------      ----------    --------------------------------------
//  1.01      2001/04/12           BDS           Initial version.
//    
//  This source file (test_sc.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 TEST_SC_CPP
#define TEST_SC_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 <Winscard.h>
#include <winerror.h>
#include "my_def.h"

struct tag_CmdBytes
{
   BYTE      
      bCla,   // The instruction class
      bIns,   // The instruction code 
      bP1,    // Parameter to the instruction
      bP2,    // Parameter to the instruction
      bP3;    // Size of I/O Transfer
} CmdBytes;

#define NOTIFICATIONEVENT_NAME  L"SC_INSERT_REMOVE"
#define IOCTL_CREATE_EVENT 	SCARD_CTL_CODE( 3000 )
LPCSTR         pmszTest_Data        = "TEST_DATA";    
PCHAR          pmszReaders          = NULL;
LPTSTR         pmszReaderGroups     = NULL;
LPTSTR         pReaderGroup         = NULL; //temp pointer
LONG           dwReturn              = 0;
DWORD          dwLength                  = SCARD_AUTOALLOCATE;
SCARDCONTEXT   hSC                  = NULL;
CHAR           chWork_Reader[]      ="COMRAD 000001 0";
LPTSTR         pReader              = NULL;
LPTSTR         pmszCards            = NULL;
LPTSTR         pCard                = NULL;
DWORD          cByte                = SCARD_AUTOALLOCATE;
DWORD          dwTemp               = 0;
SCARDHANDLE    hCardHandle          = NULL;
DWORD          dwAP                 = 0;
tag_CmdBytes   _CmdBytes;
bool           bOurReaderIsInstalled= false;

BYTE           bSend[128 + 5]       = {0};
BYTE           bRecv[130]           = {0}; // Buffer for  data exchange with card
DWORD          dwRecv               = 2;
DWORD          dwSend               = 4;
#ifdef INTRODUCE_SC
LPCSTR         szCardName           = "XXXX_Card";
LPTSTR         szProvider           = NULL;
DWORD          chProvider           = SCARD_AUTOALLOCATE;
#endif 
#ifdef _CRT_DEBUG
#ifdef   _DEBUG
_CrtMemState s1,s2,s3; 
#endif
#endif
//===================================================================================
//================================== AUXILIARY PROCEDURES ===========================
//===================================================================================
//<summary>
//  ======================================================================
//<newpara>	Author :BD		Date : 05-16-2001		</newpara>
//  ======================================================================
//<newpara>	Function :		GetAttr</newpara>
//<newpara>	Comments :										</newpara>
//</summary>
//Parameters :
//	<param name="dwAttr">                   				       </param>
//	<param name="lpzName">                  				       </param>
//	<param name="true">                     				       </param>
//<returns> </returns>
// ======================================================================
DWORD GetAttr(DWORD dwAttr, LPCSTR lpzName, bool bSCtring = true)
{
   //cByte          = 0;
   LPBYTE         pbAttr      = NULL;
   DWORD          dwAttrLen   = 0;
   DWORD          dwReturn    = SCARD_S_SUCCESS;
   if ((dwReturn = SCardGetAttrib(hCardHandle, dwAttr, (LPBYTE)pbAttr, 
      &dwAttrLen)) == SCARD_S_SUCCESS)
   {
      if (dwAttrLen > 0)
      {
         pbAttr = (LPBYTE)malloc(dwAttrLen + 2);
         VASSERT(pbAttr != NULL);
         VAS_VERIFY(memset(pbAttr,0,dwAttrLen+2) != NULL);
         printf("%s : ", lpzName);
         if ((dwReturn = SCardGetAttrib(hCardHandle, dwAttr, (LPBYTE)pbAttr, 
            &dwAttrLen)) != SCARD_S_SUCCESS)
         {
            if (dwReturn == ERROR_NOT_SUPPORTED)
               printf("Value not supported\n");
            else
               // Some other errors occurred.
               printf("SCardGetAttrib is failed, error code : %#X\n", dwReturn);
         }
         else
         {
            // Output the bytes.
            for (dwTemp = 0;dwTemp < dwAttrLen; dwTemp++)
            {
               if (bSCtring)
                  printf("%c", *(pbAttr + dwTemp));
               else
                  printf("%x", *(pbAttr + dwTemp));
            }
            printf("\n");
         }
      }
      if (pbAttr != NULL) free(pbAttr);
   }
   else
      printf("Failed SCardGetAttrib - %x\n", dwReturn);
   return (dwReturn);
}
//===================================================================================
//================================== 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 );
   _CrtMemCheckpoint( &s1 );
#endif
   printf("----------------------------------------------------------\n");
   printf(" <sc_test> utility, ver. 1.3.5.8 Test of virtual smartcard\n");
   printf(" Copyright (c) 1992 - 2003  Dmitry Basko www.dbasko.com\n");
   printf("------------------------------------------------------------\n");
   // let's establish the context.
   if ((dwReturn = SCardEstablishContext(SCARD_SCOPE_USER, NULL, NULL,
      &hSC)) == SCARD_S_SUCCESS )
   {
#ifdef INTRODUCE_SC
      // It should be done ONLY one time and only for one SCSP
      //but in our case we introduced virtual SC manually, during install procedure.
#endif
      // Retrieve the list the reader groups.
      // hSC was set by a previous call to SCardEstablishContext.
      // Usually only one group - SCard&DefaultsReaders will be found
      if ((dwReturn = SCardListReaderGroups(hSC, (LPTSTR)&pmszReaderGroups, 
         &dwLength)) == SCARD_S_SUCCESS)
      {
         VASSERT(dwLength != 0);
         pReaderGroup = pmszReaderGroups;
         VASSERT(pReaderGroup);
         while ('\0' != *pReaderGroup)
         {  // Display the value.
            printf("\t%s: %s\n","List of current reader groups", pReaderGroup);
            // Advance to the next value.
            pReaderGroup = pReaderGroup + strlen(pReaderGroup) + 1;
            VASSERT(pReaderGroup);
         }
         if (pmszReaderGroups != NULL) 
         {
            VAS_VERIFY(SCardFreeMemory(hSC, pmszReaderGroups) == SCARD_S_SUCCESS);
            pmszReaderGroups = NULL;   
         }
         dwLength = 0;
         //We requests readers from all groups in the system
         if ((dwReturn = SCardListReaders(hSC, NULL, NULL, &dwLength)) == SCARD_S_SUCCESS)
         {
            VASSERT(dwLength != 0);
            pmszReaders = (char*) malloc(dwLength);

⌨️ 快捷键说明

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