使用 Com4j 或 Jacob 连接 64 位 Java 和 32 位 COM dll
我有以下配置:
1) Windows 10 64 位
1) Windows 10 64-bit
2) 只有 32 位版本且可通过 COM 获得的应用程序.
2) An application that has only a 32-bit version and is available through COM.
我使用 tlbimb.jar 访问 32 位应用程序的 .dll 文件以生成所需的接口,我成功了.
I access the .dll file of the 32-bit application using the tlbimb.jar to generate the interfaces needed and I succeed.
场景 1:我尝试使用 Java 8 32-bit 安装访问 32 位应用程序.我可以通过 COM 调用方法成功没有任何问题.
Scenario 1: I try to access the 32-bit application using the Java 8 32-bit installation. I can invoke the methods through COM succesfully without any problem.
场景 2:我尝试使用 Java 8 64 位安装访问 32 位应用程序.我收到一条错误消息:
Scenario 2: I try to access the 32-bit application using a Java 8 64-bit installation. I get an error message:
Exception in thread "main" com4j.ExecutionException: com4j.ComException: 80040154 CoCreateInstance failed : Class not registered : .com4j.cpp:153
我在 Stackoverflow/Google 上搜索了异常,我做了以下操作:
I searched for the exceptin on Stackoverflow/Google and I have done the following:
1) 使用 Sys64WOW/regsvr32.exe 和 System32/regsvr32.exe 注册应用程序的 dll
1) Registered the application's dll using both Sys64WOW/regsvr32.exe and the System32/regsvr32.exe
2) 使用 Sys64WOW/regsvr32.exe 和 System32/regsvr32.exe 注册 com4j dll(32 位和 64 位)
2) Registered the com4j dll (both 32 and 64 bit) using both Sys64WOW/regsvr32.exe and the System32/regsvr32.exe
3) 复制 Sys64WOW 和 System32 文件夹中的 dll.
3) Copied the dlls in the Sys64WOW and System32 folders.
我已经分别完成了以上所有操作,检查了所有可能的组合.上述使用 64 位 Java 的错误仍然存在.
I have done all the above separatelly, checking all the possible combinations. The afortementioned error using 64-bit Java still exists.
我尝试过使用另一个网桥(Jacob).在 32 位 Java 上成功,在 64 位 Java 上失败.
I have tried using another bridge (Jacob). On 32-bit Java it succeeds, on 64-bit Java it fails.
我有一个问题想问可能知道的人:有什么方法可以连接一个应用程序,据我所知,该应用程序仅提供 Win32 COM dll[1],使用任何可用的 Java/COM 桥和 Java 64bit?还是只是 32 位 COM + 64 位 Java 无法连接?
I have a question for someone that might knows: Is there any way to connect an app that, as far as I can tell, offers only a Win32 COM dll[1], using any of the available Java/COM bridges and Java 64bit? Or simply 32-bit COM + 64-bit Java are not connectable?
[1]:我检查了 OLE/COM 查看器,在类型库下只有一个条目 "0 Win32 = ,所以我暗示这意味着没有 Win64 COM dll,对吧?
[1]: I checked the OLE/COM Viewer and under the Type Library there's is only one entry "0 Win32 = , so I implied that this means there's no Win64 COM dll, right?
推荐答案
这不起作用,因为您的客户端进程和 COM DLL 的位数不匹配.在尝试创建新实例时,CoCreateInstance
会尝试在注册表的 64 位配置单元中查找相关的类信息.这会失败,因为它实际上位于您注册 DLL 的 32 位配置单元中.
This won't work because the bitness of your client process and COM DLL don't match. When trying to create a new instance, CoCreateInstance
tries to find related class information in the 64-bit hive of the registry. This fails because its actually located in the 32-bit hive, where you registered the DLL.
要解决这个问题,您可以选择使用代理进程,它允许您的 CoClass 在不同的本地进程中实例化.然后,您的客户可以通过 IPC 与代理进行通信(请参阅 这里了解更多信息).
To remedy this, you have the option of using a surrogate process, which allows your CoClass to be instantiated in a distinct native process. Your client can then communicate with the surrogate via IPC (see here for further info).
作为快速入门,您可以将您的 CoClass 标记为默认 dllhost.exe
代理进程的候选者:OLE/COM Object Viewer (x86) as Admin > All Objects > [Your CoClass]> 实施 > 使用代理过程(将路径留空).
As a quick start, you can mark your CoClass as a candidate for the default dllhost.exe
surrogate process: OLE/COM Object Viewer (x86) as Admin > All Objects > [Your CoClass] > Implementation > Use Surrogate Process (leave path empty).
如果您打算分发您的应用程序,您可以将此信息放在 REG 脚本中或将您的 DLL 导入 COM+ 服务器应用程序.
If you intend to distribute your application, you may place this information in a REG script or import your DLL into a COM+ server application.
相关文章