---
title: "java中使用JNative调用dll方法"
author: "xurichusheng"
date: "2014-03-03T16:02:07Z"
tags: []
categories: ["编程语言"]
slug: "java-jnative-dll"
draft: false
lastmod: "2014-03-03T16:23:13Z"
id: 2024969
url: "https://xurichusheng.iteye.com/blog/2024969"
---

使用JNative调用dll方法,

dll 文件名为 : IMLib.dll、MTLib.dll、MT\_PlaneLib.dll、InfraredSDK.dll。

 

这些dll之间有相互依赖关系。

 

这些dll文件都是自己写的C++代码生成的，由于涉及到商业，故在此不提供。

 

使用到的 jar 包是 ： JNative.jar  见附件

 

1\. C++接口及其描述

```
HANDLE IFR_API IFR_LoadFile(HWND WndWindow, char *cFileName, int iFileType, int *nBeginTime, int *nEndTime);
函数说明：装载录像文件。此函数会自动调用 IFR_Init函数
输入参数：	
WndWindow：图像播放设备的句柄，也是消息接收句柄，如果不需要通过SDK来显示图像只要传递NULL就可以。
cFileName：要加载的文件名及路径
iFileType：要加载的文件类型（0为DLI，1为DLV文件，2为JPG）
nBeginTime：文件起始时间
nEndTime：文件结束时间
返回值：> 0 加载文件后的操作句柄，同IFR_Init 返回的句柄。
	<= 0 加载失败
```

 

2.  在java中使用junit方法测试（junit4）

 

TestDll.java

```
import org.junit.Before;
import org.junit.Test;
import org.xvolks.jnative.JNative;
import org.xvolks.jnative.Type;
import org.xvolks.jnative.exceptions.NativeException;

public class TestDll {

	@Before
	public void setUp() throws Exception {

		try {

			String userDir = System.getProperty("user.dir")
					+ "\\src-resources\\dll\\";
			System.load(userDir.concat("IMLib.dll"));
			System.load(userDir.concat("MTLib.dll"));
			System.load(userDir.concat("MT_PlaneLib.dll"));
			System.load(userDir.concat("InfraredSDK.dll"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @Title: test
	 * @deprecated: 使用JNative调用dll方法
	 * @author 
	 * @date 2014-3-3
	 */
	@Test
	public void loadFile() {
		try {
			String userDir = System.getProperty("user.dir");
			// 红外图像路径
			String imagePath = userDir+"\\src-resources\\images\\M113236.DLI";
			// 要加载的文件类型（0为DLI，1为DLV文件，2为JPG）
			Integer fileType = new Integer(0);
			// 参数说明:InfraredSDK为dll名, IFR_LoadFile为要调用的函数名
			JNative jn = new JNative("InfraredSDK", "IFR_LoadFile");
			// 设置此函数的返回值
			jn.setRetVal(Type.INT);

			//
			// jn.setParameter(0, Type.STRING, "");
			jn.setParameter(1, Type.STRING, imagePath);
			jn.setParameter(2, Type.INT, fileType.toString());
			// jn.setParameter(3, Type.STRING, "");
			// jn.setParameter(4, Type.STRING, "");

			// 执行
			jn.invoke();

			// 返回值
			int retVal = Integer.parseInt(jn.getRetVal());

			// 打印函数返回值
			System.out.println(retVal);

			if (retVal > 0) {
				System.out.println("加载红外图像成功");
			} else {
				System.out.println("加载红外图像失败");
			}

		} catch (NativeException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
```

 

3\. 返回结果

```
1230045256
加载红外图像成功
```