📄 gsmcardtestdlg.cpp
字号:
// GSMCardTestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "winscard.h"
#include "GSMCardTest.h"
#include "GSMCardTestDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
SCARD_READERSTATE ReaderState[NUMBER_OF_READERS];
SCARD_IO_REQUEST IO_Request;
SCARDCONTEXT ContextHandle;
SCARDHANDLE CardHandle;
const char *mainDir = "a0a40000027f10";
const char *telephoneDir = "a0a40000026f3a";
const char *readTelePhoneRecord[] = {
"a0b201041c",
"a0b202041c",
"a0b203041c",
"a0b204041c",
"a0b205041c"
};
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CGSMCardTestDlg dialog
CGSMCardTestDlg::CGSMCardTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CGSMCardTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CGSMCardTestDlg)
m_test1 = _T("");
m_test2 = _T("");
m_test4 = _T("");
m_test3 = _T("");
m_test5 = _T("");
act_Name = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
CGSMCardTestDlg::~CGSMCardTestDlg()
{
SCardDisconnect(CardHandle, SCARD_EJECT_CARD);
}
void CGSMCardTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CGSMCardTestDlg)
DDX_Control(pDX, IDC_EDIT, m_edit);
DDX_Text(pDX, IDC_EDIT1, m_test1);
DDX_Text(pDX, IDC_EDIT2, m_test2);
DDX_Text(pDX, IDC_EDIT4, m_test4);
DDX_Text(pDX, IDC_EDIT3, m_test3);
DDX_Text(pDX, IDC_EDIT5, m_test5);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CGSMCardTestDlg, CDialog)
//{{AFX_MSG_MAP(CGSMCardTestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDOK, OnWrite)
ON_BN_CLICKED(IDCANCEL, OnRead)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGSMCardTestDlg message handlers
BOOL CGSMCardTestDlg::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
// TODO: Add extra initialization here
//
// Open a context which communication to the Resource Manager
//
unsigned int StringLen = 0;
memset(ReaderName[0], 0, NAME_LENGTH);
memset(ReaderName[1], 0, NAME_LENGTH);
memset(ReaderName[2], 0, NAME_LENGTH);
memset(ReaderName[3], 0, NAME_LENGTH);
pOutBuffer = (PBYTE) malloc(MAX_OUTPUT);
ASSERT( pOutBuffer );
// OutBufferLine = 0;
pResponseBuffer = (PBYTE) malloc( MAX_RESPONSE );
ASSERT( pResponseBuffer );
memset(pResponseBuffer, 0x00, MAX_RESPONSE);
ret = SCardEstablishContext(SCARD_SCOPE_USER, NULL, NULL, &ContextHandle);
if (ret != SCARD_S_SUCCESS) {
MessageBuffer.Format("Function SCardEstablishContext returned 0x%X error code.", ret);
MessageBox((LPCTSTR) MessageBuffer, "SCSample", MB_OK| MB_ICONSTOP);
EndDialog(2);
return(IDCANCEL);
}
ReaderCount = 0;
ResponseLength = MAX_RESPONSE;
ret = SCardListReaders(ContextHandle, 0, (char *) pResponseBuffer, &ResponseLength);
if (ret != SCARD_S_SUCCESS) {
MessageBuffer.Format("Function SCardListReaders returned 0x%X error code.", ret);
MessageBox((LPCTSTR) MessageBuffer, "GSMCardTest", MB_OK| MB_ICONSTOP);
EndDialog(2);
return(IDCANCEL);
}
else {
while ( ResponseLength > StringLen+1) {
strcpy(ReaderName[ReaderCount], (LPCTSTR) pResponseBuffer+StringLen);
DWORD ActiveProtocol = 0;
ret = SCardConnect(ContextHandle, ReaderName[ReaderCount], SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, &CardHandle, &ActiveProtocol);
// if (ret != SCARD_E_UNKNOWN_READER)
ReaderCount++;
if (ret == SCARD_S_SUCCESS)
SCardDisconnect(CardHandle, SCARD_EJECT_CARD);
StringLen += strlen((LPCTSTR) pResponseBuffer+StringLen+1);
StringLen += 2;
}
}
if (ReaderCount == 0) {
MessageBox("No driver is available for use with the resource manager!", "SCSample", MB_ICONSTOP);
EndDialog(2);
return (IDCANCEL);
}
DWORD ActiveProtocol = 0;
ProtocolType = SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1;
do
{
ret = SCardConnect(ContextHandle, ReaderName[act_Name], SCARD_SHARE_EXCLUSIVE, ProtocolType, &CardHandle, &ActiveProtocol);
if (ret != SCARD_S_SUCCESS){
MessageBuffer.Format("error: 0x%X \n,please input the sim card!\n", ret);
MessageBox((LPCTSTR) MessageBuffer, "GSMCardTest", MB_OK| MB_ICONSTOP);
Sleep(1000);
}
} while ((ret != SCARD_S_SUCCESS));
ProtocolType = ActiveProtocol;
switch (ProtocolType) {
case SCARD_PROTOCOL_T0:
SetDlgItemText(IDC_EDIT, "Function SCardConnect ok\nProtocoltype = T0");
break;
case SCARD_PROTOCOL_T1:
SetDlgItemText(IDC_EDIT, "Function SCardConnect ok\nProtocoltype = T1");
break;
default:
sprintf((char *) pOutBuffer, "Function SCardConnect ok\n%.8x", ActiveProtocol);
SetDlgItemText(IDC_EDIT, (char *) pOutBuffer);
break;
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CGSMCardTestDlg::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 CGSMCardTestDlg::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 CGSMCardTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
short CGSMCardTestDlg::AToHex(char *mhstr, BYTE *buf)
{
InBufferLength = strlen(mhstr);
// if (InBufferLength < 3) {
// MessageBox("Input not complete!", "SCSample", MB_ICONSTOP);
// return(0);
// }
for (short i=0; i<InBufferLength; i++) {
if (mhstr[i] == '\n') {
mhstr[i] = 0;
}
else if (((mhstr[i] < '0') || (mhstr[i] > '9')) &&
((mhstr[i] != ' ') && (mhstr[i] != ',')) &&
((mhstr[i] < 'a') || (mhstr[i] > 'f')) &&
((mhstr[i] < 'A') || (mhstr[i] > 'F'))) {
MessageBox("Only hex. input allowed!", "SCSample", MB_ICONSTOP);
GetDlgItem(IDC_EDIT)->SetFocus();
return(0);
}
}
char *s;
if (mhstr[1] == ',') {
InBufferLength = (InBufferLength - 4)/2;
s = mhstr+4;
}
else {
InBufferLength = InBufferLength/2;
s = mhstr;
}
memset(buf, 0, sizeof (buf));
i = 0;
while (isspace(*s)) s++;
short k = FALSE;
while (*s) {
if (isxdigit(*s)) {
buf[i] <<= 4;
if (isalpha(*s))
buf[i] |= (tolower(*s) - 0x57);
else
buf[i] |= (*s - 0x30);
if (k) {
i++;
k= FALSE;
}
else {
k= TRUE;
}
s++;
}
else break;
}
return(1);
}
void CGSMCardTestDlg::OnWrite()
{
// TODO: Add your control notification handler code here
char mhstr[MAX_INPUT];
char buf[MAX_INPUT/2];
PBYTE pInBuffer;
memset(mhstr, 0, MAX_INPUT);
CString com[5] = {""};
// UpdateData(FALSE);
UpdateData(TRUE);
com[0] = m_test1;
com[1] = m_test2;
com[2] = m_test3;
com[3] = m_test4;
com[4] = m_test5;
pInBuffer = (PBYTE) malloc( MAX_INPUT );
for(int i=0; i<5; i++)
{
if (com[i] == "")
continue;
sprintf(mhstr , "%s", LPCTSTR(com[i]));
if (!AToHex((char *) &mhstr, (BYTE *) &buf))
{
return;
}
IO_Request.dwProtocol = ProtocolType;
IO_Request.cbPciLength = (DWORD) sizeof(SCARD_IO_REQUEST);
ASSERT( pInBuffer );
memcpy(pInBuffer, buf, InBufferLength);
ResponseLength = MAX_RESPONSE;
ret = SCardTransmit(CardHandle, &IO_Request, pInBuffer, InBufferLength, 0, pResponseBuffer, &ResponseLength);
if (ret != SCARD_S_SUCCESS){
MessageBuffer.Format("error: 0x%X \n", ret);
MessageBox((LPCTSTR) MessageBuffer, "GSMCardTest", MB_OK| MB_ICONSTOP);
free(pInBuffer);
return;
}
PBYTE pStartAddress = pOutBuffer;
for (unsigned long i=0; i<ResponseLength; i++) {
sprintf((char *) pOutBuffer, "%.2x ",(BYTE) pResponseBuffer[i]);
pOutBuffer += 3;
if (i > MAX_RESPONSE-3)
break;
}
pOutBuffer = pStartAddress;
SetDlgItemText(IDC_EDIT, (const char *) pOutBuffer);
}
free(pInBuffer);
// CDialog :: OnOK();
}
void CGSMCardTestDlg::OnRead()
{
// TODO: Add your control notification handler code here
char mhstr[MAX_INPUT];
char buf[MAX_INPUT/2];
PBYTE pInBuffer;
PBYTE pStartAddress;
memset(mhstr, 0, MAX_INPUT);
CString com[5];
UpdateData(TRUE);
pInBuffer = (PBYTE) malloc( MAX_INPUT );
for(int i=0; i<5; i++)
{
sprintf(mhstr , "%s", LPCTSTR(com[i]));
if (!AToHex((char *) &mhstr, (BYTE *) &buf))
{
return;
}
IO_Request.dwProtocol = ProtocolType;
IO_Request.cbPciLength = (DWORD) sizeof(SCARD_IO_REQUEST);
ASSERT( pInBuffer );
memcpy(pInBuffer, buf, InBufferLength);
ResponseLength = MAX_RESPONSE;
ret = SCardTransmit(CardHandle, &IO_Request, pInBuffer, InBufferLength, 0, pResponseBuffer, &ResponseLength);
if (ret != SCARD_S_SUCCESS)
{
MessageBuffer.Format("error: 0x%X \n", ret);
MessageBox((LPCTSTR) MessageBuffer, "GSMCardTest", MB_OK| MB_ICONSTOP);
free(pInBuffer);
return;
}
pStartAddress = pOutBuffer;
// strcpy((char * )com[i],pOutBuffer);
for (unsigned long i=0; i<ResponseLength; i++)
{
sprintf((char *) pOutBuffer, "%.2x ",(BYTE) pResponseBuffer[i]);
pOutBuffer += 3;
if (i > MAX_RESPONSE-3)
break;
}
//
// OutBufferLine = (ResponseLength * 3)/35;
// OutBufferLine++;
// if (OutBufferLine > 5) {
// ((CScrollBar *)GetDlgItem(IDC_SCROLLBAR))->SetScrollRange(0, OutBufferLine, FALSE);
// line = 0;
// ((CScrollBar *)GetDlgItem(IDC_SCROLLBAR))->SetScrollPos(line, TRUE);
// }
}
pOutBuffer = pStartAddress;
SetDlgItemText(IDC_EDIT, (const char *) pOutBuffer);
m_test1 = com[0];
m_test2 = com[1];
m_test3 = com[2];
m_test4 = com[3];
m_test5 = com[4];
UpdateData(FALSE);
free(pInBuffer);
CDialog :: OnOK();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -