|
@@ -8,6 +8,7 @@
|
8
|
8
|
#include "ScReader.h"
|
9
|
9
|
|
10
|
10
|
thread_local LPSCARDCONTEXT ScReader::_context = nullptr;
|
|
11
|
+thread_local LONG ScReader::_lastResult = SCARD_S_SUCCESS;
|
11
|
12
|
|
12
|
13
|
ScReader::~ScReader()
|
13
|
14
|
{
|
|
@@ -19,18 +20,18 @@ bool ScReader::establishContext()
|
19
|
20
|
if (!freeContext())
|
20
|
21
|
return false;
|
21
|
22
|
_context = new SCARDCONTEXT[1];
|
22
|
|
- auto res = SCardEstablishContext(SCARD_SCOPE_SYSTEM, nullptr, nullptr, _context);
|
23
|
|
- return res == SCARD_S_SUCCESS;
|
|
23
|
+ _lastResult = SCardEstablishContext(SCARD_SCOPE_SYSTEM, nullptr, nullptr, _context);
|
|
24
|
+ return _lastResult == SCARD_S_SUCCESS;
|
24
|
25
|
}
|
25
|
26
|
|
26
|
27
|
bool ScReader::freeContext()
|
27
|
28
|
{
|
28
|
29
|
if (!_context)
|
29
|
30
|
return true;
|
30
|
|
- auto res = SCardReleaseContext(*_context);
|
|
31
|
+ _lastResult = SCardReleaseContext(*_context);
|
31
|
32
|
delete[] _context;
|
32
|
33
|
_context = nullptr;
|
33
|
|
- return res == SCARD_S_SUCCESS;
|
|
34
|
+ return _lastResult == SCARD_S_SUCCESS;
|
34
|
35
|
}
|
35
|
36
|
|
36
|
37
|
bool ScReader::connect(DWORD shareMode, DWORD protocols, DWORD disposition)
|
|
@@ -39,32 +40,32 @@ bool ScReader::connect(DWORD shareMode, DWORD protocols, DWORD disposition)
|
39
|
40
|
return false;
|
40
|
41
|
_card = new SCARDCONTEXT[1];
|
41
|
42
|
_proto = new DWORD[1];
|
42
|
|
- auto res = SCardConnect(*_context, _name.c_str(), shareMode, protocols, _card, _proto);
|
|
43
|
+ _lastResult = SCardConnect(*_context, _name.c_str(), shareMode, protocols, _card, _proto);
|
43
|
44
|
if (*_proto == SCARD_PROTOCOL_T0)
|
44
|
45
|
_sendPci = *SCARD_PCI_T0;
|
45
|
46
|
if (*_proto == SCARD_PROTOCOL_T1)
|
46
|
47
|
_sendPci = *SCARD_PCI_T1;
|
47
|
|
- return res == SCARD_S_SUCCESS;
|
|
48
|
+ return _lastResult == SCARD_S_SUCCESS;
|
48
|
49
|
}
|
49
|
50
|
|
50
|
51
|
bool ScReader::disconnect(DWORD disposition)
|
51
|
52
|
{
|
52
|
53
|
if (!_card)
|
53
|
54
|
return true;
|
54
|
|
- auto res = SCardDisconnect(*_card, disposition);
|
|
55
|
+ _lastResult = SCardDisconnect(*_card, disposition);
|
55
|
56
|
delete[] _proto;
|
56
|
57
|
_proto = nullptr;
|
57
|
58
|
delete[] _card;
|
58
|
59
|
_card = nullptr;
|
59
|
|
- return res == SCARD_S_SUCCESS;
|
|
60
|
+ return _lastResult == SCARD_S_SUCCESS;
|
60
|
61
|
}
|
61
|
62
|
|
62
|
63
|
std::shared_ptr<ScResult> ScReader::transmit(const ScCommand& command, DWORD size)
|
63
|
64
|
{
|
64
|
65
|
auto data = command.getData();
|
65
|
66
|
auto resBuffer = new BYTE[size];
|
66
|
|
- auto res = SCardTransmit(*_card, &_sendPci, (BYTE*)data.data(), data.size(), nullptr, resBuffer, &size);
|
67
|
|
- if (res == SCARD_S_SUCCESS)
|
|
67
|
+ _lastResult = SCardTransmit(*_card, &_sendPci, (BYTE*)data.data(), data.size(), nullptr, resBuffer, &size);
|
|
68
|
+ if (_lastResult == SCARD_S_SUCCESS)
|
68
|
69
|
return std::make_shared<ScResult>(std::string((char*)resBuffer, size));
|
69
|
70
|
return std::make_shared<ScResult>("");
|
70
|
71
|
}
|
|
@@ -74,8 +75,8 @@ std::vector<std::shared_ptr<ScReader>> ScReader::getReaders()
|
74
|
75
|
std::vector<std::shared_ptr<ScReader>> readers;
|
75
|
76
|
DWORD readerCount = SCARD_AUTOALLOCATE;
|
76
|
77
|
LPTSTR readersNames = nullptr;
|
77
|
|
- auto res = SCardListReaders(*_context, nullptr, (LPTSTR)&readersNames, &readerCount);
|
78
|
|
- if (res == SCARD_S_SUCCESS)
|
|
78
|
+ _lastResult = SCardListReaders(*_context, nullptr, (LPTSTR)&readersNames, &readerCount);
|
|
79
|
+ if (_lastResult == SCARD_S_SUCCESS)
|
79
|
80
|
{
|
80
|
81
|
LPTSTR reader = readersNames;
|
81
|
82
|
while (*reader)
|
|
@@ -87,3 +88,8 @@ std::vector<std::shared_ptr<ScReader>> ScReader::getReaders()
|
87
|
88
|
SCardFreeMemory(*_context, readersNames);
|
88
|
89
|
return readers;
|
89
|
90
|
}
|
|
91
|
+
|
|
92
|
+LONG ScReader::getLastResult()
|
|
93
|
+{
|
|
94
|
+ return _lastResult;
|
|
95
|
+}
|