java - 无法共同创建对象/找不到名字对象 |雅各布

标签 java com jacob

使用 JACOB 创建 ActiveXComponent 时出现以下错误。

com.jacob.com.ComFailException: Can't co-create object
    at com.jacob.com.Dispatch.createInstanceNative(Native Method)
    at com.jacob.com.Dispatch.<init>(Dispatch.java:99)
    at com.jacob.activeX.ActiveXComponent.<init>(ActiveXComponent.java:58)
    at com.paston.jacobtest.RidderIQ.main(RidderIQ.java:30)

我需要从一个程序中使用的 COM 对象,该程序在安装期间不会自行注册其 DLL。

为了注册 DLL,我根据 this 使用了 64 位版本的 RegAsm可能有帮助的文章。此外,我尝试加载外部程序中的每个 DLL,因为我怀疑加载依赖项可能存在“某些”错误。

这是我当前的代码:

public static void main(String[] args) {

    String dllDir = "C:\\Program Files (x86)\\Ridder iQ Client\\Bin\\";
    File folder = new File( dllDir );

    for (final File fileEntry : folder.listFiles()) {
        String str = fileEntry.getName();
        if (str.substring(str.lastIndexOf('.') + 1).equals("dll")) {
            System.out.println(fileEntry.getName());
            System.load(dllDir + str);
        }
    }

    try {
        ActiveXComponent example = new ActiveXComponent("RidderIQSDK");
    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }

}

将名称更改为 clsid 时,出现不同的异常。

com.jacob.com.ComFailException: Can't find moniker
at com.jacob.com.Dispatch.createInstanceNative(Native Method)
at com.jacob.com.Dispatch.<init>(Dispatch.java:99)
at com.jacob.activeX.ActiveXComponent.<init>(ActiveXComponent.java:58)
at com.paston.jacobtest.RidderIQ.main(RidderIQ.java:28)

我让 JACOB 在另一个使用系统的随机对象的测试中使用我的代码。

    ActiveXComponent random = new ActiveXComponent("clsid:4E77EC8F-51D8-386C-85FE-7DC931B7A8E7");
    Object obj = random.getObject();

    Object result = Dispatch.call((Dispatch) obj, "Next");
    System.out.println("Result: "+result);

最佳答案

我尝试了所有的解决方案,终于成功破解了JACOB相关的密码。按照以下示例代码创建您的代码。

public static void main(String[] args) {
        String libFile = System.getProperty("os.arch").equals("amd64") ? "jacob-1.17-x64.dll" :"jacob-1.17-x86.dll";
        try{
            /**
             * Reading jacob.dll file
             */
            InputStream inputStream = certificatemain.class.getResourceAsStream(libFile);
            /**
             *  Step 1: Create temporary file under <%user.home%>\AppData\Local\Temp\jacob.dll 
             *  Step 2: Write contents of `inputStream` to that temporary file.
             */
            File temporaryDll = File.createTempFile("jacob", ".dll");
            FileOutputStream outputStream = new FileOutputStream(temporaryDll);
            byte[] array = new byte[8192];
            for (int i = inputStream.read(array); i != -1; i = inputStream.read(array)){
                outputStream.write(array, 0, i);
            }
            outputStream.close();
            /* Temporary file will be removed after terminating-closing-ending the application-program */
            System.setProperty(LibraryLoader.JACOB_DLL_PATH, temporaryDll.getAbsolutePath());
            LibraryLoader.loadJacobLibrary();

            ActiveXComponent comp=new ActiveXComponent("Com.Calculation");        
            System.out.println("The Library been loaded, and an activeX component been created");

            int arg1=100;
            int arg2=50;
            //using the functions from the library:        
            int summation=Dispatch.call(comp, "sum",arg1,arg2).toInt();
            System.out.println("Summation= "+ summation);
        }catch(Exception e){
            e.printStackTrace();
        }
}

现在让我告诉您如何注册您的 DLL。我也关注了你提到的同一篇文章,但在处理小程序时没有工作。

使用命令行转到 x86 框架。

C:\Windows\Microsoft.NET\Framework\v2.0.50727

注册做同样的事情

regasm.exe path_to_your_dll.dll/codebase

不要传递除/codebase 之外的任何其他标志。你已经完成了它......你仍然发现任何问题让我知道......

关于java - 无法共同创建对象/找不到名字对象 |雅各布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16147193/

相关文章:

Java整数ArrayList返回特定范围内的元素

java - Java Swing中如何实现空闲任务

python - 使用 win32inet.WinHttpGetProxyForUrl 的正确方法是什么

java - Jacob:com.jacob.com.ComFailException:无法共同创建对象

java - 从服务通过 JACOB 调用时,Office 2007 无法打开文件

java - 运行 Jar 文件不起作用。找不到主程序

java - 将 maven 依赖项从 spring 3.0 更新到 3.1.1 并将 hibernate 3.6 更新到 4.0 后。很多错误即将到来

.net - 调用Word.Documents.Add后WinWord.exe不会退出-Word .NET Interop

c# - 在 IDL 库中包含 C# 接口(interface)

java - 使用 JACOB (Java) 保存 Word 文档