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

时间:2013-04-22 12:31:27

标签: 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使用系统的Random对象在另一个测试中使用我的代码。

    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);

1 个答案:

答案 0 :(得分:1)

我尝试了所有解决方案,最终成功破解了与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。我也跟着你提到的同一篇文章但在你处理applet时没有工作。

使用命令行转到x86框架。

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

注册与

相同

regasm.exe path_to_your_dll.dll /codebase

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