为什么我没有通过 Com4J 接收 COM 事件?

发布于 2024-09-09 05:35:59 字数 659 浏览 6 评论 0原文

我正在使用 Com4J 与 Microsoft Outlook 交互。我已经按照 Com4J 教程生成了 Java 类型定义。以下是等待用户关闭电子邮件的一些代码示例。

// Registers my event handler
mailItem.advise(
        ItemEvents.class,
        new ItemEvents() {
            @Override
            public void close(Holder<Boolean> cancel) {
                // TODO Auto-generated method stub
                super.close(cancel);
                System.out.println("Closed");
            }
        }
    );

// Displays the email to the user
mailItem.display();

此代码成功向用户显示电子邮件。不幸的是,当用户关闭窗口时,我的程序永远不会打印“Closed”。

I am using Com4J to interact with Microsoft Outlook. I have generated the Java type definitions as per the Com4J tutorial. Here is an example of some code that waits for the user to close an email.

// Registers my event handler
mailItem.advise(
        ItemEvents.class,
        new ItemEvents() {
            @Override
            public void close(Holder<Boolean> cancel) {
                // TODO Auto-generated method stub
                super.close(cancel);
                System.out.println("Closed");
            }
        }
    );

// Displays the email to the user
mailItem.display();

This code successfully displays the email to the user. Unfortunately, my program never prints "Closed" when the user closes the window.

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

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

发布评论

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

评论(1

少女的英雄梦 2024-09-16 05:35:59

当 Com4J 生成事件类(在我的场景中为 ItemEvents)时,所有生成方法的默认行为是抛出 UnsupportedOperationException(请参阅 com4j.tlbimp.EventInterfaceGenerator 类了解详细信息)。

例如,下面是我的匿名类重写的 ItemEvents 类的 close 方法:

@DISPID(61444)
public void close(Holder<Boolean> cancel) {
    throw new UnsupportedOperationException();
}

因此,当我的匿名类调用 super.close(cancel);< /code>,父类抛出 UnsupportedOperationException,阻止执行到达我的 System.out.println("Closed"); 语句。因此,我的匿名类应该确实看起来像这样:

mailItem.advise(
        ItemEvents.class,
        new ItemEvents() {
            @Override
            public void close(Holder<Boolean> cancel) {
                System.out.println("Closed");
            }
        }
    );

令我惊讶的是,Com4J 似乎只是忽略了从事件中抛出的UnsupportedOperationException完全处理程序,让我没有任何迹象表明实际发生了什么。我编写此代码是为了演示:

mailItem.advise(
        ItemEvents.class,
        new ItemEvents() {
            @Override
            public void close(Holder<Boolean> cancel) {
                System.out.println("Getting ready to throw the exception...");
                throw new RuntimeException("ERROR! ERROR!");
            }
        }
    );

程序发出此输出:

Getting ready to throw the exception...

但是,没有任何迹象表明曾经抛出过 RuntimeException。

When Com4J generates an event class (ItemEvents in my scenario), the default behavior for all generated methods is to throw an UnsupportedOperationException (see the com4j.tlbimp.EventInterfaceGenerator class for details).

For example, here is the close method of the ItemEvents class that my anonymous class overrides:

@DISPID(61444)
public void close(Holder<Boolean> cancel) {
    throw new UnsupportedOperationException();
}

Therefore, when my anonymous class calls super.close(cancel);, the parent class throws an UnsupportedOperationException, preventing execution from reaching my System.out.println("Closed"); statement. Therefore, my anonymous class should really have looked like this:

mailItem.advise(
        ItemEvents.class,
        new ItemEvents() {
            @Override
            public void close(Holder<Boolean> cancel) {
                System.out.println("Closed");
            }
        }
    );

What surprised me is that Com4J appears to have simply ignored the UnsupportedOperationException thrown from the event handler altogether, leaving me no indication of what actually happened. I wrote this code to demonstrate:

mailItem.advise(
        ItemEvents.class,
        new ItemEvents() {
            @Override
            public void close(Holder<Boolean> cancel) {
                System.out.println("Getting ready to throw the exception...");
                throw new RuntimeException("ERROR! ERROR!");
            }
        }
    );

The program emits this output:

Getting ready to throw the exception...

However, there is no indication that a RuntimeException was ever thrown.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文