JNA and Winscard: Reading attributes (SCardGetAttrib) without card

房东的猫 提交于 2019-12-24 14:22:08

问题


I have problems to read out reader attributes (like SCARD_ATTR_VENDOR_IFD_SERIAL_NO), without a card present on the reader though setting the parameter dwShareMode of the function SCardConnect to SCARD_SHARE_DIRECT. What is going wrong?

This is the Code:

    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();
          }
        }
      }
    }

These are the constants that I'm using:

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;

This is the Interface:

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);
}

来源:https://stackoverflow.com/questions/18357557/jna-and-winscard-reading-attributes-scardgetattrib-without-card

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!