简体   繁体   English

JNA 中的 Application.advise() ComException

[英]Application.advise() ComException in JNA

I'm handling Excel in Java. We are accessing Excel application data using JNA's COM API. I want to hook up when the Excel application closes.我在Java中处理Excel。我们正在使用JNA的COM API访问Excel应用程序数据。我想在Excel应用程序关闭时连接。 So, I'm using a JNA-based library.所以,我使用的是基于 JNA 的库。

your text com.sun.jna.platform.win32.COM.util.Factory your text eu.doppel_helix.jna.tlb.excel1.AppEventsListenerHandler your text eu.doppel_helix.jna.tlb.excel1.Application... your text com.sun.jna.platform.win32.COM.util.Factory your text eu.doppel_helix.jna.tlb.excel1.AppEventsListenerHandler your text eu.doppel_helix.jna.tlb.excel1.Application...

However, when registering an event handler in Application.advise(), it is blocked, so if you do it with threads, the error will occur as shown below.但是,当在 Application.advise() 中注册事件处理程序时,它会被阻塞,因此如果您使用线程进行,则会出现如下所示的错误。

Exception in thread "Thread-1" com.sun.jna.platform.win32.COM.COMException: advise: Interface must define a value for either iid via the ComInterface annotation at com.sun.jna.platform.win32.COM.util.ProxyObject.advise(ProxyObject.java:287) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ...线程“Thread-1”中的异常 com.sun.jna.platform.win32.COM.COMException:建议:接口必须通过位于at com.sun.jna.platform.win32.COM.util.ProxyObject.advise(ProxyObject.java:287) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)的 ComInterface 注释为任一 iid 定义一个值at com.sun.jna.platform.win32.COM.util.ProxyObject.advise(ProxyObject.java:287) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ...

What should I do to prevent the above exception from occurring?我应该怎么做才能防止上述异常发生? Fundamentally, how do I catch an event where the Excel application that I ran closes?从根本上说,我如何捕获我运行的 Excel 应用程序关闭的事件? Please help me.请帮我。 I don't have Windows COM operation method and API knowledge.我没有Windows COM操作方法和API知识。

Thank you.谢谢你。

I did below:我在下面做了:

Factory factory = new Factory( myComThread ); Application excelApp= factory.createObject(Application.class); excelApp.advise(Application.class, new MyAppEventListenerHandler()); //no response. blocking

I expected when Excep application is closing I can catch that event.我预计当 Excep 应用程序关闭时我可以捕获该事件。

You can handle the Application.WorkbookBeforeClose event which is fired immediately before any open workbook closes.您可以处理在任何打开的工作簿关闭之前立即触发的Application.WorkbookBeforeClose事件。 If there is a single workbook opened the Excel Application instance can be closed with it.如果打开了一个工作簿,则 Excel 应用程序实例可以用它关闭。

Be aware, Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.请注意,Microsoft 目前不建议也不支持来自任何无人值守、非交互式客户端应用程序或组件(包括 ASP、ASP.NET、DCOM 和 NT 服务)的 Microsoft Office 应用程序自动化,因为 Office 可能表现出不稳定的行为和/ 或在此环境中运行 Office 时出现死锁。

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution.如果您正在构建一个在服务器端上下文中运行的解决方案,您应该尝试使用已被安全用于无人值守执行的组件。 Or, you should try to find alternatives that allow at least part of the code to run client-side.或者,您应该尝试找到至少允许部分代码在客户端运行的替代方案。 If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully.如果您使用来自服务器端解决方案的 Office 应用程序,该应用程序将缺少许多成功运行所必需的功能。 Additionally, you will be taking risks with the stability of your overall solution.此外,您将承担整体解决方案稳定性的风险。 Read more about that in the Considerations for server-side Automation of Office article.服务器端自动化 Office 文章的注意事项中阅读更多相关信息。

If you deal with open XML documents only you may consider using the Open XML SDK instead of automating Excel. See Welcome to the Open XML SDK 2.5 for Office for more information about that.如果您只处理打开的 XML 文档,您可以考虑使用 Open XML SDK 而不是自动化 Excel。有关更多信息,请参阅 欢迎使用 Open XML SDK 2.5 for Office

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM