Make your own free website on Tripod.com

Coding A DCOM Server Component from IDL
Gopalan Suresh Raj

Note
To work with any of these samples, you will need the following:
.........................................Microsoft Visual J++ ver 6.0
.........................................guidgen.exe and midl.exe from Microsoft Visual C++ ver 6.0

In Visual J++, the most simplest method to create a DCOM server component is to first write your server code in pure Java and then create a COM wrapper for it. However, creating the component from scratch using the IDL will allow you more flexibility and control in coding the server component. Hence, I am presenting here a stock market server component developed from scratch using IDL.

The Steps involved in developing the DCOM Server component from IDL are

1. Create an empty project
2. Define your interface in IDL
3. Compile your IDL and generate the required typelibs necessary
4. Set the properties for your project
5. Implement the DCOM server component
6. Build and register the DCOM server component

1. Create an empty project
Select New Project and from the File menu, choose the Empty project template in Visual J++. Name the project StockDCOM.

2. Define your interface in IDL
Use
guidgen.exe where necessary to generate new UUIDs.

StockMarketLib.idl
[
  uuid(7371a240-2e51-11d0-b4c1-444553540000),
  version(1.0)
]
library SimpleStocks {
  importlib("stdole32.tlb");
  [
    uuid(BC4C0AB0-5A45-11d2-99C5-00A02414C655),
    dual
  ]
  interface IStockMarket : IDispatch {
    HRESULT get_price([in] BSTR p1, [out, retval] float * rtn);
  }

  [
    uuid(BC4C0AB3-5A45-11d2-99C5-00A02414C655),
  ]
  coclass StockMarket {
    interface IStockMarket;
  };
};

3. Compile the idl file and generate typelibs
Use the
midl.exe that comes with your VC++ to compile the IDL files. If the VC++ environment variables are not loaded use the full path to midl.exe. Notice that stockmarketlib.tlb will now appear under the StockDCOM project

E:\MyProjects\StockDCOM>
E:\MyProjects\StockDCOM>midl StockMarketLib.idl
Microsoft (R) MIDL Compiler Version 5.01.0164
Copyright (c) Microsoft Corp 1991-1997. All rights reserved.
Processing .\StockMarketLib.idl
StockMarketLib.idl
Processing D:\Program Files\Microsoft Visual Studio\VC98\include\oaidl.idl
oaidl.idl
Processing D:\Program Files\Microsoft Visual Studio\VC98\include\objidl.idl
objidl.idl
Processing D:\Program Files\Microsoft Visual Studio\VC98\include\unknwn.idl
unknwn.idl
Processing D:\Program Files\Microsoft Visual Studio\VC98\include\wtypes.idl
wtypes.idl

E:\MyProjects\StockDCOM>

4. Set the properties for your project

1. ....From the Project Explorer | StockDCOM | StockDCOM Properties | ClassPath, uncheck the "Merge all Project-specific ........ClassPaths in solution" check box.
2. ....From the Project Explorer | StockDCOM | StockDCOM Properties | COM Classes check the "Use existing Type Library" ........radio button.
3..... Click the "Select" button.
4..... Click the "Browse" button.
5..... Browse to the newly created
StockMarketLib.tlb file in the project directory.
6..... Select and open the
StockMarketLib.tlb file.
7..... Click the "OK" button to confirm the list of COM Components.
8..... Click "OK" to confirm
StockMarketLib Properties form changes.
9..... Notice that the
StockMarketLib folder is added to the StockDCOM project.
10... Notice that three java source files have been created:
......
StockMarket.java, IStockMarket.java, and IStockMarketDefault.java
11...Rename the generated implementation class, StockMarket.java to StockMarketImpl.java.
12...
StockMarket.class will be a COM wrapper automatically generated during the compile phase.

5. Implement the DCOM Server Component
Edit
StockMarketImpl.java as needed to implement the interface. Notice that when JActiveX created the file it placed skeleton code in place.

StockMarketImpl.java
//
// Auto-generated using JActiveX.EXE 5.00.2918
//   ("D:\Program Files\Microsoft Visual Studio\VJ98\jactivex.exe" /javatlb /c2j
//   /creg /xh /wfc  /w /xi /X:rkc /l "F:\TEMP\jvcD.tmp" /nologo /d
//   "E:\MyProjects\StockDCOM" "E:\MyProjects\StockDCOM\StockMarketLib.tlb")
//
// WARNING: Do not remove the comments that include "@com" directives.
// This source file must be compiled by a @com-aware compiler.
// If you are using the Microsoft Visual J++ compiler, you must use
// version 1.02.3920 or later. Previous versions will not issue an error
// but will not generate COM-enabled class files.
//

package stockmarketlib;

import com.ms.com.*;
import com.ms.com.IUnknown;
import com.ms.com.Variant;

/** @com.register(clsid=BC4C0AB3-5A45-11D2-99C5-00A02414C655,
 * typelib=7371A240-2E51-11D0-B4C1-444553540000,
 * version="1.0") */

public class StockMarketImpl implements IUnknown, com.ms.com.NoAutoScripting,
  stockmarketlib.IStockMarketDefault {
 
  public float get_price(String symbol) {
    float price = 0;

    for( int i = 0; i < symbol.length(); i++ ) {
      price += (int) symbol.charAt(i);
    }

    price /= 5;
    return price;
  }

}

6. Build and register the COM DLL Server component
Project Explorer | StockDCOM | Build
Notice that both
StockMarket.class and StockMarketImpl.class are in the project folder.
StockMarket.class will be "hidden" using the Project Explorer.
StockMarket is the COM callable wrapper generated by the @com.register directive.
StockMarketImpl is the J++ code we defined for the COM object.

click here to go to the
Developing a DCOM Client...
click here to go to
My DCOM HomePage...

Go to the Component Engineering Cornucopia page

This site was developed and is maintained by Gopalan Suresh Raj

This page has been visited times since March 17,1999.

Last Updated : Mar 17, '99

If you have any questions, comments, or problems regarding this site, please write to me I would love to hear from you.


Copyright (c) 1997-99, Gopalan Suresh Raj - All rights reserved. Terms of use.

All products and companies mentioned at this site,are trademarks of their respective owners.