int main(int argc, char *argv[]) { int index = 0; //fptrs.gost28147_uz = nullptr; //fptrs.impr_data_8_uz = nullptr; FUNC_PTRS fptrs; void* pctx; fptrs.gost28147_uz = gost28147_uz; fptrs.pzshk_uz = pzshk_uz; fptrs.pzdshk_uz = pzdshk_uz; fptrs.impr_data_8_uz = impr_data_8_uz; //auto error = VDT_CreateContext(&fptrs, VDT_SCOPE_USER, &pctx); //if (error) // qDebug() << "error = " << error; LONG lRetValue; LPTSTR pmszReaders = NULL; LPTSTR pszReader; LPTSTR pszaReaders[16]; DWORD cch = SCARD_AUTOALLOCATE; INT iNumberOfReaders; INT iSelectedReader; // The SCardEstablishContext function establishes the resource manager context while (true) { Sleep(500); printf("-----listReaders-----%d\n", index); ++index; /*uint32 amount; auto readers = VDT_GetReaders(pctx, &amount); for (uint i = 0; i < amount; ++i) { qDebug() << "readers[" << i << "] = " << readers[i]; } qDebug() << "VDT_GetReaders = " << amount;*/ listReaders(); } //VDT_ReleaseContext(pctx); QCoreApplication a(argc, argv); return a.exec(); //return 0; }
static std:: vector<BYTE> sendAPDU(const char *apdu) { SCARDHANDLE hCard; unsigned char *recvBuf; //char debugString[2048]; unsigned char sendBuf[280]; size_t sendBufLen = sizeof(sendBuf); std:: vector<BYTE> result; SCARDCONTEXT ctx; const char *readerName; long ret = SCardEstablishContext(SCARD_SCOPE_USER, NULL, NULL, &ctx); CHECK_PCSC_RET("SCardEstablishContext", ret); if (SCARD_S_SUCCESS == ret) { char readerList[2000]; readerName = listReaders(ctx, readerList, sizeof(readerList), true, -1); DWORD protocol; char *chipnrForCompare = NULL; long ret = SCardConnectA(ctx, readerName, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0, &hCard, &protocol); CHECK_PCSC_RET("SCardConnect", ret); if (SCARD_S_SUCCESS == ret) { DWORD recvBufLen = sizeof(recvBuf); unsigned char recvBuf[258]; // If chipnrForCompare == NULL then we retrieve the chip number // of this card to store all APDUs const char *thisChipNr = NULL; char chipNrBuf[2 * CHIP_NR_LEN + 1]; if (NULL == chipnrForCompare) { thisChipNr = GetChipNrFromCard(hCard, chipNrBuf); if (NULL == thisChipNr) { SCardDisconnect(hCard, SCARD_LEAVE_CARD); //return -1; } } //if (NULL == chipnrForCompare) recvBufLen = (DWORD) sizeof(recvBuf); //0x00000102 if (0 == hex2bin(apdu, sendBuf, &sendBufLen)) { // Check the APDU if (sendBufLen < 4) { printf("ERR: APDU should be at least 4 bytes\n"); DebugMessage("ERR: APDU should be at least 4 bytes\n"); } else if (sendBufLen > 5 && (size_t) (5 + sendBuf[4]) != sendBufLen) { printf("ERR: wrong P3 byte in case 3 APDU\n"); DebugMessage("ERR: wrong P3 byte in case 3 APDU\n"); } else { long ret = SCardTransmit2(hCard, &g_rgSCardT0Pci, sendBuf, (DWORD) sendBufLen, NULL, recvBuf, &recvBufLen); if (SCARD_S_SUCCESS == ret) { for (int i = 0; i < (int) recvBufLen; i++) { result.push_back(recvBuf[i]); } return result; // success } } } } } return result; // failed }
int main(int argc, const char **argv) { Params params; SCARDCONTEXT ctx; int returnValue = 0; if (0 != parseCommandLine(argc, argv, ¶ms)) return -1; long ret = SCardEstablishContext(SCARD_SCOPE_USER, NULL, NULL, &ctx); CHECK_PCSC_RET("SCardEstablishContext", ret); if (SCARD_S_SUCCESS == ret) { char readerList[2000]; #ifdef LOOP_LIST_READERS again: #endif const char *readerName = listReaders(ctx, readerList, sizeof(readerList), 'l' == params.command, params.readerNr); #ifdef LOOP_LIST_READERS if (params.command == 'l') { getchar(); goto again; } #endif if (NULL != readerName) { switch (params.command) { case 's': getStatusChange(ctx, readerList, params.timeout); break; case 'l': // already done in listReaders() break; case 'a': sendAPDUS(ctx, readerName, params.apduCount, params.apdus); break; case 'f': testCardFunctionality(ctx, readerName); break; case 't': testAPDUS(ctx, readerName, params.chipnr); break; case 'x': switch (params.test) { case 1: returnValue = testStatus(ctx, readerName, readerList); break; case 2: returnValue = testConnect(ctx, readerName); break; case 3: returnValue = testTransaction(ctx, readerName); break; case 4: returnValue = testTransmit(ctx, readerName, params.apdus); break; default: printf("Unknown -x parameter '%d', exiting\n", params.test); } break; default: printf("Unknown command '%c', exiting\n", params.command); } } } ret = SCardReleaseContext(ctx); return returnValue; }