#include "afx.h"
#include "cpcsc.h"
#include <iostream>
//#include<Windows.h>
#define _CRTDBG_MAP_ALLOC
#include<stdlib.h>
#include<crtdbg.h>
using namespace std;
CPCSC::CPCSC()
{
m_hContext = 0;
m_hCard =0;
m_cardName="";
}
CPCSC::~CPCSC()
{
DisconnectCard();
}
bool CPCSC::ConnectCard(char *sReaderName)
{
DWORD dwAP;
LONG res;
res = ::SCardEstablishContext(SCARD_SCOPE_USER,NULL,NULL,&m_hContext);
if(res != SCARD_S_SUCCESS)
{
m_hContext =0;
return false;
}
char * mszReaders=NULL;
LPTSTR pReader =NULL;
DWORD cch= sizeof(mszReaders);
bool bConnected = false;
if(sReaderName == NULL )
{
res = ::SCardListReaders(m_hContext,NULL,(LPTSTR)mszReaders,&cch);
if(res ==SCARD_S_SUCCESS)
{
pReader =(LPTSTR)mszReaders;
if('\0' == *pReader) return false;
while( '\0' != *pReader)
{
m_cardName =pReader;
printf("Reader:%s\n",pReader);
res = ::SCardConnect(m_hContext,pReader,SCARD_SHARE_SHARED,
SCARD_PROTOCOL_T0|SCARD_PROTOCOL_T1|SCARD_PROTOCOL_RAW,
&m_hCard,&dwAP);
if(res ==SCARD_S_SUCCESS)
{
cout<<"%s"<<pReader<<endl;
cout<<":is successed connected\n"<<endl;
bConnected =true;
break;
}
else pReader =pReader +strlen((const char *)pReader) +1;
}
}
else cout<<"error list reader"<<endl;
bConnected =false;
::SCardFreeMemory(m_hContext,(LPCVOID)mszReaders);
}
// _CrtSetBreakAlloc(149);
else
{
m_cardName =sReaderName;
res = ::SCardConnect(m_hContext,(LPTSTR)sReaderName,SCARD_SHARE_SHARED,
SCARD_PROTOCOL_T0|SCARD_PROTOCOL_T1|SCARD_PROTOCOL_RAW,
&m_hCard,&dwAP);
if(res ==SCARD_S_SUCCESS)
bConnected =true;
else bConnected =false;
::SCardFreeMemory(m_hContext,(LPCVOID)mszReaders);
}
//cout<<"ok here"<<endl;
if(!bConnected)
{
::SCardReleaseContext(m_hContext);
m_hContext = 0;
m_hCard =0;
}
if(dwAP ==SCARD_PROTOCOL_T0)
m_isT0= TRUE;
else if(dwAP ==SCARD_PROTOCOL_T1)
m_isT0= FALSE;
free(pReader);
return bConnected;
}
void CPCSC::DisconnectCard()
{
if(m_hCard)
{
::SCardDisconnect(m_hCard,SCARD_UNPOWER_CARD);
m_hCard =0;
}
if(m_hContext)
{
::SCardReleaseContext(m_hContext);
m_hContext;
}
}
void CPCSC::GetPCSCInfo(CString &strATR,BOOL& isT0)
{
isT0=m_isT0;
SCARD_READERSTATE Reader[1];
Reader[0].dwCurrentState =SCARD_STATE_UNAWARE;
Reader[0].dwEventState =SCARD_STATE_UNAWARE;
Reader[0].szReader =m_cardName;
long res = SCardGetStatusChange(m_hContext,300,Reader,1);
if(res ==SCARD_S_SUCCESS)
{
strATR ="復位資訊:";
CString strTemp;
for(DWORD i=0;i<Reader->cbAtr;i++)
{
strTemp.Format(_T("%02X"),Reader->rgbAtr[i]);
strATR += strTemp;
}
}
else strATR +="復位資訊:Error!";
}
bool CPCSC::SendRevAPDU(IN unsigned char * ucCmd, IN DWORD nCmdLen,
OUT unsigned char * ucRes, IN OUT DWORD & nResLen)
{
BYTE sw1,sw2;
//BYTE select_mf[]={0x00,0xA4,0x00,0x02,0x3F,0x00};
LONG res;
res = ::SCardTransmit(m_hCard, SCARD_PCI_T0,ucCmd,nCmdLen,NULL,ucRes,&nResLen);
if (res!=SCARD_S_SUCCESS)
{
printf("Faile sCardtransmit");
return false;
}
else
{
sw1=ucRes[nResLen-2];
sw2=ucRes[nResLen-1];
}
if ( sw1==0x61 )
{
if(true==CPCSC::SendRevAPDU((unsigned char*)("00C00000"+sw2),5,ucRes,nResLen))
{
sw1=ucRes[nResLen-2];
sw2=ucRes[nResLen-1];
}
}
return true;
}
int main()
{
CPCSC apdu;
//CFile f1;
//cout<<"class is ok"<<endl;
if(false==apdu.ConnectCard("usb1"))
{
cout<<"connect is not ok"<<endl;
exit(1);
}
cout<<"connect is ok"<<endl;
char *data;
data="https://bbs.csdn.net/topics/00A40000023F00";
int datalen;
datalen=sizeof(*data);
CString *recv=NULL;
DWORD recvlen;
/*if( !f1.Open( _T("D:\\Users\\PC\\Desktop\\1M.txt"), CFile::modeRead ))
{
cout<<"file is not exeast\n"<<endl;
exit(1);
}
if(false==f1.Read(data,(unsigned int)f1.GetLength()))
{
cout<<"file cannot read \n"<<endl;
exit(1);
}
f1.Close();
*/
apdu.SendRevAPDU((unsigned char*)data,datalen,(unsigned char*)recv,recvlen);
cout<<"the result is : "<<*recv << endl;
//while(1);
apdu.DisconnectCard();
apdu.~CPCSC();
return 0;
}
/*
頭檔案
/******************************************************************************************************************************
*******************************************************************************************************************************
*/
#ifndef __CPCSC_HEADER__
#define __CPCSC_HEADER__
#include <winscard.h>
#pragma comment(lib,"winscard")
class CPCSC
{
public:
CPCSC();
virtual ~CPCSC();
bool ConnectCard(char * sReaderName=NULL);
void DisconnectCard();
public:
bool SendRevAPDU(IN unsigned char * ucCmd, IN DWORD nCmdLen,
OUT unsigned char * ucRes, IN OUT DWORD& nResLen);
void GetPCSCInfo(CString &strATR,BOOL& isT0);
protected:
SCARDCONTEXT m_hContext; //卡設備背景關系
SCARDHANDLE m_hCard; //讀卡器句柄
CString m_cardName; //讀卡器名稱
BOOL m_isT0;
//char *pickOut;
};
#endif
//--------------------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------------
/*錯誤資訊*/
0x758B812F (KernelBase.dll) (Main.exe 中)處的第一機會例外: 0x000006BA: RPC 服務器不可用。。
First-chance exception at 0x758B812F in Main.exe: Microsoft C++ exception: unsigned long at memory location 0x001DFD44.
First-chance exception at 0x758B812F in Main.exe: Microsoft C++ exception: unsigned long at memory location 0x001DFDDC.
0x758B812F (KernelBase.dll) (Main.exe 中)處的第一機
[code=c][/code]會例外: 0x0000071A: The remote procedure call was canceled, or if a call time-out was specified, the call timed out。
執行緒 0x60c 已退出,回傳值為 1 (0x1)。
執行緒 0x1350 已退出,回傳值為 1 (0x1)。
Detected memory leaks!
Dumping objects ->
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(156) : {149} normal block at 0x0022DA20, 124 bytes long.
Data: <D Q5 5 > 44 F9 CA 51 35 00 00 00 35 00 00 00 01 00 00 00
Object dump complete.
程式“[4292] Main.exe”已退出,回傳值為 1 (0x1)。
uj5u.com熱心網友回復:
先再可能出問題的函式里插入斷點,然后,一步步來,打到導致程式崩潰的代碼也可以先設兩個斷點,相當于設定一個范圍,按f5除錯,如果兩個斷點之間代碼運行沒問題,再更改設定范圍,可以先大范圍,再逐漸縮小
uj5u.com熱心網友回復:
首先謝謝你的解答,我每次設定斷點都不管用,就知道在呼叫main函式里的if(false==apdu.ConnectCard("usb1")){
cout<<"connect is not ok"<<endl;
exit(1);
一步時候出錯,當給if(false==apdu.ConnectCard("usb1")),設定斷點時候就直接跳過去,根本不管用
uj5u.com熱心網友回復:
你在if上面設定一個斷點, 先按f5到斷點處,再按f10一步步除錯,看看程式有沒有執行if里的代碼如果當黃色箭頭(除錯執行的當前位置)在if(false==apdu.ConnectCard("usb1"))時,按f10程式崩潰的話,那一定是ConnectCard函式里出的問題
如果有必要,對每個函式呼叫采用例外處理機制,這樣等程式崩潰時,就知道哪出的問題
try catch
uj5u.com熱心網友回復:

得 花點時間了 !
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/95800.html
標籤:網絡及通訊開發
上一篇:BCB xe7 stringgrid 匯入匯出excel
下一篇:timeSetEvent出現問題
