Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JNA和Winscard:不带卡读取属性(ScardGetTrib)_Java_Dll_Jna_Winscard - Fatal编程技术网

Java JNA和Winscard:不带卡读取属性(ScardGetTrib)

Java JNA和Winscard:不带卡读取属性(ScardGetTrib),java,dll,jna,winscard,Java,Dll,Jna,Winscard,虽然将函数SCardConnect的参数dwShareMode设置为SCARD_SHARE_DIRECT,但在读卡器上没有卡的情况下,我无法读取读卡器属性(如SCARD_ATTR_VENDOR_IFD_SERIAL_NO)。出什么事了 代码如下: NativeLong res; IntBuffer b = IntBuffer.allocate(1024); NativeLongByReference phContext = new NativeLongByReferenc

虽然将函数SCardConnect的参数dwShareMode设置为SCARD_SHARE_DIRECT,但在读卡器上没有卡的情况下,我无法读取读卡器属性(如SCARD_ATTR_VENDOR_IFD_SERIAL_NO)。出什么事了

代码如下:

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

LongByReference
可能不正确。您通常不会看到API询问64位元素的地址
SCardConnectW
是不正确的,因为它应该使用
WString
而不是
String
(尽管您应该跳过
-W
-A
后缀,并对方法名称和类型映射使用与JNA在初始化库时对w32 API映射相同的方法)。我发现了错误:SCardConnectA(dwPreferredProtocols)的第四个参数只需要是SCARD_PROTOCOL_UNDEFINED。没有后缀的SCardConnect不起作用。好的,如果不包括适当的函数名映射器,例如
com.sun.jna.win32.w32apipoptions
中的映射器,它就不起作用了。好的,完成了,感谢您的提示。我会修改上面的代码,希望到时候可以。