代码上:import org.xvolks.jnative.JNative;
import org.xvolks.jnative.Type;
import org.xvolks.jnative.exceptions.NativeException;
import org.xvolks.jnative.pointers.Pointer;
import org.xvolks.jnative.pointers.memory.HeapMemoryBlock;
import org.xvolks.jnative.pointers.memory.MemoryBlockFactory;
import org.xvolks.jnative.misc.basicStructures.LONG;
import org.xvolks.jnative.misc.basicStructures.HWND;
import org.xvolks.jnative.misc.basicStructures.LPARAM;
import org.xvolks.jnative.misc.basicStructures.HANDLE;
import org.xvolks.jnative.util.Callback;public class CustomizeEP {
private static JNative v = null;
/**
 * @&m_hSC
 */
private static Pointer cardContext = null;
/**
 * @&m_hCard
 */
private static Pointer cardHandle = null;
/**
 * @&psReaders
 */
private static Pointer cardReader = null;
/**
 * @&byte[]
 */
private static byte[] addresses = null;
/**
 * Protocol
 */
private static LONG protocol = new LONG((int)getNextLong());
/**
 * The context is a user context, and anydatabase operations
 * are performed within the domain of the user.
 */
private static final int SCARD_SCOPE_USER = 0;
/**
 *  This application is not willing to share this card with other applications.
 */
private static final int SCARD_SHARE_EXCLUSIVE = 1;
/**
 * MAX_READERNAME_LEN
 */
private static final int MAX_READERNAME_LEN = 256;
/**
 * MAX_ATR_LEN
 */
private static final int MAX_ATR_LEN = 32;
/**
 * APDU_PSE
 */
private static final String APDU_PSE = "00A404000E315041592E5359532E4444463031";
/**
 * 非接卡片
 */
private static final String SCARD_CONNECTLESS_READER = "SCM Microsystems Inc. SDI010 Contactless Reader 0";
/**
 * 加载的DLL
 */
private static final String DLL_LIB = "winscard.dll"; /**
 * 列出所有读卡器
 */
public static void listAllReaders(){
try{
LONG ret = new LONG((int)getNextLong());
v = new JNative(DLL_LIB, "SCardListReadersA");
int i = 0;
v.setParameter(i++, cardContext);
v.setParameter(i++, 0);
v.setParameter(i++, cardReader);
v.setParameter(i++, ret.getPointer());
v.setRetVal(Type.INT);
v.invoke();
System.out.println(v+"|"+v.getRetVal());
log(ret.getValueFromPointer());
log(cardReader.getAsInt(0));
}catch(NativeException ne){
System.err.println("listAllReaders NativeException!");
releaseCard();
ne.printStackTrace();
}catch(IllegalAccessException iae){
System.err.println("listAllReaders IllegalAccessException!");
releaseCard();
iae.printStackTrace();
}
}
/**
 * 设置读卡器
 */
private static void setReader(){
try{
v = new JNative(DLL_LIB, "SCardEstablishContext");
int i = 0;
v.setRetVal(Type.INT);
v.setParameter(i++, SCARD_SCOPE_USER);
v.setParameter(i++, 0);
v.setParameter(i++, 0);
v.setParameter(i++, cardContext.getPointer());
//v.setRetVal(Type.INT);
v.invoke();
System.out.println(v+"|"+v.getRetVal());
}catch(NativeException ne){
System.err.println("setReader NativeException!");
releaseCard();
ne.printStackTrace();
}catch(IllegalAccessException iae){
System.err.println("setReader IllegalAccessException!");
releaseCard();
iae.printStackTrace();
}
}
/**
 * 设置协议
 */
private static void setProtocal(){
try{
protocol.setValue(0);
}catch(NativeException ne){
System.err.println("setProtocal NativeException!");
ne.printStackTrace();
}
}
/**
 * 连接卡片
 */
private static void connectCard(){
try{
v = new JNative(DLL_LIB, "SCardConnectA");
int i = 0;
v.setParameter(i++, cardContext);
v.setParameter(i++, Type.STRING, SCARD_CONNECTLESS_READER);
v.setParameter(i++, Type.INT, "" + SCARD_SHARE_EXCLUSIVE);
v.setParameter(i++, Type.INT, protocol.getValue().toString());
v.setParameter(i++, cardHandle);
v.setParameter(i++, 0);
v.setRetVal(Type.INT);
v.invoke();
System.out.println(v+"|"+v.getRetVal());
log("cardHandle:"+cardHandle.getAsLong(8));
}catch(NativeException ne){
System.err.println("connectCard NativeException!");
releaseCard();
ne.printStackTrace();
}catch(IllegalAccessException iae){
System.err.println("connectCard IllegalAccessException!");
releaseCard();
iae.printStackTrace();
}
}
/**
 * 释放读卡器
 */
public static void releaseCard(){
try{
v = new JNative(DLL_LIB, "SCardReleaseContext");
int i = 0;
v.setParameter(i++, cardContext);
v.setRetVal(Type.INT);
v.invoke();
System.out.println(v+"|"+v.getRetVal());
}catch(NativeException ne){
System.err.println("releaseCard NativeException!");
ne.printStackTrace();
}catch(IllegalAccessException iae){
System.err.println("releaseCard IllegalAccessException!");
iae.printStackTrace();
}
}
/**
 * @return protocol
 */
private static int getNextLong() {
// TODO Auto-generated method stub
return 0;
}
/**
 * 命令行输出变量
 */
public static void log(Object o){
System.err.println(o);
}
/**
 * 电子钱包个人化
 */
public void customizeEP(){
try{
cardContext = createPointer();
cardHandle =  createPointer();
cardReader =  createPointer();
addresses = new byte[32];
cardContext.setLongAt(0, 8);
cardReader.setIntAt(0, 16);
cardHandle.setLongAt(8, 128);
protocol.createPointer();
}catch(NativeException ne){
System.err.println("cardContext NativeException!");
ne.printStackTrace();
}
setReader();
listAllReaders();
setProtocal();
connectCard();
try{
log("cardContext:"+cardContext.getSize());
log("cardContext:"+cardContext.getAsLong(0));
}catch(NativeException ne){
System.err.println("cardReader NativeException!");
ne.printStackTrace();
} releaseCard();
}
 /** *//**
     * 分配内存,并返回指针
     */
    public Pointer createPointer() throws NativeException{
        Pointer pointer = new Pointer(MemoryBlockFactory.createMemoryBlock(getSizeOf()));
        return pointer;
    }    /** *//**
     * 内存大小
     */
    public int getSizeOf(){
        return 8 * 2;
    }
public static void main(String[] args) throws NativeException,
IllegalAccessException {
CustomizeEP cusEp = new CustomizeEP();
cusEp.customizeEP();
}
}winscard.dll中函数参数如下:SCardEstablishContext(
    IN  DWORD dwScope,
    IN  LPCVOID pvReserved1,
    IN  LPCVOID pvReserved2,
    OUT LPSCARDCONTEXT phContext);extern WINSCARDAPI LONG WINAPI
