-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSCard.cpp
More file actions
107 lines (90 loc) · 2.21 KB
/
SCard.cpp
File metadata and controls
107 lines (90 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "SCard.h"
#include<iostream>
#include<qdebug.h>
using namespace std;
SCard::SCard()
{
lReturn = SCardEstablishContext(SCARD_SCOPE_USER, NULL, NULL, &hSC);
if (lReturn != SCARD_S_SUCCESS)
{
qDebug() << "SCardEstablishContext() error "<<endl;
exit(1);
}
}
SCard::~SCard()
{
lReturn = SCardReleaseContext(hSC);
if (lReturn != SCARD_S_SUCCESS)
printf("Failed SCardReleaseContext\n");
}
SCard* SCard::reader=new SCard();
SCard *SCard::getReader()
{
return reader;
}
void SCard::SetReader()
{
dwLen = sizeof(mszReaders);
lReturn = SCardListReaders(hSC, NULL, (LPTSTR)mszReaders, &dwLen);
pReader = (LPTSTR)mszReaders;
if (lReturn != SCARD_S_SUCCESS)
{
qDebug() << "SCardlistcards() error " << mszReaders << endl;
exit(1);
}
}
int SCard::ConnectReader()
{
lReturn = SCardConnect(
hSC,
pReader,
SCARD_SHARE_SHARED,
SCARD_PROTOCOL_T1 ,
&hCardHandle,
&dwAP);
if (lReturn != SCARD_S_SUCCESS)
{
return 0;
}
return 1;
}
int SCard::DisConnectReader()
{
SCardDisconnect(hCardHandle, SCARD_EJECT_CARD);
return 0;
}
int SCard::SendCommand(BYTE command[], DWORD commandLength, BYTE result[], DWORD *resultLength)
{
int flag=1;
SCARD_IO_REQUEST ScardIoRequest;
ScardIoRequest.dwProtocol = dwAP;
ScardIoRequest.cbPciLength = sizeof(dwAP);
lReturn = SCardTransmit(
hCardHandle,
SCARD_PCI_T1,
command,
commandLength,
NULL,
result,
resultLength);
if (lReturn != SCARD_S_SUCCESS)
{
qDebug() << "SendCommand() error"<<endl;
flag=0;
}
return flag;
}
int SCard::ReadTheCard(BYTE *recvBuffer, DWORD &recvlen)
{
BYTE Comm[] = { 0xFF, 0xCA, 0x01, 0x00,0x00 }; //传入命令
DWORD commLen = sizeof(Comm);
qDebug() << "请放入智能卡"<<endl; //输入提示
while (!ConnectReader());
SendCommand(Comm, commLen, recvBuffer, &recvlen);
qDebug() << "读取成功请取走卡"<<endl;
while (ConnectReader())
{
DisConnectReader();
}
return 0;
}