JACOB (Java/COM/ActiveX) - 如何解决事件处理问题?

发布于 2024-08-31 06:12:10 字数 1564 浏览 8 评论 0原文

我正在尝试使用 JACOB 与 COM 对象交互。

我能够调用对象的初始化方法(并获取其属性),但没有返回任何事件。代码引用如下。
我有一个示例 HTML+Javascript 页面(在 IE 中运行),它成功接收来自同一对象的事件。
我正在考虑以下选项,但希望有任何具体的故障排除想法...

  • 将我的 Java 程序发送给开发 COM 对象的团队,并获得 他们寻找任何可疑的东西(该对象是否有办法知道是否有客户端正在侦听其事件,以及它们是否已成功交付?)

  • 进入 JACOB 的本机部分并尝试对此进行调试边。这有点可怕,因为我的 C++ 很生疏,而且我从来没有为 Windows 编程过。

public static void main(String[] args) {
    try {
        ActiveXComponent c = new ActiveXComponent(
                              "CLSID:{********-****-****-****-************}"); // My object's clsid
        if (c != null) {
            System.out.println("Version:"+c.getProperty("Version"));
            InvocationProxy proxy = new InvocationProxy() {                 
                @Override
                public Variant invoke(String methodName, Variant[] targetParameters) {
                    System.out.println("*** Event ***: " + methodName);
                    return null;
                }
            };
            DispatchEvents de = new DispatchEvents((Dispatch) c.getObject(), proxy);
            c.invoke("Init", new Variant[] {
                    new Variant(10), //param1
                    new Variant(2), //param2
                     });
            System.out.println("Wating for events ...");
            Thread.sleep(60000); // 60 seconds is long enough
            System.out.println("Cleaning up ...");
            c.safeRelease();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        ComThread.Release();
    }
}

I'm trying to use JACOB to interact with a COM object.

I was able to invoke an initialization method on the object (and to get its properties), but am not getting any events back. The code is quoted below.
I have a sample HTML+Javascript page (running in IE) that successfully receives events from the same object.
I'm considering the following options, but would appreciate any concrete troubleshooting ideas ...

  • Send my Java program to the team who developed the COM object, and have
    them look for anything suspicious on their side (does the object have a way on knowing whether there's a client listening to its events, and whether they were successfully delivered?)

  • Get into the native parts of JACOB and try to debug on that side. That's a little scary given that my C++ is rusty and that I've never programmed for Windows.

public static void main(String[] args) {
    try {
        ActiveXComponent c = new ActiveXComponent(
                              "CLSID:{********-****-****-****-************}"); // My object's clsid
        if (c != null) {
            System.out.println("Version:"+c.getProperty("Version"));
            InvocationProxy proxy = new InvocationProxy() {                 
                @Override
                public Variant invoke(String methodName, Variant[] targetParameters) {
                    System.out.println("*** Event ***: " + methodName);
                    return null;
                }
            };
            DispatchEvents de = new DispatchEvents((Dispatch) c.getObject(), proxy);
            c.invoke("Init", new Variant[] {
                    new Variant(10), //param1
                    new Variant(2), //param2
                     });
            System.out.println("Wating for events ...");
            Thread.sleep(60000); // 60 seconds is long enough
            System.out.println("Cleaning up ...");
            c.safeRelease();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        ComThread.Release();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水水月牙 2024-09-07 06:12:10

以下是针对我的具体问题的解决方案:

事实证明,COM 对象被编写为不可见的 ActiveX 控件,该控件假定它在窗口上下文中运行。这是在我向组件的开发团队提供基于 JACOB 的程序后“发现”的。

JACOB 似乎没有提供窗口上下文。

我们案例中的解决方案是摆脱 COM(并使用 JNA 直接调用本机 DLL 中的 C 函数)。

Here's the solution to my specific problem:

It turned out that the COM object was written as an invisible ActiveX control that assumes it's running in the context of a window. This was "discovered" by the development team of the component after I gave them the JACOB-based program.

Providing the window context is something that JACOB doesn't seem to do.

The solution in our case will be to get rid of COM (and use JNA to directly call C functions in the native DLL).

不念旧人 2024-09-07 06:12:10

解决获取事件问题的一个简单方法是打开 Jacob 调试,通过使用选项 com.jacob.debug=true 运行 java:

java -Dcom.jacob.debug=true ...

所有接收到的事件都显示在标准错误(或输出)上:

Thread-0: InvocationProxy: trying to invoke Change on Sink@882c01f
Thread-0: InvocationProxy: listener (Sink@882c01f) doesn't implement Change

An easy way to troubleshoot getting events is turning Jacob debugging on, by running java with option com.jacob.debug=true:

java -Dcom.jacob.debug=true ...

All received events are presented on standard error (or output):

Thread-0: InvocationProxy: trying to invoke Change on Sink@882c01f
Thread-0: InvocationProxy: listener (Sink@882c01f) doesn't implement Change
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文