jnative实现调用动态库(2)

本文介绍了一个使用Java调用DLL文件的具体实例,演示了如何通过JNative库加载SCReader.dll并调用其中的SCHelp_HexStringToBytes函数。此函数用于将十六进制字符串转换为字节数组。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2007-07-25 20:33

简单测试,Javadoc 下和官方网上有些例子,下面的是我随便从IC读卡程序中找了个DLL进行的测试:
SCReader.dll 下的SCHelp_HexStringToBytes()函数原型

SCREADER_API WINAPI long SCHelp_HexStringToBytes(

LPCTSTR pSrc,

BYTE* pTar,

int MaxCount

);

注意:dll文件需要放到System32下,否则可能找不到

 

通过Jnative 用java 来调用代码如下:

package onlyfun.dllcall;
import org.xvolks.jnative.JNative;
import org.xvolks.jnative.exceptions.NativeException;
import org.xvolks.jnative.pointers.Pointer;
import org.xvolks.jnative.pointers.memory.MemoryBlockFactory;
import org.xvolks.jnative.Type;
public class UserCall {
   
/**
   
* return 转换成功的字节数
   
*/
   
static JNative Something = null;
   
static Pointer pointer;
   
public String getSomething(String pSrc, Pointer pTar, int MaxCount) throws NativeException, IllegalAccessException{
     
      
try{
          
if(Something == null){
              pTar =
new Pointer(MemoryBlockFactory.createMemoryBlock(36));
             
Something = new JNative("SCReader.DLL", "SCHelp_HexStringToBytes");
//
利用org.xvolks.jnative.JNative 来装载 SCReader.dll,并利用其SCHelp_HexStringToBytes方法
             
Something.setRetVal(Type.INT);
//
指定返回参数的类型
           }
          
int i=0;
          
Something.setParameter(i++,pSrc);
          
Something.setParameter(i++,pTar);
          
Something.setParameter(i++,MaxCount);
           System.
out.println("调用的DLL文件名为:"+Something.getDLLName());
           System.
out.println("调用的方法名为:"+Something.getFunctionName());
//传值

           Something.invoke();//调用方法
          
return Something.getRetVal();
       }
finally{
          
if(Something!=null){
             
Something.dispose();//释放
           }
       }
    }
   
public Pointer creatPointer() throws NativeException{
      
pointer = new Pointer(MemoryBlockFactory.createMemoryBlock(36));
      
pointer.setIntAt(0, 36);
      
return pointer;
    }

   
public static void main(String[] args) throws NativeException, IllegalAccessException {
       UserCall uc =
new UserCall();
       String result = uc.getSomething(
"0FFFFF", uc.creatPointer(), 100);
       System.
err.println("转换成功的字节数为:"+result);
       TestCallback.runIt();
    }
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值