Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Creating a COM DLL

Visual Studio 6.0

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

  1. On the File menu, click New Project.

  2. On the New tab, expand the Visual J++ Projects folder and click Components. Then select the COM DLL icon.

  3. In the Name box, enter a name for your project.

  4. In the Location box, enter the path where you want to save your project, or click Browse to navigate to the folder.

  5. Click Open. A collapsed view of your project appears in http://msdn.microsoft.com/en-us/library/aa243138(v=vs.60).aspx .

  6. In Project Explorer, expand the project node. A file with the default name of Class1.java has been added to your project.

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.)

Adding Code in the Text Editor

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

  • Inside the class declaration, add the following code:
    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 http://msdn.microsoft.com/en-us/library/aa242915(v=vs.60).aspx .

Building the DLL

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

Importing 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

  1. Close your DLL project by clicking Close All on the File menu.

  2. To create a new project, click New Project on the File menu. On the New tab, expand the Visual J++ Projects folder and click Applications. Then select the Windows Application icon.

  3. In the Name box, enter a name for your project. In the Location box, enter the path where you want to save your project, or click Browse to navigate to the folder.

  4. Click Open. In Project Explorer, expand the project node. A file with the default name of Form1.java has been added to your project.

To import the DLL

  1. In Project Explorer, right-click the name of the new project. Point to Add on the shortcut menu and then click Add COM Wrapper.

  2. The COM Wrappers dialog box lists the type libraries that are registered on your computer. Select the name of the DLL project that you had previously created.

  3. Click OK.

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

  1. In Project Explorer, expand the project node. Double-click Form1.java in Project Explorer to open it in the Forms Designer.

  2. Click the Events toolbar button in the Properties window to display the events for the form. (If the Properties window is not displayed, click Properties Window on the View menu.)

  3. Find the click event and enter formClick for the name of the method that will handle the event. When you press ENTER, the Text editor opens to an empty event handler named formClick.

  4. Go to the beginning of the file and add the following import statement:
    import DLLProjectName.*;

    where DLLProjectName is the name of the DLL project that you imported.

  5. Inside the Form1 class definition, before the constructor, declare a Class1_Dispatch interface object:
    Class1_Dispatch c;
    
  6. In the Form1 constructor after the //TODO comment, instantiate Class1 through the Class1_Dispatch object:
    c = new Class1();
    
  7. Inside the defintion of the formClick method, invoke the showDialog method of Class1 through the Class1_Dispatch object:
    c.showDialog();
    

To build and run the WFC application

  1. On the Build menu, click Build. (If you receive any compilation errors or messages, correct the errors and rebuild your application.)

  2. To run the application, click Start on the Debug menu.

  3. Click the form. A message box appears that displays "Hello, World!"

  4. To close the application, click the Windows Close button located in the upper-right corner of the form.

For more information about creating COM objects, see http://msdn.microsoft.com/en-us/library/aa266312(v=vs.60).aspx . For more information about WFC applications, see Creating a Windows application with WFC.

Show:
© 2014 Microsoft. All rights reserved.