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