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

📄 pcscdlg.cpp

📁 同方PC读卡器驱动
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// PCSCDlg.cpp : implementation file
//

#include "stdafx.h"
#include "PCSC.h"
#include "PCSCDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//////////////////////////////////////////////////////////////////////
// CPCSCDlg dialog

CPCSCDlg::CPCSCDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPCSCDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPCSCDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CPCSCDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPCSCDlg)
	DDX_Control(pDX, IDC_EDIT7, m_DataIn);
	DDX_Control(pDX, IDC_EDIT6, m_Le);
	DDX_Control(pDX, IDC_EDIT5, m_Lc);
	DDX_Control(pDX, IDC_EDIT4, m_P2);
	DDX_Control(pDX, IDC_EDIT3, m_P1);
	DDX_Control(pDX, IDC_EDIT2, m_INS);
	DDX_Control(pDX, IDC_EDIT1, m_CLA);
	DDX_Control(pDX, IDC_LIST1, m_list);
	DDX_Control(pDX, IDC_COMBO1, m_rlist);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPCSCDlg, CDialog)
	//{{AFX_MSG_MAP(CPCSCDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_RESET, OnReset)
	ON_CBN_SELCHANGE(IDC_COMBO1, OnSelchangeCombo1)
	ON_BN_CLICKED(IDC_APDU, OnApdu)
	ON_EN_SETFOCUS(IDC_EDIT7, OnSetfocusEdit7)
	ON_EN_CHANGE(IDC_EDIT7, OnChangeEdit7)
	ON_EN_MAXTEXT(IDC_EDIT7, OnMaxtextEdit7)
	ON_BN_CLICKED(IDC_EJECT, OnEject)
	ON_BN_CLICKED(IDC_Refresh, OnRefresh)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPCSCDlg message handlers

BOOL CPCSCDlg::OnInitDialog()
{
	
	
	CDialog::OnInitDialog();

	// 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
	
	CPCSCDlg::OnRefresh();
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 CPCSCDlg::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 CPCSCDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CPCSCDlg::OnReset() 
{
	CString sReset;

	int nIndex = m_rlist.GetCurSel();
	int nCount = m_rlist.GetCount();
	if ((nIndex != LB_ERR) && (nCount >= 1))
	{
	/*------------------------------------------------------------------------------
		Establish a context with the card
	------------------------------------------------------------------------------*/
	dwShareMode = SCARD_SHARE_SHARED;
	dwPreferedProtocol = SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1;
	lRet = SCardConnect(hContext,(LPCTSTR)szFirstReader,dwShareMode,
						dwPreferedProtocol,	&hCard,	&dwProtocol);
   
	sReset.Format("SCardConnect: %Xh",lRet);
	m_list.AddString(sReset); 
	if (lRet != SCARD_S_SUCCESS)
	{
		//CDialog::OnOK();  
        //return;
	}
	lRet = SCardState(hCard,&dwState,	&dwProtocol,szAtr,&dwAtrLength);
	if (lRet != SCARD_S_SUCCESS)
	{
		CPCSCDlg::OnEject(); 
	}
	else {	
	
	m_list.AddString("===RESET CARD===");
	if (szAtr[5] == 65)
	{
		bACOS = 1;
		if (szAtr[6] == 1)
		{
			switch (szAtr[7])
			{
			case 16:
				m_list.AddString("ACOS Card  Version 1.0 Revision 1.0");
				break;
			case 32:
				m_list.AddString("ACOS Card  Version 1.0 Revision 2.0");
				break;
			}
			switch (szAtr[16])
			{
			case 0:
				m_list.AddString("Life Cycle Stage :  User Stage");
				break;
			case 1:
				m_list.AddString("Life Cycle Stage :  Manufacturing Stage");
				break;
			case 2:
				m_list.AddString("Life Cycle Stage :  Personalization Stage");
				break;
			}
		}
	}
	sReset.Format("Protocol:   %d",dwProtocol);
	m_list.AddString(sReset);
	sReset.Format("ATR Length:  %d",dwAtrLength);
	m_list.AddString(sReset);
	CString sOut;
	sOut = "ATR :  "; 
	for (dwIndex=0; dwIndex<dwAtrLength; dwIndex++)
	{
		sReset.Format("%02X ",szAtr[dwIndex]);
		sOut = sOut + sReset;
	}

	m_list.AddString(sOut);
	}
	int nCount = m_list.GetCount(); 
	m_list.SetCurSel(nCount-1); 
	
	}
}

void CPCSCDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	/*------------------------------------------------------------------------------
		Terminates the opened connection with the card.
	------------------------------------------------------------------------------*/
	lRet = SCardDisconnect(hCard,SCARD_LEAVE_CARD);
   

	/*------------------------------------------------------------------------------
		Close the establish context with the ICC Resource Manager.
	------------------------------------------------------------------------------*/
	lRet = SCardReleaseContext(hContext);
   
	CDialog::OnOK();
}

