3

创建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)

我需要在安装过程中不自行注册其 DLL 的程序中使用的 COM 对象。

为了注册 DLL,我根据这篇文章使用了 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);
4

1 回答 1

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。我还关注了您提到的同一篇文章,但在处理小程序时不起作用。

使用命令行转到x86框架。

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

注册做同样的事情

regasm.exe path_to_your_dll.dll /codebase

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

于 2013-08-30T12:52:59.650 回答