110,538
社区成员
发帖
与我相关
我的任务
分享
//上下文句柄
int pcscContext = 0;
//建立API
Int32 ret = PCSCCSharp.SCardEstablishContext(0, 0, 0,out pcscContext);
//列出读卡机
List<string> list = new List<string>();
list = PCSCCSharp.SCardListReaders(pcscContext);
//连接
int phCard = 0;
int pdwActiveProtocol = 0;
ret = PCSCCSharp.SCardConnect(pcscContext, list[0], 2, 1, ref phCard, ref pdwActiveProtocol);
PCSC.SCARD_IO_REQUEST SendPci, RecvPci;
SendPci.dwProtocol = pdwActiveProtocol;
SendPci.cbPciLength = 8;
RecvPci.dwProtocol = pdwActiveProtocol;
RecvPci.cbPciLength = 8;
byte[] SelectRecvBytes = new byte[20];//应回 90 00
int SelectRecvLength = 2000;
//提交指令
// ret = PCSCCSharp.SCardBeginTransaction(phCard);
byte[] byInBuffer;
byte[] byOutBuffer;
//下达Select Profile的ADPU
ret = PCSCCSharp.SCardTransmit(phCard, ref SendPci, SelectAPDU, SelectAPDU.Length, ref RecvPci, ref SelectRecvBytes[0], ref SelectRecvLength);
byInBuffer = new byte[] { 0x3B, 0x7B, 0x11, 0x22, 0x33, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A };
SelectRecvBytes = new byte[20];
ret = PCSCCSharp.SCardTransmit(phCard, ref SendPci, byInBuffer, byInBuffer.Length, ref RecvPci, ref SelectRecvBytes[0], ref SelectRecvLength);
byInBuffer = new byte[] { 0xA0, 0xA4, 0x00, 0x00, 0x02, 0x3F, 0x00 };
SelectRecvBytes = new byte[20];
ret = PCSCCSharp.SCardTransmit(phCard, ref SendPci, byInBuffer, byInBuffer.Length, ref RecvPci, ref SelectRecvBytes[0], ref SelectRecvLength);
byInBuffer = new byte[] { 0xA0, 0xA4, 0x00, 0x00, 0x02, 0x7F, 0x10 };
SelectRecvBytes = new byte[20];
ret = PCSCCSharp.SCardTransmit(phCard, ref SendPci, byInBuffer, byInBuffer.Length, ref RecvPci, ref SelectRecvBytes[0], ref SelectRecvLength);
byInBuffer = new byte[] { 0xA0, 0xA4, 0x00, 0x00, 0x02, 0x6F, 0x3A };
SelectRecvBytes = new byte[20];
ret = PCSCCSharp.SCardTransmit(phCard, ref SendPci, byInBuffer, byInBuffer.Length, ref RecvPci, ref SelectRecvBytes[0], ref SelectRecvLength);
//没有读取出来 A0C000000F <-------------------------
byInBuffer = new byte[] { 0xA0, 0xC0, 0x00, 0x00, 0x0F };
SelectRecvBytes = new byte[20];
ret = PCSCCSharp.SCardTransmit(phCard, ref SendPci, byInBuffer, byInBuffer.Length, ref RecvPci, ref SelectRecvBytes[0], ref SelectRecvLength);
//没有读取出来 <-------------------------
byInBuffer = new byte[] { 0xA0, 0xB2, 0x01, 0x04, 0x1C };
SelectRecvBytes = new byte[20];
ret = PCSCCSharp.SCardTransmit(phCard, ref SendPci, byInBuffer, byInBuffer.Length, ref RecvPci, ref SelectRecvBytes[0], ref SelectRecvLength);
//ret = PCSCCSharp.SCardEndTransaction(pcscContext, 1);
ret = PCSCCSharp.SCardDisconnect(pcscContext, 1);
ret = PCSCCSharp.SCardReleaseContext(pcscContext);
Code Snippet
[DllImport("sms.dll")]
private static extern IntPtr SmsGetPhoneNumber(IntPtr psmsaAddress);
[DllImport("cellcore.dll")]
private static extern IntPtr SimInitialize(IntPtr dwFlags, IntPtr lpfnCallBack, IntPtr dwParam, out IntPtr lphSim);
[DllImport("cellcore.dll")]
private static extern IntPtr SimGetRecordInfo(IntPtr hSim, IntPtr dwAddress, ref SimRecord lpSimRecordInfo);
[DllImport("cellcore.dll")]
private static extern IntPtr SimReadRecord(IntPtr hSim, IntPtr dwAddress, IntPtr dwRecordType, IntPtr dwIndex, byte[] lpData, IntPtr dwBufferSize, ref IntPtr lpdwBytesRead);
[DllImport("cellcore.dll")]
private static extern IntPtr SimDeinitialize(IntPtr hSim );
public static string GetServiceProvider()
{
IntPtr hSim, res;
res = SimInitialize(IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, out hSim);
if (res != IntPtr.Zero)
throw new Exception("Could not initialize SIM");
SimRecord rec = new SimRecord();
rec.cbSize = (IntPtr)Marshal.SizeOf(rec);
res = SimGetRecordInfo(hSim, (IntPtr)SERVICE_PROVIDER, ref rec);
if (res != IntPtr.Zero)
throw new Exception("Could not read the service provider information from the SIM");
byte[] bData = new byte[(int)rec.dwSize + 1];
IntPtr dwBytesRead = IntPtr.Zero;
res = SimReadRecord(hSim, (IntPtr)SERVICE_PROVIDER, rec.dwRecordType, IntPtr.Zero, bData, (IntPtr)bData.Length, ref dwBytesRead);
if (res != IntPtr.Zero)
throw new Exception("Could not read the service provider from the SIM");
byte[] bScrubbed = new byte[(int)dwBytesRead];
int nPos = 0;
// Scrub the non-ascii characters
for (int i = 0; i < (int)dwBytesRead; i ++)
{
if (((int)bData[i] > 19) && ((int)bData[i] < 125))
{
bScrubbed[nPos] = bData[i];
nPos++;
}
}
SimDeinitialize(hSim);
return Encoding.ASCII.GetString(bScrubbed, 0, bScrubbed.Length);
}