void CPCSCDlg::OnMaxtextEdit7()
{
}

void CPCSCDlg::OnSelchangeCombo1() 
{
	// TODO: Add your control notification handler code here
	CString 
		cmb_rlist,
		sStr;
	
	m_rlist.GetWindowText(cmb_rlist); 
	m_list.AddString(cmb_rlist); 
	dwIndex = 0;
	dwReader = 0;
	int nIndex = m_rlist.GetCurSel();
	int nCount = m_rlist.GetCount();
/*------------------------------------------------------------------------------
	Select a reader
------------------------------------------------------------------------------*/
	if ((nIndex != LB_ERR) && (nCount >= 1))
	{
	
		dwSelect = (DWORD) nIndex + 1;
		szFirstReader = (LPTSTR)szListReaders;
		while (*(szFirstReader) != '\0')
		{
			if (dwSelect == (dwReader + 1))
			break;
			dwReader++;
			dwIndex += strlen(szFirstReader) + 1;
			szFirstReader = (LPTSTR)(szListReaders + dwIndex);
		}
/*------------------------------------------------------------------------------
	Tracking cards within the specified reader
------------------------------------------------------------------------------*/
		SRState.szReader = szFirstReader;
		SRState.pvUserData = NULL;
		SRState.dwCurrentState = SCARD_STATE_IGNORE;
		SRState.dwEventState = SCARD_STATE_CHANGED;
		while (TRUE)
		{
			lRet = SCardGetStatusChange(hContext, 8, &SRState, 1);
			if (lRet == SCARD_S_SUCCESS)
			{
      			m_list.AddString("SCardGetStatusChange OK");
	  			sStr.Format("Card current state = %04X", SRState.dwCurrentState);
				m_list.AddString(sStr);
			} else
			{
      			sStr.Format("SCardGetStatusChange fail! ErrorCode = %08X",lRet);
				m_list.AddString(sStr);
				break;
			}
		}
	}
	int sCount = m_list.GetCount(); 
	m_list.SetCurSel(sCount-1); 
}

void CPCSCDlg::OnApdu() 
{
	// TODO: Add your control notification handler code here
	CString sText;
	int iLc=0, iLe=0 ;
	
	dwLengthIn=0;
	
	m_CLA.GetWindowText(sText);
	nData = (BYTE) ( (BYTE) AscToByte( sText[0] ) << 4 ) | (BYTE) ( AscToByte( sText[1] ) ) ;
	szMyDataIn.bCla = (BYTE)nData;
	szDataIn[dwLengthIn++] = nData;
	
	m_INS.GetWindowText(sText);
	nData = (BYTE) ( (BYTE) AscToByte( sText[0] ) << 4 ) | (BYTE) ( AscToByte( sText[1] ) ) ;
	szDataIn[dwLengthIn++] =  nData;
	
	m_P1.GetWindowText(sText);
	nData = (BYTE) ( (BYTE) AscToByte( sText[0] ) << 4 ) | (BYTE) ( AscToByte( sText[1] ) ) ;
	szDataIn[dwLengthIn++] =  nData;
	
	m_P2.GetWindowText(sText);
	nData = (BYTE) ( (BYTE) AscToByte( sText[0] ) << 4 ) | (BYTE) ( AscToByte( sText[1] ) ) ;
	szDataIn[dwLengthIn++] =  nData;

	m_Lc.GetWindowText(sText);
	iLc = (BYTE) ( (BYTE) AscToByte( sText[0] ) << 4 ) | (BYTE) ( AscToByte( sText[1] ) ) ;

	m_Le.GetWindowText(sText);
	iLe = (BYTE) ( (BYTE) AscToByte( sText[0] ) << 4 ) | (BYTE) ( AscToByte( sText[1] ) ) ;


	if ( iLc > 0 )
	{
		CString s,temp;
		int j;
		int nLen = m_DataIn.GetWindowTextLength(); 

		szDataIn[dwLengthIn++] = iLc ;

		m_DataIn.GetWindowText(s); 

		j=0 ;
		while ( j < nLen )
		{
			nData = (BYTE) ( (BYTE) AscToByte( s[j] ) << 4 ) | (BYTE) ( AscToByte( s[j+1] ) ) ;
			szDataIn[dwLengthIn++] = (BYTE) nData;
			j += 3 ;
		}
	}
	
/*------------------------------------------------------------------------------
         Read Lex
------------------------------------------------------------------------------*/
	if ( dwProtocol == SCARD_PROTOCOL_T1 )
	{
		nLenOut = iLe ;

		if ( iLe > 0 )
		{
			szDataIn[dwLengthIn++] = iLe;
		}
	}
/*------------------------------------------------------------------------------

⌨️ 快捷键说明

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