jacob – Can’t co-create object

22Apr12

Probably you have a Java Application that uses jacob library to call a method inside a Windows COM object.

objActiveXComponent = new ActiveXComponent("randomCOMObjectName");
result = Dispatch.call(objActiveXComponent, "myMethod", input);

When executing the above code, you may run into this error:

com.jacob.com.ComFailException: Can’t co-create object
at com.jacob.com.Dispatch.createInstance(Native Method)

First, a little bit of theory.

Intro on COM objects

A COM object exposes a piece of functionality. For example if you want to manipulate Excel, there is a COM object that encapsulates all the methods needed to do pretty much anything you want (create charts, Pivot Tables, etc). Each COM object can be uniquely identified via a 128-bit number, e.g. {0EA22763-9933-11D0-A535-00C04AEV3F} called or CLSID ,or GUID

  • All objects available in your system can be seen using regedit.exe. Open it and go to HKEY_CLASSES_ROOT\CLSID.

Nobody wants to remember a 128 bit number, so an additional level of abstraction exists for identifying COM objects, called ProgID which is basically an alias.

  • The GUID – ProgID mapping is also stored in the Windows Registry. Open regedit and browse to HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Word.Application\CLSID  to view the CLSID of the COM object that allows you manipulating the Word application.

Functionality offered by a COM object is exposed through interfaces, which contain one or more methods. Each interface is uniquely identifiable via a CLSID as well.

  • For example, Excel.Application COM object exposes the Charts interface, which contains the Add method.

Object Browser in Visual Studio can be used for browsing the
COM objects and the interfaces exposed by them: Click on View->Object Browser.  From the Browse select list, choose Edit Custom Component Set Switch to the COM tab

Using a COM object

Before using a COM object, it must be registered to the system (e.g. it’s CLSID or ProgID must exist in Windows Registry). If it’s not registered, regasm.exe utility can be used to do that:

%systemroom%\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe /verbose /nologo /codebase C:\temp\myCOMObject.dll

Once it’s registered  CoCreateInstance() API method can be used to obtain a reference to an interface exposed by the object and call its methods.

Troubleshooting the jacob error

Under the hood, jacob will do the following:

  1. Obtain the CLSID of the ProgId supplied in the ActiveXComponent constructor
  2. Call CoCreateInstance to get a reference to the object
  3. CoCreateInstance fails and the error is thrown back in Java.

Resolution

  1. Either your object is not registered (use RegAsm.exe to register it)
  2. Jacob attempts to get a reference to a 32 bit COM object from a 64 bit environment, e.g.  Java application running inside a JRE 64 (this was my case)
    1. Unregister the 32 bit version
      • %systemroom%\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe /unregister C:\temp\myCOMObject.dll
    2. register again the object using the 64 bit version of regasm
      • %systemroom%\Microsoft.NET\Framework64\v2.0.50727\RegAsm.exe /verbose /nologo /codebase C:\temp\myCOMObject.dll

Having all this info, hopefully you can fix your applications as well. If you have found a different cause for this error, leave it in the comments and I’ll update the post.

UPDATE: Use OLE /COM Object viewer to manage COM objects registered in your system:

Advertisements


