使用C#调用PC\SC接口返回读卡器数量不对
using System;
using System.Text;
using System.Runtime.InteropServices;
class PCSC
{
public struct SCARD_IO_REQUEST
{
public int dwProtocol;
public int cbPciLength;
}
[DllImport("WinScard.dll")]
public static extern int SCardEstablishContext(uint dwScope, int nNotUsed1, int nNotUsed2, ref uint phContext);
[DllImport("WinScard.dll")]
public static extern int SCardReleaseContext(int phContext);
[DllImport("WinScard.dll")]
public static extern int SCardConnect(int hContext, string cReaderName,
uint dwShareMode, uint dwPrefProtocol, ref int phCard, ref int ActiveProtocol);
[DllImport("WinScard.dll")]
public static extern int SCardDisconnect(int hCard, int Disposition);
[DllImport("WinScard.dll")]
public static extern int SCardListReaderGroups(int hContext, ref string cGroups, ref int nStringSize);
[DllImport("WinScard.dll")]
public static extern int SCardListReaders(uint hContext, string cGroups, ref string cReaderLists, ref int nReaderCount);
[DllImport("WinScard.dll")]
public static extern int SCardFreeMemory(uint hContext, string cResourceToFree);
[DllImport("WinScard.dll")]
public static extern int SCardGetAttrib(int hContext, uint dwAttrId,
ref byte[] bytRecvAttr, ref int nRecLen);
[DllImport("WinScard.dll")]
public static extern int SCardTransmit(int hCard, ref SCARD_IO_REQUEST pioSendPci, byte[] pbSendBuffer, int cbSendLength, ref SCARD_IO_REQUEST pioRecvPci, ref byte[] pbRecvBuffer, ref int pcbRecvLength);
[DllImport("WinScard.dll")]
public static extern int SCardTransmit(int hCard, ref SCARD_IO_REQUEST pioSendPci, byte[] pbSendBuffer, int cbSendLength, ref SCARD_IO_REQUEST pioRecvPci, ref byte pbRecvBuffer, ref int pcbRecvLength);
static void Main(string[] args)
{
int cch = -1;
long lReturn;
uint m_hContext;
m_hContext = 0;
string pmszReaders = string.Empty;
string aReaders = "";
string pReader;
int iCount = 0;
lReturn = SCardEstablishContext(0, 0, 0, ref m_hContext);
if (lReturn < 0)
{
//AfxMessageBox("建立连接读卡器失败");0xcd010000
return;
}
lReturn = SCardListReaders(m_hContext, null, ref pmszReaders, ref cch);
System.Console.WriteLine(pmszReaders);
}
}
//上面是我写的C#代码,我电脑上面有两个PC\SC的读卡器,但pmszReaders就只返回一个读卡器
//但我使用C++去调用是可以返回两个读卡器的,
//求大神知道
[解决办法]
应该是传递参数问题,string 见到/0就认为是完了,不过没找到什么解决办法,用char[]也不行,你有什么别的传递类型试试吧,最后一个参数是list 的长度