JNA和Winscard:读取属性(SCardGetAttrib)无卡(JNA and Winsca

2019-10-18 12:36发布

我有问题,读出的属性读者(如SCARD_ATTR_VENDOR_IFD_SERIAL_NO),而不存在于读取器虽然设置功能SCardConnect到SCARD_SHARE_DIRECT的参数dwShareMode卡。 这是怎么回事了?

这是代码:

    NativeLong res;
    IntBuffer b = IntBuffer.allocate(1024);
    NativeLongByReference phContext = new NativeLongByReference();
    LongByReference phCard = new LongByReference();

    res = Winscard.INSTANCE.SCardEstablishContext(SCARD_SCOPE_USER, null, null, phContext);

    if (res.intValue() == SCARD_S_SUCCESS) {
      NativeLong hContext = phContext.getValue();   
      res = Winscard.INSTANCE.SCardConnect(hContext, cardReaderName, SCARD_SHARE_DIRECT, SCARD_PROTOCOL_UNDEFINED, phCard, b);

      if (res.intValue() == SCARD_S_SUCCESS) {
        res = Winscard.INSTANCE.SCardGetAttrib(new NativeLong(phCard.getValue()), SCARD_ATTR_VENDOR_IFD_SERIAL_NO, null, b);

        if (res.intValue() == SCARD_S_SUCCESS) {
          int pbAttrLenInt = b.get();
          int[] intArray = { pbAttrLenInt };
          IntBuffer pcbAttrLen = IntBuffer.wrap(intArray);
          ByteBuffer pbAttr = ByteBuffer.allocate(pbAttrLenInt);

          res = Winscard.INSTANCE.SCardGetAttrib(new NativeLong(phCard.getValue()), SCARD_ATTR_VENDOR_IFD_SERIAL_NO, pbAttr, pcbAttrLen);

          byte[] result = pbAttr.array();
          try {
            String text = new String(result, 0, result.length, "ASCII");
            System.out.println(text);
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
      }
    }

这些是我使用的常量:

final static int SCARD_S_SUCCESS = 0;
final static int SCARD_SCOPE_USER = 0;
final static int SCARD_SHARE_DIRECT = 3;
final static int SCARD_PROTOCOL_UNDEFINED = 0;
final static int SCARD_ATTR_VENDOR_IFD_SERIAL_NO = 65795;

这是接口:

public interface Winscard extends Library  {
  Winscard INSTANCE = (Winscard) Native.loadLibrary("winscard", Winscard.class, W32APIOptions.UNICODE_OPTIONS);

  NativeLong SCardEstablishContext(
    int dwScope, //SCARD_SCOPE_USER = 0
    Pointer pvReserved1, //must be null
    Pointer pvReserved2, //must be null
    NativeLongByReference phContext);

  NativeLong SCardConnect(
    NativeLong hContext,
    String szReader,
    int dwShareMode, //SCARD_SHARE_DIRECT = 3
    int dwPreferredProtocols,
    LongByReference phCard,
    IntBuffer pdwActiveProtocol);     

  NativeLong SCardGetAttrib(
    NativeLong hCard, 
    int dwAttrId, 
    ByteBuffer pbAttr, 
    IntBuffer pcbAttrLen);
}
文章来源: JNA and Winscard: Reading attributes (SCardGetAttrib) without card