📄 mtestdlg.cpp
字号:
// mtestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "mtest.h"
#include "mtestDlg.h"
#include "winscard.h"
#include "des.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
const int RV_OK = 0;
const int RV_FAIL = 1;
unsigned ICCStatus(LPCBYTE cbRecvBuffer,DWORD cbRecvLength)
{
return MAKEWORD(cbRecvBuffer[cbRecvLength-2],cbRecvBuffer[cbRecvLength-1]);
}
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CMtestDlg dialog
CMtestDlg::CMtestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMtestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMtestDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMtestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMtestDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMtestDlg, CDialog)
//{{AFX_MSG_MAP(CMtestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1)
ON_BN_CLICKED(IDC_OK, OnClickedOk)
ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMtestDlg message handlers
BOOL CMtestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
CHAR i;
for(i=0;i<3;i++)
hCardHandle[i]=0;
CWnd* pWnd2=GetDlgItem(IDC_EDIT2);//get the pointer of the control bar IDC_EDIT2
pWnd2->SetWindowText("00 84 00 00 08");
lReturn = SCardEstablishContext(SCARD_SCOPE_SYSTEM, //initiation
NULL,
NULL,
&hSC);
if ( SCARD_S_SUCCESS != lReturn )
MessageBox("Established Context Failed"); //return the error message
else
{
LPTSTR pmszReaders;
pmszReaders = NULL;
cch = SCARD_AUTOALLOCATE;
CWnd* pList1=GetDlgItem(IDC_LIST1);
lReturn = SCardListReaders(hSC,
NULL,
(LPTSTR)&pmszReaders,
&cch );
if ( SCARD_S_SUCCESS != lReturn ) //list reader initation
{
MessageBox("list reader failed");
}
else
{
while(lstrlen(pmszReaders)>0)
{
pList1->SendMessage(LB_ADDSTRING,0,(LPARAM)pmszReaders);
pmszReaders+=lstrlen(pmszReaders)+1;
}
}
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CMtestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX) //#define IDM_ABOUTBOX 0x0010
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
void CMtestDlg::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 CMtestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMtestDlg::OnButton1()
{
lReturn = SCardEstablishContext(SCARD_SCOPE_SYSTEM,
NULL,
NULL,
&hSC);
if ( SCARD_S_SUCCESS != lReturn )
MessageBox("Established Context Failed");
else
{
MessageBox("Established Context OK");
}
}
void CMtestDlg::OnButton2() //test connection function
{
char buf[300];
CWnd* pButton4=GetDlgItem(IDC_BUTTON4);
CWnd* pButton5=GetDlgItem(IDC_BUTTON5);
CWnd* pEdit2=GetDlgItem(IDC_EDIT2);
//GetOverlappedResult()
hContext=hSC;
lReturn = SCardConnect(hContext,
Readers,
SCARD_SHARE_EXCLUSIVE ,
SCARD_PROTOCOL_T0|SCARD_PROTOCOL_T1,
&hCardHandle[index],
&dwAP[index] );
if ( SCARD_S_SUCCESS != lReturn )
{
pButton4->EnableWindow(FALSE); //make the button change into gray,not display
pButton5->EnableWindow(FALSE);
sprintf(buf,"Connect Failed,ErrorCode:%X",lReturn);
MessageBox(buf);
}
else
{
pButton4->EnableWindow(TRUE);
pButton5->EnableWindow(TRUE);
pEdit2->SetFocus(); //SetFocus ? display
}
}
int SendCommand(
SCARDHANDLE hToken,
DWORD dwAP,
const BYTE cls,
const BYTE ins,
const BYTE p1,
const BYTE p2,
const BYTE lc,
const BYTE *pindata,
const BYTE le,
BYTE *prpdu,
BYTE *prpdulen
)
{
char buf[4096];
int j,i=0;
BYTE comm[1000],resp[1000];
DWORD len=0;
DWORD dwLenr=4000;
LONG lReturn;
DWORD dwReturn;
if(!hToken)
return RV_FAIL;
if(le>0)
{
sprintf(buf,"COMM %02X %02X %02X %02X %02X %02x ",cls,ins,p1,p2,lc,le);
for(int ii=0;ii<lc;ii++)
sprintf(buf+23+ii*3," %02x",pindata[ii]);
}else
{
sprintf(buf,"COMM %02X %02X %02X %02X %02X ",cls,ins,p1,p2,lc);
for(int ii=0;ii<lc;ii++)
sprintf(buf+20+ii*3," %02x",pindata[ii]);
}
comm[i] = cls;
i++;
comm[i] = ins;
i++;
comm[i] = p1;
i++;
comm[i] = p2;
i++;
if (lc != 0x00)
{
comm[i] = lc;
i++;
for(j=0;j<lc;j++)
{
comm[i] = pindata[j];
i++;
}
}
if (le != 0x00)
{
comm[i] = le;
i++;
}
if(lc == 0x00 && le == 0x00)
{
comm[i] = 0x00;
i++;
}
len = i;
lReturn = SCardTransmit(hToken,
(dwAP==SCARD_PROTOCOL_T0?SCARD_PCI_T0:SCARD_PCI_T1),
comm,
len,
NULL,
resp,
&dwLenr);
if ( SCARD_S_SUCCESS != lReturn )
{
return RV_FAIL;
}
if(resp[dwLenr-2]==0x90 && resp[dwLenr-1]==0x00)
{
if(prpdulen)
*prpdulen=dwLenr-2;
if(dwLenr>2 && prpdu)
memcpy(prpdu,resp,dwLenr-2);
for(int i=0;i<dwLenr-2;i++)
{
sprintf(buf+i*3," %02x",resp[i]);
}
return RV_OK;
}
else if(resp[dwLenr-2]==0x61 && dwLenr==2)
{
memcpy(comm,"\x00\xC0\x00\x00\x08",5);
comm[4]=resp[dwLenr-1];
len=5;
dwLenr=4000;
lReturn = SCardTransmit(hToken,
dwAP==SCARD_PROTOCOL_T0?SCARD_PCI_T0:SCARD_PCI_T1,
comm,
len,
NULL,
resp,
&dwLenr);
if ( SCARD_S_SUCCESS != lReturn )
{
return RV_FAIL;
}
if(resp[dwLenr-2]==0x90 && resp[dwLenr-1]==0x00)
{
if(prpdulen)
*prpdulen=dwLenr-2;
if(dwLenr>2 && prpdu)
memcpy(prpdu,resp,dwLenr-2);
for(int i=0;i<dwLenr-2;i++)
{
sprintf(buf+i*3," %02x",resp[i]);
}
return RV_OK;
}
else
{
if(prpdulen)
*prpdulen=0;
dwReturn=resp[dwLenr-2]*256+resp[dwLenr-1];
sprintf(buf,"dwReturn=%04x\n",dwReturn);
return dwReturn;
}
}
else
{
if(prpdulen)
*prpdulen=0;
dwReturn=resp[dwLenr-2]*256+resp[dwLenr-1];
sprintf(buf,"dwReturn=%04x\n",dwReturn);
return dwReturn;
}
return RV_OK;
}
void CMtestDlg::OnButton4()
{
char buf[200];
unsigned char ii=0;
cByte=32;
cch=0;
dwState=0;
lReturn = SCardStatus(hCardHandle[index],
NULL,
&cch,
&dwState,
&dwProtocol,
bAttr,
&cByte); //get the status of a smart card in a reader
if ( SCARD_S_SUCCESS != lReturn )
{
char data[50];
data[0]=0;
strcpy(data,"status:");
sprintf(buf,"%08X \n", dwState);
strcat(data,buf);
MessageBox(data,"",MB_OK);
}
else
{
char data[50];
data[0]=0;
strcpy(data,"status:");
sprintf(buf,"%08X \n", dwState); //definition in the header file
strcat(data,buf);
strcat(data,"ATR:");
for( ii=0;ii<cByte;ii++)
{
sprintf(buf,"%02X ", bAttr[ii]);
strcat(data,buf);
}
MessageBox(data,"",MB_OK);
}
}
void CMtestDlg::OnButton5() //send ICC command,import reference valueable
{
CHAR buf[1000];
BYTE apdu[7]={0x00,0xa4,0x00,0x00,0x02,0x3f,0x00}; //select Master File at any direction
BYTE inBuf[1000],outBuf[1000];
DWORD i,len,dwstatusLength=4000,inBufLen;
CWnd* pWnd1=GetDlgItem(IDC_EDIT1); //the answer result ,big edit1
CWnd* pWnd2=GetDlgItem(IDC_EDIT2); //ICC command under command line
pWnd2->GetWindowText(buf,1000); //search command or not command in IDC_EDIT2 ?
len=pWnd2->GetWindowTextLength(); //how to use GetWindowText() function
if(len==0)
MessageBox("Input ICC Command ,for example 00 84 00 00 08,Please");
for(i=0;i<1000;i++) //buf get data from *pWnd2,
{ //in order to output buf
sscanf(buf+3*i,"%x",inBuf+i); //research the send data structure,x -hex
if ( i*3>=len) //Read formatted data from a string.
break; //two formats
}
inBufLen=i;
/*
((PSCARD_IO_REQUEST)inBuf)->dwProtocol=SCARD_PROTOCOL_T1;
((PSCARD_IO_REQUEST)inBuf)->cbPciLength=sizeof(SCARD_IO_REQUEST);
memcpy(inBuf+sizeof(SCARD_IO_REQUEST),"\x00\x84\x00\x00\x08",5);
inBufLen=5+sizeof(SCARD_IO_REQUEST);
outBufLen=100;
lReturn = SCardControl(hCardHandle,
(DWORD)IOCTL_SMARTCARD_TRANSMIT,
inBuf,
inBufLen,
outBuf,
outBufLen,
&dwstatusLength );
*/
lReturn = SCardTransmit(hCardHandle[index],
(dwAP[index]==SCARD_PROTOCOL_T0?SCARD_PCI_T0:SCARD_PCI_T1),
inBuf,
inBufLen,
NULL,
outBuf,
&dwstatusLength ); //send a service request to a smart card,communication
if ( SCARD_S_SUCCESS != lReturn )
{
sprintf(buf,"The Command Execute error:%08X",lReturn);
MessageBox(buf);
}
else
{
for(i=0;i<dwstatusLength;i++)
sprintf(buf+3*i,"%02X \0" ,(BYTE)outBuf[i]);
pWnd1->SetWindowText(buf); //display in dialog
}
}
void CMtestDlg::OnChangeEdit1() //why is bank
{
}
void CMtestDlg::OnSelchangeList1()
{
CWnd* pList1=GetDlgItem(IDC_LIST1);
CWnd* pButton2=GetDlgItem(IDC_BUTTON2);
CWnd* pButton4=GetDlgItem(IDC_BUTTON4);
CWnd* pButton5=GetDlgItem(IDC_BUTTON5);
index=pList1->SendMessage(LB_GETCURSEL,0,0);
//Send an LB_GETCURSEL message to retrieve the index of
//the currently selected item, if any, in a single-selection list box.
pList1->SendMessage(LB_GETTEXT,index,(LPARAM)Readers);
//An application sends an LB_GETTEXT message to retrieve a string from a list box.
//(LPARAM)Readers is the return item ,buffer for items (LPTSTR)
pButton2->EnableWindow(TRUE);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -