Create a coclass Instance

With tlb2java generated Java bindings to COM objects you can create any coclass defined in a Type Library by simply creating a new instance of the Java class with the same name as the coclass. For the sample ComServer there is a coclass named Dual.

Dual dual = new Dual();

However, before you can make any COM calls you will need to make sure that OleInitalize has been called.

COM.OleInitialize();

To get access to the COM.OleInitialize native method you need to load the DLL implementing this JNI method.

NativeLibraryLoader.loadLibrary("com_moesol_bindings");

To get access to the JNI code for Dual you'll need to load its DLL

NativeLibraryLoader.loadLibrary("com_moesol_tests_com_server");

Finally, you may notice that coclasses do not normally have any COM methods or properties. However, tlb2java takes the Visual Basic convention and extends the coclass's default interface so that you can use the coclass in the same way as you could use the default interface. In this case IDual. Here is the complete code from Tlb2JavaTest.

...
import com.moesol.bindings.platform_sdk.component_services.COM;
import com.moesol.bindings.platform_sdk.component_services.COMException;
import com.moesol.tests.com_server.Dual;
...
public class Tlb2JavaTest extends TestCase {
    static {
        NativeLibraryLoader.loadLibrary("com_moesol_bindings");
        NativeLibraryLoader.loadLibrary("com_moesol_tests_com_server");
    }
    public void testSample() {
        COM.OleInitialize();
        try {
            Dual dual = new Dual();
            try {
                useDual(dual);
            } finally {
                dual.Release();
            }
        } finally {
            COM.OleUninitialize();
        }
    }

$Id: create.html 3769 2007-06-08 19:06:43Z hastings $