📄 batchtestdlg.cpp
字号:
// BatchTestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "BatchTest.h"
#include "BatchTestDlg.h"
#include <winscard.h>
#include <winsmcrd.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CBatchTestDlg dialog
CBatchTestDlg::CBatchTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CBatchTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CBatchTestDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CBatchTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CBatchTestDlg)
DDX_Control(pDX, IDC_ETEXT, m_CETEXT);
DDX_Control(pDX, IDC_TESTRESULT, m_CTESTRESULT);
DDX_Control(pDX, IDC_VIEWLOG, m_CVIEWLOG);
DDX_Control(pDX, IDC_RESULTLIST, m_CRESULTLIST);
DDX_Control(pDX, IDC_COMBOREADERS, m_CCOMBOREADERS);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CBatchTestDlg, CDialog)
//{{AFX_MSG_MAP(CBatchTestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_TEST, OnTest)
ON_BN_CLICKED(IDC_VIEWLOG, OnViewlog)
ON_CBN_EDITCHANGE(IDC_COMBOREADERS, OnEditchangeComboreaders)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBatchTestDlg message handlers
BOOL CBatchTestDlg::OnInitDialog()
{
LPTSTR pReader ;
CDialog::OnInitDialog();
this->SetWindowPos( &wndTopMost, 15, 30, 490, 120, SWP_NOREPOSITION ) ;
m_CRESULTLIST.ShowWindow( SW_HIDE ) ;
m_CETEXT.SetWindowText( "Detail log" ) ;
m_CETEXT.ShowWindow( SW_HIDE ) ;
// Add "About..." menu item to system menu.
// 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
cch = SCARD_AUTOALLOCATE ;
m_CRESULTLIST.ResetContent() ;
// TODO: Add extra initialization here
lastReturn = SCardEstablishContext( SCARD_SCOPE_USER, NULL, NULL, &sc ) ;
PrintPCSCError( "SCardEstablishContext", lastReturn ) ;
lastReturn = SCardListReaders( sc, NULL,(LPTSTR)&pmszReaders, &cch );
PrintPCSCError( "SCardListReaders", lastReturn ) ;
//if ( lastReturn == SCARD_E_NO_READERS_AVAILABLE )
//{
// MessageBox( "No readers availiable !", "Smart card test", MB_OK ) ;
//}
pReader = pmszReaders;
while ( '\0' != *pReader )
{
// Display the value. printf("Reader: %S\n", pReader );
// Advance to the next value.
m_CCOMBOREADERS.AddString( pReader ) ;
pReader = pReader + strlen(pReader) + 1;
}
lastReturn = SCardFreeMemory( sc, pmszReaders );
PrintPCSCError( "SCardFreeMemory", lastReturn ) ;
return TRUE; // return TRUE unless you set the focus to a control
}
void CBatchTestDlg::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 CBatchTestDlg::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 CBatchTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CBatchTestDlg::OnTest()
{
// TODO: Add your control notification handler code here
CString temp ;
char ReaderName[1024] ;
DWORD dwState, dwProtocol, ATRlen, RdrLen, i=0, byteret ;
BYTE atr[128] ;
BYTE bytein[256], byteout[256] ;
SCARD_IO_REQUEST sior ;
m_CRESULTLIST.ResetContent() ;
if ( m_CCOMBOREADERS.GetCurSel() == CB_ERR )
{
MessageBox( "Please select readers first !", "Test", MB_OK ) ;
m_CTESTRESULT.SetWindowText( "" ) ;
m_CVIEWLOG.EnableWindow( TRUE ) ;
return ;
}
m_CCOMBOREADERS.GetLBText( m_CCOMBOREADERS.GetCurSel(), temp ) ;
m_CRESULTLIST.AddString( "===========" ) ;
lastReturn = SCardConnect( sc, (LPCTSTR)temp, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, &sh, &ConnectedProtocol ) ;
PrintPCSCError( "SCardConnect", lastReturn ) ;
if ( lastReturn != SCARD_S_SUCCESS )
{
m_CTESTRESULT.SetWindowText( "Failed" ) ;
m_CVIEWLOG.EnableWindow( TRUE ) ;
return ;
}
if ( ConnectedProtocol & SCARD_PROTOCOL_T0 )
{
m_CRESULTLIST.AddString( "Support T0 protocol" ) ;
}
if ( ConnectedProtocol & SCARD_PROTOCOL_T1 )
{
m_CRESULTLIST.AddString( "Support T1 protocol" ) ;
}
m_CRESULTLIST.AddString( "===========" ) ;
RdrLen = 200 ;
lastReturn = SCardStatus( sh, (LPTSTR)ReaderName, &RdrLen, &dwState, &dwProtocol, (LPBYTE) atr, &ATRlen ) ;
PrintPCSCError( "SCardStatus", lastReturn ) ;
if ( lastReturn != SCARD_S_SUCCESS )
{
m_CTESTRESULT.SetWindowText( "Failed" ) ;
m_CVIEWLOG.EnableWindow( TRUE ) ;
return ;
}
wsprintf( buffer, "Reader len=%ld name=%s", RdrLen, ReaderName ) ;
m_CRESULTLIST.AddString( buffer ) ;
wsprintf( buffer, "Card state: " ) ;
if ( (DWORD) ( dwState & SCARD_ABSENT ) == SCARD_ABSENT )
{
strcat( buffer, "SCARD_ABSENT " ) ;
}
if ( (DWORD) ( dwState & SCARD_PRESENT ) == SCARD_PRESENT )
{
strcat( buffer, "SCARD_PRESENT " ) ;
}
if ( (DWORD) ( dwState & SCARD_SWALLOWED ) == SCARD_SWALLOWED )
{
strcat( buffer, "SCARD_SWALLOWED " ) ;
}
if ( (DWORD) ( dwState & SCARD_POWERED ) == SCARD_POWERED )
{
strcat( buffer, "SCARD_POWERED " ) ;
}
if ( (DWORD) ( dwState & SCARD_NEGOTIABLE ) == SCARD_NEGOTIABLE )
{
strcat( buffer, "SCARD_NEGOTIABLE " ) ;
}
if ( (DWORD) ( dwState & SCARD_SPECIFIC ) == SCARD_SPECIFIC )
{
strcat( buffer, "SCARD_SPECIFIC " ) ;
}
m_CRESULTLIST.AddString( buffer ) ;
wsprintf( buffer, "Current protocol: " ) ;
if ( (DWORD) ( ConnectedProtocol & SCARD_PROTOCOL_T0 ) == SCARD_PROTOCOL_T0 )
{
strcat( buffer, "T0 " ) ;
}
if ( (DWORD) ( ConnectedProtocol & SCARD_PROTOCOL_T1 ) == SCARD_PROTOCOL_T1 )
{
strcat( buffer, "T1 " ) ;
}
m_CRESULTLIST.AddString( buffer ) ;
wsprintf( buffer, "ATR len=%ld", ATRlen ) ;
m_CRESULTLIST.AddString( buffer ) ;
for (i=0; i<ATRlen; i++)
{
char tmpbuf[8] ;
wsprintf( tmpbuf, "%.2X ", atr[i] ) ;
if ( i==0 )
strcpy( buffer, tmpbuf ) ;
else
strcat( buffer, tmpbuf ) ;
}
m_CRESULTLIST.AddString( buffer ) ;
memcpy( byteout, "\x00\xA4\x00\x00\x02\x3F\x00", 7 ) ;
wsprintf( buffer, "Command len=%ld", 7 ) ;
m_CRESULTLIST.AddString( buffer ) ;
for (i=0; i<7; i++)
{
char tmpbuf[8] ;
wsprintf( tmpbuf, "%.2X ", byteout[i] ) ;
if ( i==0 )
strcpy( buffer, tmpbuf ) ;
else
strcat( buffer, tmpbuf ) ;
}
m_CRESULTLIST.AddString( buffer ) ;
byteret = 200;
sior.dwProtocol = dwProtocol;
sior.cbPciLength = sizeof(SCARD_IO_REQUEST);
m_CRESULTLIST.AddString( "===========" ) ;
lastReturn = SCardTransmit( sh, &sior, (LPCBYTE) byteout, 7, NULL, bytein, &byteret);
PrintPCSCError( "SCardTransmit", lastReturn ) ;
if ( lastReturn != SCARD_S_SUCCESS )
{
m_CTESTRESULT.SetWindowText( "Failed" ) ;
m_CVIEWLOG.EnableWindow( TRUE ) ;
return ;
}
wsprintf( buffer, "Command return len=%ld", byteret ) ;
m_CRESULTLIST.AddString( buffer ) ;
for (i=0; i<byteret; i++)
{
char tmpbuf[8] ;
wsprintf( tmpbuf, "%.2X ", bytein[i] ) ;
if ( i==0 )
strcpy( buffer, tmpbuf ) ;
else
strcat( buffer, tmpbuf ) ;
}
m_CRESULTLIST.AddString( buffer ) ;
m_CRESULTLIST.AddString( "===========" ) ;
lastReturn = SCardDisconnect( sh, SCARD_LEAVE_CARD ) ;
PrintPCSCError( "SCardDisconnect", lastReturn ) ;
m_CRESULTLIST.AddString( "===========" ) ;
m_CTESTRESULT.SetWindowText( "Succeeded" ) ;
m_CVIEWLOG.EnableWindow( TRUE ) ;
}
void CBatchTestDlg::PrintPCSCError( const char header[], LONG pcscerror)
{
switch ( pcscerror )
{
case SCARD_S_SUCCESS:
wsprintf( buffer, "%s: %s", header, "SCARD_S_SUCCESS" ) ;
break ;
case SCARD_F_INTERNAL_ERROR:
wsprintf( buffer, "%s: %s", header, "SCARD_F_INTERNAL_ERROR" ) ;
break ;
case SCARD_E_CANCELLED:
wsprintf( buffer, "%s: %s", header, "SCARD_E_CANCELLED" ) ;
break ;
case SCARD_E_INVALID_HANDLE:
wsprintf( buffer, "%s: %s", header, "SCARD_E_INVALID_HANDLE" ) ;
break ;
case SCARD_E_INVALID_PARAMETER:
wsprintf( buffer, "%s: %s", header, "SCARD_E_INVALID_PARAMETER" ) ;
break ;
case SCARD_E_INVALID_TARGET:
wsprintf( buffer, "%s: %s", header, "SCARD_E_INVALID_TARGET" ) ;
break ;
case SCARD_E_NO_MEMORY:
wsprintf( buffer, "%s: %s", header, "SCARD_E_NO_MEMORY" ) ;
break ;
case SCARD_F_WAITED_TOO_LONG:
wsprintf( buffer, "%s: %s", header, "SCARD_F_WAITED_TOO_LONG" ) ;
break ;
case SCARD_E_INSUFFICIENT_BUFFER:
wsprintf( buffer, "%s: %s", header, "SCARD_E_INSUFFICIENT_BUFFER" ) ;
break ;
case SCARD_E_UNKNOWN_READER:
wsprintf( buffer, "%s: %s", header, "SCARD_E_UNKNOWN_READER" ) ;
break ;
case SCARD_E_TIMEOUT:
wsprintf( buffer, "%s: %s", header, "SCARD_E_TIMEOUT" ) ;
break ;
case SCARD_E_SHARING_VIOLATION:
wsprintf( buffer, "%s: %s", header, "SCARD_E_SHARING_VIOLATION" ) ;
break ;
case SCARD_E_NO_SMARTCARD:
wsprintf( buffer, "%s: %s", header, "SCARD_E_NO_SMARTCARD" ) ;
break ;
case SCARD_E_UNKNOWN_CARD:
wsprintf( buffer, "%s: %s", header, "SCARD_E_UNKNOWN_CARD" ) ;
break ;
case SCARD_E_CANT_DISPOSE:
wsprintf( buffer, "%s: %s", header, "SCARD_E_CANT_DISPOSE" ) ;
break ;
case SCARD_E_PROTO_MISMATCH:
wsprintf( buffer, "%s: %s", header, "SCARD_E_PROTO_MISMATCH" ) ;
break ;
case SCARD_E_NOT_READY:
wsprintf( buffer, "%s: %s", header, "SCARD_E_NOT_READY" ) ;
break ;
case SCARD_E_INVALID_VALUE:
wsprintf( buffer, "%s: %s", header, "SCARD_E_INVALID_VALUE" ) ;
break ;
case SCARD_E_SYSTEM_CANCELLED:
wsprintf( buffer, "%s: %s", header, "SCARD_E_SYSTEM_CANCELLED" ) ;
break ;
case SCARD_F_COMM_ERROR:
wsprintf( buffer, "%s: %s", header, "SCARD_F_COMM_ERROR" ) ;
break ;
case SCARD_F_UNKNOWN_ERROR:
wsprintf( buffer, "%s: %s", header, "SCARD_F_UNKNOWN_ERROR" ) ;
break ;
case SCARD_E_INVALID_ATR:
wsprintf( buffer, "%s: %s", header, "SCARD_E_INVALID_ATR" ) ;
break ;
case SCARD_E_NOT_TRANSACTED:
wsprintf( buffer, "%s: %s", header, "SCARD_E_NOT_TRANSACTED" ) ;
break ;
case SCARD_E_READER_UNAVAILABLE:
wsprintf( buffer, "%s: %s", header, "SCARD_E_READER_UNAVAILABLE" ) ;
break ;
case SCARD_P_SHUTDOWN:
wsprintf( buffer, "%s: %s", header, "SCARD_P_SHUTDOWN" ) ;
break ;
case SCARD_E_PCI_TOO_SMALL:
wsprintf( buffer, "%s: %s", header, "SCARD_E_PCI_TOO_SMALL" ) ;
break ;
case SCARD_E_READER_UNSUPPORTED:
wsprintf( buffer, "%s: %s", header, "SCARD_E_READER_UNSUPPORTED" ) ;
break ;
case SCARD_E_DUPLICATE_READER:
wsprintf( buffer, "%s: %s", header, "SCARD_E_DUPLICATE_READER" ) ;
break ;
case SCARD_E_CARD_UNSUPPORTED:
wsprintf( buffer, "%s: %s", header, "SCARD_E_CARD_UNSUPPORTED" ) ;
break ;
case SCARD_E_NO_SERVICE:
wsprintf( buffer, "%s: %s", header, "SCARD_E_NO_SERVICE" ) ;
break ;
case SCARD_E_SERVICE_STOPPED:
wsprintf( buffer, "%s: %s", header, "SCARD_E_SERVICE_STOPPED" ) ;
break ;
case SCARD_W_UNSUPPORTED_CARD:
wsprintf( buffer, "%s: %s", header, "SCARD_W_UNSUPPORTED_CARD" ) ;
break ;
case SCARD_W_UNRESPONSIVE_CARD:
wsprintf( buffer, "%s: %s", header, "SCARD_W_UNRESPONSIVE_CARD" ) ;
break ;
case SCARD_W_UNPOWERED_CARD:
wsprintf( buffer, "%s: %s", header, "SCARD_W_UNPOWERED_CARD" ) ;
break ;
case SCARD_W_RESET_CARD:
wsprintf( buffer, "%s: %s", header, "SCARD_W_RESET_CARD" ) ;
break ;
case SCARD_W_REMOVED_CARD:
wsprintf( buffer, "%s: %s", header, "SCARD_W_REMOVED_CARD" ) ;
break ;
default:
wsprintf( buffer, "%s: %.8X", header, pcscerror ) ;
break ;
}
m_CRESULTLIST.AddString( buffer ) ;
}
void CBatchTestDlg::OnViewlog()
{
// TODO: Add your control notification handler code here
char tmpbuf[32] ;
m_CVIEWLOG.GetWindowText( tmpbuf, 32 ) ;
if ( strcmp( tmpbuf, "Hide log" )==0 )
{
m_CRESULTLIST.ShowWindow( SW_HIDE ) ;
m_CVIEWLOG.SetWindowText( "View detailed results" ) ;
m_CETEXT.ShowWindow( SW_HIDE ) ;
this->SetWindowPos( &wndTopMost, 15, 30, 490, 120, SWP_NOREPOSITION ) ;
}
else
{
m_CRESULTLIST.ShowWindow( SW_NORMAL ) ;
m_CVIEWLOG.SetWindowText( "Hide log" ) ;
m_CETEXT.ShowWindow( SW_NORMAL ) ;
this->SetWindowPos( &wndTopMost, 15, 30, 490, 420, SWP_NOREPOSITION ) ;
}
}
void CBatchTestDlg::OnEditchangeComboreaders()
{
// TODO: Add your control notification handler code here
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -