JNATIVE 호출 dll 메서드

4288 단어 jnative
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
",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);
        }
    }
}

좋은 웹페이지 즐겨찾기