SCardReleaseContext(
    IN      SCARDCONTEXT hContext);
SCardListReadersA(
    IN      SCARDCONTEXT hContext,
    IN      LPCSTR mszGroups,
    OUT     LPSTR mszReaders,
    IN OUT  LPDWORD pcchReaders);
SCardConnectA(
    IN      SCARDCONTEXT hContext,
    IN      LPCSTR szReader,
    IN      DWORD dwShareMode,
    IN      DWORD dwPreferredProtocols,
    OUT     LPSCARDHANDLE phCard,
    OUT     LPDWORD pdwActiveProtocol);运行结果如下:
[list]
[*]winscard.dll-SCardEstablishContext|0
[*]winscard.dll-SCardListReadersA|6
[*]winscard.dll-SCardConnectA|6
[*]winscard.dll-SCardReleaseContext|6
[/list]
其中6的错误是ERROR_INVALID_HANDLE意思是问题就是这样的,只有第一个函数SCardEstablishContext成功执行,但是还不确定一定正确。。
其他的几个函数传递的参数可能有问题。请教各位达人,这种情况应该怎么处理?
最重要的是,传递的参数怎么返回?
比如传递一个pointer进去,怎么知道它的变化呢?或者是说获得其返回值。不胜感激~

解决方案 »

  1.   

    再贴下代码。。import org.xvolks.jnative.JNative;
    import org.xvolks.jnative.Type;
    import org.xvolks.jnative.exceptions.NativeException;
    import org.xvolks.jnative.pointers.Pointer;
    import org.xvolks.jnative.pointers.memory.HeapMemoryBlock;
    import org.xvolks.jnative.pointers.memory.MemoryBlockFactory;
    import org.xvolks.jnative.misc.basicStructures.LONG;
    import org.xvolks.jnative.misc.basicStructures.HWND;
    import org.xvolks.jnative.misc.basicStructures.LPARAM;
    import org.xvolks.jnative.misc.basicStructures.HANDLE;
    import org.xvolks.jnative.util.Callback;public class CustomizeEP {
    private static JNative v = null;
    /**
     * @&m_hSC
     */
    private static Pointer cardContext = null;
    /**
     * @&m_hCard
     */
    private static Pointer cardHandle = null;
    /**
     * @&psReaders
     */
    private static Pointer cardReader = null;
    /**
     * @&byte[]
     */
    private static byte[] addresses = null;
    /**
     * Protocol
     */
    private static LONG protocol = new LONG((int)getNextLong());
    /**
     * The context is a user context, and anydatabase operations
     * are performed within the domain of the user.
     */
    private static final int SCARD_SCOPE_USER = 0;
    /**
     *  This application is not willing to share this card with other applications.
     */
    private static final int SCARD_SHARE_EXCLUSIVE = 1;
    /**
     * MAX_READERNAME_LEN
     */
    private static final int MAX_READERNAME_LEN = 256;
    /**
     * MAX_ATR_LEN
     */
    private static final int MAX_ATR_LEN = 32;
    /**
     * APDU_PSE
     */
    private static final String APDU_PSE = "00A404000E315041592E5359532E4444463031";
    /**
     * 非接卡片
     */
    private static final String SCARD_CONNECTLESS_READER = "SCM Microsystems Inc. SDI010 Contactless Reader 0";
    /**
     * 加载的DLL
     */
    private static final String DLL_LIB = "winscard.dll"; /**
     * 列出所有读卡器
     */
    public static void listAllReaders(){
    try{
    LONG ret = new LONG((int)getNextLong());
    v = new JNative(DLL_LIB, "SCardListReadersA");
    int i = 0;
    v.setParameter(i++, cardContext);
    v.setParameter(i++, 0);
    v.setParameter(i++, cardReader);
    v.setParameter(i++, ret.getPointer());
    v.setRetVal(Type.INT);
    v.invoke();
    System.out.println(v+"|"+v.getRetVal());
    log(ret.getValueFromPointer());
    log(cardReader.getAsInt(0));
    }catch(NativeException ne){
    System.err.println("listAllReaders NativeException!");
    releaseCard();
    ne.printStackTrace();
    }catch(IllegalAccessException iae){
    System.err.println("listAllReaders IllegalAccessException!");
    releaseCard();
    iae.printStackTrace();
    }
    }
    /**
     * 设置读卡器
     */
    private static void setReader(){
    try{
    v = new JNative(DLL_LIB, "SCardEstablishContext");
    int i = 0;
    v.setRetVal(Type.INT);
    v.setParameter(i++, SCARD_SCOPE_USER);
    v.setParameter(i++, 0);
    v.setParameter(i++, 0);
    v.setParameter(i++, cardContext.getPointer());
    //v.setRetVal(Type.INT);
    v.invoke();
    System.out.println(v+"|"+v.getRetVal());
    }catch(NativeException ne){
    System.err.println("setReader NativeException!");
    releaseCard();
    ne.printStackTrace();
    }catch(IllegalAccessException iae){
    System.err.println("setReader IllegalAccessException!");
    releaseCard();
    iae.printStackTrace();
    }
    }
    /**
     * 设置协议
     */
    private static void setProtocal(){
    try{
    protocol.setValue(0);
    }catch(NativeException ne){
    System.err.println("setProtocal NativeException!");
    ne.printStackTrace();
    }
    }
    /**
     * 连接卡片
     */
    private static void connectCard(){
    try{
    v = new JNative(DLL_LIB, "SCardConnectA");
    int i = 0;
    v.setParameter(i++, cardContext);
    v.setParameter(i++, Type.STRING, SCARD_CONNECTLESS_READER);
    v.setParameter(i++, Type.INT, "" + SCARD_SHARE_EXCLUSIVE);
    v.setParameter(i++, Type.INT, protocol.getValue().toString());
    v.setParameter(i++, cardHandle);
    v.setParameter(i++, 0);
    v.setRetVal(Type.INT);
    v.invoke();
    System.out.println(v+"|"+v.getRetVal());
    log("cardHandle:"+cardHandle.getAsLong(8));
    }catch(NativeException ne){
    System.err.println("connectCard NativeException!");
    releaseCard();
    ne.printStackTrace();
    }catch(IllegalAccessException iae){
    System.err.println("connectCard IllegalAccessException!");
    releaseCard();
    iae.printStackTrace();
    }
    }
    /**
     * 释放读卡器
     */
    public static void releaseCard(){
    try{
    v = new JNative(DLL_LIB, "SCardReleaseContext");
    int i = 0;
    v.setParameter(i++, cardContext);
    v.setRetVal(Type.INT);
    v.invoke();
    System.out.println(v+"|"+v.getRetVal());
    }catch(NativeException ne){
    System.err.println("releaseCard NativeException!");
    ne.printStackTrace();
    }catch(IllegalAccessException iae){
    System.err.println("releaseCard IllegalAccessException!");
    iae.printStackTrace();
    }
    }
    /**
     * @return protocol
     */
    private static int getNextLong() {
    // TODO Auto-generated method stub
    return 0;
    }
    /**
     * 命令行输出变量
     */
    public static void log(Object o){
    System.err.println(o);
    }
    /**
     * 电子钱包个人化
     */
    public void customizeEP(){
    try{
    cardContext = createPointer();
    cardHandle =  createPointer();
    cardReader =  createPointer();
    addresses = new byte[32];
    cardContext.setLongAt(0, 8);
    cardReader.setIntAt(0, 16);
    cardHandle.setLongAt(8, 128);
    protocol.createPointer();
    }catch(NativeException ne){
    System.err.println("cardContext NativeException!");
    ne.printStackTrace();
    }
    setReader();
    listAllReaders();
    setProtocal();
    connectCard();
    try{
    log("cardContext:"+cardContext.getSize());
    log("cardContext:"+cardContext.getAsLong(0));
    }catch(NativeException ne){
    System.err.println("cardReader NativeException!");
    ne.printStackTrace();
    } releaseCard();
    }
     /** *//**
         * 分配内存,并返回指针
         */
        public Pointer createPointer() throws NativeException{
            Pointer pointer = new Pointer(MemoryBlockFactory.createMemoryBlock(getSizeOf()));
            return pointer;
        }    /** *//**
         * 内存大小
         */
        public int getSizeOf(){
            return 8 * 2;
        }
    public static void main(String[] args) throws NativeException,
    IllegalAccessException {
    CustomizeEP cusEp = new CustomizeEP();
    cusEp.customizeEP();
    }
    }