News:

GinGly.com - Used by 85,000 Members - SMS Backed up 7,35,000 - Contacts Stored  28,850 !!

Main Menu

Generate a COM Component in J#

Started by sukishan, Jul 14, 2009, 03:54 PM

Previous topic - Next topic

sukishan

How to: Generate a COM Component in J#

COM components provide functionality that you can use in your code to perform specialized tasks. COM components are the key to building powerful, real-world applications. The following example shows you how to create a COM component and also how a client can use the component.

To generate a COM Component
On the File menu, point to New, click Project, and then click OK.

In the New Project dialog box, click Class Library, and then in the Name text box, type IClock.

Click OK.

Right-click Solution Explorer, point to Add, and then click Component Class.

In the Name box, type Clock, and click OK.

Replace the contents of the Clock.jsl, with the following code:

package IClock;

import System.ComponentModel.*;
import java.util.Date;

public class Clock extends System.ComponentModel.Component
{
    private System.ComponentModel.IContainer components;
    private long startTime;
    private long stopTime;
    private long elapsedTime;

    public Clock()
    {
        components = new Container();

        //Add Clock Client to the component container.
        components.Add(this);
        InitializeComponent();
    }
    public Clock(System.ComponentModel.IContainer container)
    {
        container.Add(this);
        InitializeComponent();
    }

    public void Start()
    {
        System.out.println("Starting the timer");
        startTime = (new Date()).getTime();
    }

    public long Stop()
    {
        System.out.println("Halting the timer");
        stopTime = (new Date()).getTime();
        elapsedTime = stopTime - startTime;
        Dispose(true);
        return elapsedTime;
    }

    #region Component Designer generated code
    protected void Dispose(boolean disposing)
    {
        //Remove Clock from the container.
        if (disposing)
        {
            if (components != null)
            {
                components.Dispose();
            }
        }
        //Dispose of the container itself.
        super.Dispose(disposing);
    }

    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
    }
    #endregion
}Press F5 to compile the COM Server.

To generate and register a type library
Open a command line and type, tlbexp Clock.dll.

Register the assembly to run locally by typing, regasm Clock.dll

-or-

Register the assembly to run in the Global Assembly Cache (GAC) by typing, gacutil /i Clock.dll

To build the client code

From Visual Studio, on the File menu, point to New, and then click New Project.

In the New Project dialog box, select Console Application, and in the Name box, type ClockClient.

In the Solution Name box, select ConsoleApplication1, and type ClockClient. Click OK.

In Solution Explorer, right-click ClockClient, and then click Add Reference.

Click the Browse tab, and add the Clock.dll as COM server.

In the Clock.jsl file, replace the existing code with the following code:


package ClockClient;

import System.*;
import IClock.*;

public class Program
{
    public static void main(String[] args)
    {
        Clock myclock = new Clock();
        myclock.Start();
        for (int idx = 0; idx < 5000; idx++)
        {
            //Generate some clock cycles.
            ;
        }
        long sysClock = myclock.Stop();
        System.out.println("Time elapsed since you added and removed as a COM component " + sysClock + " milliseconds");
    }
}Press F5 to compile and run your application.

As the application runs, you can set various breakpoints on the COM server to see how myClock moves in and out of the container.

Robust Programming
Each time you add or change methods in the COM component, you must re-export and re-register the assembly using the tlbexp, and regasm or GAC utilities.
A good beginning makes a good ending