Search code examples
javacomjna

Use JNA to CreateObject of a COM object I have no .class for


I have seen examples like this

MsWordApp comObj = this.factory.createObject(MsWordApp.class);

in other questions here on SO.

My problem is that I need to create an object like it is done in a vbscript example:

Set tdc = CreateObject("TDApiOle80.TDConnection")

In this Scenario the createObject would need to process a String, not a class. How can I translate this vbscript to Java / JNA ?


Solution

  • VBScript's CreateObject is essentially CLSIDFromProgID followed by CoCreateInstance, with error handling.

    VBScript always uses late binding, so you're interested in IDispatch support.

    It seems JNA provides this through COMBindingBaseObject:

    new COMBindingBaseObject("TDApiOle80.TDConnection", false)
    

    Then, use the provided IDispatch to invoke methods and get or set properties.

    Reference.