When a Java class is packaged into a COM DLL, it can be used by any application that supports COM. All public methods defined in your class are exposed through a COM interface.
Note Before you use the following procedure to create a COM DLL, close any projects that you may already have open. (On the File menu, click Close All.)
To create a COM DLL
Note Renaming this file does not rename the associated class in the source code, and vice versa. You must manually change all instances of the old name. (Note that you can create an empty project and then add a class with the Class template. This two-step process allows you to name the class before it is created; however, the Class template does not provide the basic code framework for a COM DLL.)
To view the source code that was generated, double-click Class1.java in Project Explorer. The @com.register
directive specifies a GUID for your class. You can also add the onCOMRegister
method to your class, which is automatically invoked when the DLL is registered; you can use this method to perform additional registration, such as creating any custom registry keys needed by the DLL.
The following procedure shows how to add a constructor and create a method that displays a message box.
To add code to your class
public Class1()
{
}
public void showDialog()
{
com.ms.wfc.ui.MessageBox.show("Hello, World!", "COM");
}
For more information about modifying your code in the development environment, see Editing Code.
When you build your project, a DLL and type library (named ProjectName.dll and ProjectName.tlb, respectively) are automatically created and registered on your computer.
To build the DLL
Once your DLL is registered, it can be used by any application that supports COM. The following procedures show how to import your DLL into another Visual J++ project. This example creates a WFC form that invokes the showDialog
method whenever the form is clicked.
To create a WFC application
To import the DLL
The DLL is imported into your new project as a subfolder containing two .java files: Class1.java and Class1_Dispatch.java. Class1 implements the Class1_Dispatch interface, which exposes the public methods of the Class1 object in the DLL. To access these methods, use a Class1_Dispatch object to instantiate Class1. This is demonstrated in the following procedure.
To modify the WFC form
formClick
. import
statement:import DLLProjectName.*;
where DLLProjectName is the name of the DLL project that you imported.
Class1_Dispatch c;
//TODO
comment, instantiate Class1 through the Class1_Dispatch object: c = new Class1();
formClick
method, invoke the showDialog
method of Class1 through the Class1_Dispatch object:c.showDialog();
To build and run the WFC application
For more information about creating COM objects, see Building and Importing COM Objects. For more information about WFC applications, see Creating a Windows application with WFC.