JNATIVE 调用dll方法

JNATIVE能很方面的调用dll中的方法:
C语言代码:
#include "stdafx.h"
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <string.h>


typedef int (*bOpenUsb20Video)();
typedef int (*sGetBarData)(char *out);
typedef int (*bCloseUsb20)();

int main(int argc, char* argv[])
{

HINSTANCE hDll; //DLL句柄
bOpenUsb20Video DllbOpenUsb20Video;
sGetBarData DllsGetBarData;
bCloseUsb20 DllbCloseUsb20;

hDll = LoadLibrary("dllLpDecode.dll");
if (hDll != NULL)
{
DllbOpenUsb20Video = (bOpenUsb20Video)GetProcAddress(hDll,"bOpenUsb20Video");
if(DllbOpenUsb20Video==NULL)
return 0;

DllsGetBarData = (sGetBarData)GetProcAddress(hDll,"sGetBarData");
if(DllsGetBarData==NULL)
return 0;

DllbCloseUsb20 = (bCloseUsb20)GetProcAddress(hDll,"bCloseUsb20");
if(DllbCloseUsb20==NULL)
return 0;

}


if (0 != DllbOpenUsb20Video ())
return 0;

int ret;
char out[256];
int count=10;
while (count)
{
ret = DllsGetBarData (out);
if (ret == 1)
{
printf ("result:%s\r\n",out);
count --;
}

Sleep (100);
}

DllbCloseUsb20 ();

FreeLibrary(hDll);


return 0;

}


使用jnative改写的方法:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.tfsm.movie.camera;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.xvolks.jnative.JNative;
import org.xvolks.jnative.Type;
import org.xvolks.jnative.pointers.Pointer;
import org.xvolks.jnative.pointers.memory.MemoryBlockFactory;

/**
*
* @author Administrator
*/
public class HY100CameraDecoder {

static {
//加载HY100驱动
System.loadLibrary("lib/dllLpDecode");
}

public boolean openCamera() {
try {
JNative openCamera = new JNative("lib/dllLpDecode", "bOpenUsb20Video");
openCamera.setRetVal(Type.INT);
openCamera.invoke();
return openCamera.getRetValAsInt() == 0;
} catch (Exception ex) {
Logger.getLogger(HY100CameraDecoder.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
}

public CameraDecodeResult getDecodeData() {
try {
JNative decodeData = new JNative("lib/dllLpDecode", "sGetBarData");
Pointer p = new Pointer(MemoryBlockFactory.createMemoryBlock(4 * 256));
decodeData.setParameter(0, p);
decodeData.setRetVal(Type.INT);
decodeData.invoke();
int resultCode = decodeData.getRetValAsInt();
CameraDecodeResult reuslt = new CameraDecodeResult();
//为1代表成功
if (resultCode == 1) {
String result = new String(p.getAsString().getBytes(), "UTF-8");
return reuslt.setSuccess(true).setResult(result);
}else{
return reuslt;
}
} catch (Exception ex) {
Logger.getLogger(HY100CameraDecoder.class.getName()).log(Level.SEVERE, null, ex);
return new CameraDecodeResult();
}
}

public void closeCamera() {
try {
JNative closeCamera = new JNative("lib/dllLpDecode", "bCloseUsb20");
closeCamera.setRetVal(Type.INT);
closeCamera.invoke();
} catch (Exception ex) {
Logger.getLogger(HY100CameraDecoder.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值