一、背景介绍
现有标准的 win32 com组件,有如下的参数:
属性 | 值 | 说明 |
Program ID | yinhai.yh_hb_sctr | |
COM ClassID | ||
COM ClassName | COClass_yh_hb_sctr | |
Interface Type | Dual Interface | |
Interface Name | Iyh_hb_sctr |
具有一个方法:
yh_hb_call( string astr_jyhb, string astr_jysr, ref string astr_jysc)
需要使用Java进行调用。
二、采用jacob进行调用
2.1 添加依赖
<dependency><groupId>com.hynnet</groupId><artifactId>jacob</artifactId><version>1.18</version>
</dependency>
2.2 将dll文件放置在java_home\bin目录中
package com.msip;import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;public class TestCom {public static void main(String[] args) {ComThread.InitSTA(false);ActiveXComponent component = new ActiveXComponent("yinhai.yh_hb_sctr");try {// 获取 COM 组件的 Dispatch 对象Dispatch dispatch = (Dispatch) component.getObject();String rest = null ;String input = "{}";// 直接调用方法Variant result = Dispatch.invoke(dispatch, "yh_hb_call", Dispatch.Method,new Object[]{"1101", input,rest}, new int[1]);// 获取方法调用的结果System.out.println("Result: " + result.toString());System.out.println("rest="+rest);} finally {// 关闭 COM 组件,释放资源component.safeRelease();}}
}
三、遇到的问题
3.1 jdk版本问题,文中的com组件是32位系统的,所以程序的jdk必须也是32位的。
3.2 配套的dll程序必须在类路径中
3.3 代码中的try finally语法块是必须的,否则,机会提示找不到对应的方法名。