News:

MyKidsDiary.in :: Capture your kids magical moment and create your Online Private Diary for your kids

Main Menu

Reference a COM Object

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

Previous topic - Next topic

sukishan

A COM Object component is executable code contained in a dynamic-link library (.dll) or in an executable (.exe) file. Components provide one or more objects and self-contained units of code which perform specific functions within the component. Each object has methods, programmed procedures, and properties, behavioral attributes.

To reference a COM object
1. In Solution Explorer, right-click on References and click Add Reference. 1
2. Click the COM tab to display a list of registered J++ COM objects or other libraries and controls.
3. Select the component, and click OK.
4. From your client .jsl file, add an import (Visual J#) statement that references the library. For example:
import COMSERVERLib.*;
5. Use the new (Visual J#) keyword to instantiate the object. For example:
COMServer_DClass cd = new COMServer_DClass();

This example demonstrates how Visual J# applications can access COM DLLs using the .NET Framework COM interop semantics.

Example
The following example shows how a Visual J# client can communicate with C++ COM dispatcher and interface servers. The client gets an instance of the COM server and binds to the interface of each server. After the bind, the client begins communicating with both servers. Additionally, the client can launch events to COMServer_D, the dispatch server.

Copy Code
//client.jsl
import COMSERVERLib.*;

public class Client {
   
    public Client() {

        // Create instances of the COM classes.
        COMServer_DClass cd = new COMServer_DClass();
        ICOMServer_D cdi = (ICOMServer_D) cd;
       
        COMServer_IClass ci = new COMServer_IClass();
        ICOMServer_I cii = (ICOMServer_I) ci;

        String greeting = "Ping:  COMServer_D";
        System.out.println(greeting);

        // Call a method on a Dispatch interface implemented
        //by COMServer_D CoClass.
        String reply = cdi.ReturnGreeting(greeting);
        if (null == reply)
            System.Console.WriteLine("Error: Unmanaged COM server returned a null string");
        else
            System.Console.WriteLine("  [From Visual J# .NET] " +  reply);
       
        // Call a method on an interface implemented by
        //COMServer_I CoClass.
        greeting = "Ping:  COMServer_I";
        System.out.println(greeting);
        reply = cii.ReturnGreeting(greeting);
        if (null == greeting)
            System.Console.WriteLine("Error: Unmanaged COM server returned a null string");
        else
            System.Console.WriteLine("  [From Visual J# .NET] " +  reply);
       
        //Add an event handler for the 'COMEvent' event
        //of the COMServer_D CoClass.
        _ICOMServer_DEvents_COMEventEventHandler eventHandler = new _ICOMServer_DEvents_COMEventEventHandler(this.COMEvent);
        cd.add_COMEvent(eventHandler);
       
        System.Console.WriteLine("\nCausing COMServer_D to fire an event");
        cdi.FireCOMEvent();
    }
   
    public void COMEvent() {
        System.out.println("  An event was fired by the COM class COMServer_D");
    }
   
    /** @attribute System.STAThread() */
    public static void main(String args[]) throws java.io.IOException {
        new Client();   
       
        System.out.println("\nPress the Return key to continue...");
        System.in.read();
    }
}
//Dispatch ComServer D's code.
#include "stdafx.h"
#include "COMServer.h"
#include "COMServer_D.h"

/////////////////////////////////////////////////////////////////////////////
// CCOMServer_D

STDMETHODIMP CCOMServer_D::InterfaceSupportsErrorInfo(REFIID riid)
{
    static const IID* arr[] =
    {
        &IID_ICOMServer_D
    };
    for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
    {
        if (InlineIsEqualGUID(*arr,riid))
            return S_OK;
    }
    return S_FALSE;
}

STDMETHODIMP CCOMServer_D::ReturnGreeting(BSTR Name, BSTR *Greeting)
{
    if (NULL == Name || NULL == Greeting)
        return E_POINTER;

    wprintf(L"\n[Unmanaged COM Server] Received string \"%s\" which I am returning to the client\n", Name);
    CComBSTR ccb;
    ccb.Attach(Name);
    ccb.Append(" Service is available");
    *Greeting = ccb.Copy();
    ccb.Detach();   
    return S_OK;
}

STDMETHODIMP CCOMServer_D::FireCOMEvent()
{
    Fire_COMEvent();
    return S_OK;
}

STDMETHODIMP CCOMServer_D::UseCallBack()
{
    printf_s("  [UnManaged] Method \"UseCallBack()\" called on the STA object CCOMServer_D");
    return S_OK;
}


//Interface ComServer I's code.
#include "stdafx.h"
#include "COMServer.h"
#include "COMServer_I.h"

/////////////////////////////////////////////////////////////////////////////
// CCOMServer_I

STDMETHODIMP CCOMServer_I::InterfaceSupportsErrorInfo(REFIID riid)
{
    static const IID* arr[] =
    {
        &IID_ICOMServer_I
    };
    for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
    {
        if (InlineIsEqualGUID(*arr,riid))
            return S_OK;
    }
    return S_FALSE;
}

STDMETHODIMP CCOMServer_I::ReturnGreeting(BSTR Name, BSTR *Greeting)
{
    if (NULL == Name || NULL == Greeting)
        return E_POINTER;

    wprintf(L"\n[Unmanaged COM Server] Received string \"%s\" which I am returning to the client\n", Name);
    CComBSTR ccb;
    ccb.Attach(Name);
    ccb.Append(" Service is available");
    *Greeting = ccb.Copy();
    ccb.Detach();
   
    return S_OK;
}

STDMETHODIMP CCOMServer_I::FireCOMEvent2()
{
    Fire_COMEvent2();

    return S_OK;
A good beginning makes a good ending