22 Responses to “jacob – Can’t co-create object”

  1. 1 Guan

    Thanks for the post. I didn’t had the problem but the explanation of the interaction of JACOB with COM objects has cleared me lot of things.

  2. 2 Michael

    Good Stuff! I’m having the same problem as you are but when I try and deregister/register my dll using the above command I get this:

    C:\Windows\Microsoft.NET\Framework\v2.0.50727>RegAsm.exe /unregister “C:\Program
    Files (x86)\Common Files\Mercury Interactive\Quality Center\OTAClient.dll”
    Microsoft (R) .NET Framework Assembly Registration Utility 2.0.50727.5420
    Copyright (C) Microsoft Corporation 1998-2004. All rights reserved.

    RegAsm : error RA0000 : Failed to load ‘C:\Program Files (x86)\Common Files\Merc
    ury Interactive\Quality Center\OTAClient.dll’ because it is not a valid .NET ass
    embly

    any suggestions?

    • 3 Michael

      I forgot to mention I did try and run RegAsm.exe from C:\Windows\Microsoft.NET\Framework\v4.0.30319> but no luck either. I then attempted to follow the same logic but first deregistering 32-bit here :

      C:\Windows\SysWOW64>regsvr32.exe /u “C:\Program
      Files (x86)\Common Files\Mercury Interactive\Quality Center\OTAClient.dll”

      then register for 64-bit here

      C:\Windows\System32>regsvr32.exe “C:\Program
      Files (x86)\Common Files\Mercury Interactive\Quality Center\OTAClient.dll”

      It worked, meaning the deregister/register was a success but I still got the Can’t co-create object exception 😦

      • 4 netheadaches

        Hi Michael,

        Two possibilities I can think of:
        1) maybe OTAClient.dll does not exposes any COM functionality (i also use Quality Center – i feel your pain bro – and from a quick glance i couldn’t find any COM exposed by this DLL

        2) maybe the object you are invoking is not the correct one. Use this tool to view all registered objects on your system:
        http://www.microsoft.com/en-us/download/details.aspx?id=7007

        Another thing to try: remove all references from registry to OTAClient.dll and the GUIDs referenced by this file and then register it again

  3. 5 Michael

    Thanks for the fast response! To respond to your 1st comment, it has to expose the COM functionality as it works using a 32-bit JVM. Just that when you switch to 64-bit JVM it complains most likely due to what you mentioned in this article. I will try what you mentioned in step 2 & the last thing you mentioned as well. Thanks a lot too bro! This has been a serious headache!

    • 6 netheadaches

      Ok, in that case manually remove from registry all keys pointing to the DLL file AND it’s GUID. (remove also GUIDs that point to the interfaces exposed by the COM object).

      After the registry is “reset” , register it again using the 64bit regasm.

      If it works, I`ll update the article

      • biasalah tu izat..myampah dgn setengah2 org yg dok bangga bli kete mahal, bli iphone4, fancy gadget n bla2…pny la blagak. tp bli myk hrga myk naek sket..da me&kkaem#8230;pastu rmai2 g petrol pam isi myk pnuh2..mentaliti bdoh je yg wat cmtu…mcm myk tu xkurang2 da.

  4. 8 Michael

    OK..For the record and documentation purposes I did the following:

    1. Removed all registry keys that pointed to OTAClient.dll
    2. Removed all GUIDs that pointed to interface. For the record they were:

    TDApiOle80.TDConnection.1
    TDApiOle80.TDConnection <——————-The one I'm trying to reference
    TDApiOle80.WarningInfo.1
    TDApiOle80.WarningInfo
    TDApiOle80.CacheMgr.1
    TDApiOle80.CacheMgr
    TDApiOle80.AuditRecordData.1
    TDApiOle80.AuditRecordData
    TDClient80.AmarillusHash.1
    TDClient80.AmarillusHash
    TDApiOle80.List.1
    TDApiOle80.List
    TDClient80.ComFrec.1
    TDClient80.ComFrec
    TDClient.TDUtils.1
    TDClient.TDUtils
    TDClient.Import.1
    TDClient.Import
    TDClient.Export.1
    TDClient.Export

    3. I tred to unregister OTAClient.dll using RegAsm.exe but again I got this exception:

    RegAsm : error RA0000 : Failed to load ‘C:\Program Files (x86)\Common Files\Merc
    ury Interactive\Quality Center\OTAClient.dll’ because it is not a valid .NET ass
    embly

    I don't want to continue as you may know how I can get around this. I did all of the above using regsrv32.exe under SysWOW64 (for unregister) and regsrv32.exe under System32 (for register) which worked, meaning it stated both were succesful, restarted machine and still got can't create object issue. I'm not too sure if RegAsm.exe and regsrv32.exe do the same thing. Any ideas on how to get it to work w/ RegAsm.exe?

  5. 9 netheadaches

    i did some quick research. OTAClient.dll is a traditional COM object written in C++ and for that you need to use regsvr32. (regsvr and regasm have the same purpose, the difference is that you use one or the other depending on type of COM object you are working with).

    So given the above info, you need to forget about regasm; the error it’s reporting is accurate: OTAClient is not a .NET assembly

  6. 10 netheadaches

    i just remembered: Quality Center does not support 64 bit! you cannot invoke any Quality Center COM object from a 64 bit environment.

    I know this for sure because I tried installing Tasktop QC connector (for Eclipse) in a 64 bit environment and it failed. Tasktop support told me that they don’t support 64bit version for this connector because QC does not have support for it.
    https://tasktop.com/connectors/hp/known-issues

    • 11 Michael

      Ahhhhh…well, ok then. Thank you a lot for the additional research too. Looks like I will have to relate this information to others. So 32-bit JVM it is 😦 . Just to clear thing up for others, it does work using a 64-bit OS w/ a 32-bit JVM but not 64-bit OS w/ a 64-bit JVM.

  7. 12 Mohsin

    @ Michael did you find the solution ?

    Until yesterday I was able to use the jacob lib, today I uninstalled some unrequired .Net softwares on my machine and suddenly it stopped working and throws following error –

    com.jacob.com.ComFailException: Can’t co-create object at com.jacob.com.Dispatch.createInstanceNative(Native Method) at com.jacob.com.Dispatch.(Dispatch.java:101) at com.jacob.activeX.ActiveXComponent.(ActiveXComponent.java:58)

  8. 13 Crystal

    Hi,

    I’m using jacob-1.17-M2-x86.dll in a Windows 32 – bit. I am still getting the ComFailExcaption – Can’t co-create object after I”ve registered my .dll and my jvm is a 32 bit. Do you know what else could cause this error?

  9. 14 Crystal

    Are there specific .NET .dll that I need that Jacob is referencing?

  10. 15 JD

    I’ve run into the same problem when working with a Windows Server 2008 r2 x64 system. I have the 32 bit JRE and 32 bit Com DLL. The odd thing is, we have succesfully installed the same thing on other x64 systems without any problem. To register the DLL, I use a .Net Setup project. The only thing I find different on the problematic Server, is that 64 bit Oracle is running and in the Task Manager a 64 bit java.exe with description “Java(TM) 2 Platform Standard Edition Binary” this is aside from the 32 bit java.exe (Java(TM) Platform SE binary).

    Any thoughts that could lead my to identify and solve this issue? Thanks in advance.

  11. 16 KR

    Hi,

    I would like to know few information on JACOB Library.Is jacob library is used at any retail industry? If used then I would like to know few industries name. Please help me out.

    Thanks in Advance

  12. OS : Windows 7
    DLL : 32 bit dll
    Java : 32 bit

    If I copy dll in jre/bin then it works fine,If I remove it from there it showing Can’t Cocreate object error

    inserted dll path in PATH environment variable

    What can be the issue,do I always needs to copy dll in jre/bin path

    • 18 Deepen Shah

      I am facing similar issue. I have following environment,

      OS: Win 7 64bit
      Java – 32 bit
      DLL – 32 bit

      I have set environment variable ‘LibraryLoader.JACOB_DLL_PATH’ to jcob dll. But when i try to create object i get error ‘Can’t co create object’.

  13. I had the same problem. It worked great on 32 bit JVM and it threw this exception on 64bit. For this see this link: http://www.gfi.com/blog/32bit-object-64bit-environment/ it solved the problem for me


  1. 1 Can’t co-create object | Yambo Answers
  2. 2 Issue calling method in ActiveX Control (.ocx) via Java | Solutions for enthusiast and professional programmers
  3. 3 http://www.voucherslug.co.uk

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: