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[]){ HINSTANCEhDll; //DLL句柄bOpenUsb20VideoDllbOpenUsb20Video;sGetBarDataDllsGetBarData;bCloseUsb20DllbCloseUsb20; 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); } }}