无法共同创建对象/找不到名字| 雅各(Can't co-create object /

2019-08-31 18:11发布

当创建一个ActiveXComponent使用JACOB我碰到下面的错误。

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的程序中使用。

要注册我根据所用RegAsm的64位版本的DLL 这个文章可能会有所帮助。 另外,我试图加载外部程序的每个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);

Answer 1:

我尝试了所有的解决方案并最终成功破解相关的雅各的代码。 创建你的代码按下面的示例代码。

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 。 你用它做......不过您发现任何问题,让我知道...



文章来源: Can't co-create object / Can't find moniker | Jacob
标签: java com jacob