JACOB (Java/COM/ActiveX) - 如何解决事件处理问题?
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是针对我的具体问题的解决方案:
事实证明,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).
解决获取事件问题的一个简单方法是打开 Jacob 调试,通过使用选项 com.jacob.debug=true 运行 java:
所有接收到的事件都显示在标准错误(或输出)上:
An easy way to troubleshoot getting events is turning Jacob debugging on, by running java with option
com.jacob.debug=true
:All received events are presented on standard error (or output):