现有标准的 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 |
- <dependency>
- <groupId>com.hynnetgroupId>
- <artifactId>jacobartifactId>
- <version>1.18version>
- dependency>

- 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语法块是必须的,否则,机会提示找不到对应的方法名。