Library Zone Articles
External Articles
Byte Size

Discovery Zone Catalogue
Diary
Links
Bookstore
Interactive Zone Ask the Gurus
Discussion Groups
Newsletters
Feedback
Etc Cartoons
Humour
COMpetition
Advertising
Site Builder ASP Web Ring ASP Web Ring


The Developer's Resource & Community Site
COM XML ASP Java & Misc. NEW: VS.NET
International This Week Forums Author Central Find a Job

Coding a DCOM Server Component from IDL

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.

Next....Coding a DCOM Client


What do you think of this article?

You can also write a review. We will publish the best ones here on this article. Send your review to [email protected].

Mail a question to the author!!

As part of the IDevResource commitment to Open Publishing, all of our authors are available to answer all of your trickiest questions at Author Central. For information about the authors, or to mail a question, visit them at Author Central.

Want to read more articles by this author?

Try these:

Byte size articles:

COM Threading Models By Gopalan Suresh Raj, 070200
Gopalan explains the differences in COM and Win 32 threading models.
Go To Article.

ActiveX & COM By Gopalan Suresh Raj, 270100
Gopalan explains the basics of ActiveX / COM as a truly distributed Object Oriented Architecture.
Go To Article.

Full size articles:

COM Channel:

Java COM Integration - Use Visual J++ to implement COM Objects By Gopalan Suresh Raj.
Go To Article.

Developing an MSMQ Server Application using VJ++ By Gopalan Suresh Raj.
Go To Article.

Developing an MSMQ Client using VJ++ By Gopalan Suresh Raj.
Go To Article.

Coding a DCOM Server Component from IDL By Gopalan Suresh Raj.

Coding a DCOM Client By Gopalan Suresh Raj.

Microsoft Transaction Server By Gopalan Suresh Raj.
Gopalans introductory article on Microsoft Transaction Server intorduces the basics to MTS, and leads in to the example articles included in the series.

Developing a Simple MTS Server Component By Gopalan Suresh Raj.
Part 1 of a two part example.

Developing a Simple MTS Client Application By Gopalan Suresh Raj.
Part 2 of a two part example.

Developing The Bank Account IDL By Gopalan Suresh Raj.
A Three-Tier Architecture for a Bank Checking Account - Developing The Bank Account IDL is part 1 of a 3 part example.

MTS Server Component By Gopalan Suresh Raj.
A Three-Tier Architecture for a Bank Checking Account - MTS Server Component is the second part of this three part example.

MTS Client By Gopalan Suresh Raj.
A Three-Tier Architecture for a Bank Checking Account - MTS Server Component is the third part of this three part example.

Java Channel:

Enterprise Java Beans By Gopalan Suresh Raj.
In this introduction to Enterprise Java Beans, Gopalan covers the bases then goes on to demonstrate how to build server side business object components. This article is the introduction to Gopalan's series of Enterprise JavaBeans articles. (This series of articles is © Copyright 1997-2000 Gopalan Suresh Raj. Reproduced with Permission)

Enterprise Java Beans Series - Components at the Server By Gopalan Suresh Raj.

Enterprise Java Beans Series - EJB Model By Gopalan Suresh Raj.

Enterprise Java Beans Series - EJB Naming Services and JNDI By Gopalan Suresh Raj.

Enterprise Java Beans Series - EJB Transactions and JTS By Gopalan Suresh Raj.

Enterprise Java Beans Series - EJB Lifecycle By Gopalan Suresh Raj.

Enterprise Java Beans Series - EJB Servers By Gopalan Suresh Raj.

Enterprise Java Beans Series - EJB Containers By Gopalan Suresh Raj.

Enterprise Java Beans Series - EJB Components By Gopalan Suresh Raj.

Enterprise Java Beans Series - EJB Session Beans By Gopalan Suresh Raj.

Enterprise Java Beans Series - EJB Entity Beans By Gopalan Suresh Raj.

Enterprise Java Beans Series - Writing an Entity Bean By Gopalan Suresh Raj.
Part 1 of a four part series: A four tier bank account example

Enterprise Java Beans Series - Writing a Session Bean By Gopalan Suresh Raj.
Part 2 of a four part series: A four tier bank account example

Enterprise Java Beans Series - Writing an EJB Client By Gopalan Suresh Raj.
Part 3 of a four part series: A four tier bank account example

Enterprise Java Beans Series - Writing an EJB Servlet Client By Gopalan Suresh Raj.
Part 4 of a four part series: A four tier bank account example

More COM Library articles:

CAtlBitmapButton - ATL/WTL Ownerdraw Superclassed Bitmap Button By Amit Dey.
Amit describes building a simple user interface consisting of a series of bitmap buttons in a dialog.

ATL COM and ADO By Paddy Srinivas.
COM+ Events addresses many shortcomings of Connection points. Paddy Srinivas walks us through the COM+ Events System.

ATL COM and ADO By Amit Dey.
New author Amit Dey (he's looking for a job BTW ;-) ) explains his recent experiences with ATL COM and ADO. He simply explains the fundamentals of ADO and then runs through some ATL code examples in his application.

COM Singletons: A Dangerous Animal By Richard Blewett.
Richard Blewitt simply explains the use of COM Singletons.

ASP COM Objects By Jan Verhoeven.
Creating ASP com objects, or Active Server Objects with Delphi 5 is very easy, once you know how to do it.

SafeArrays - For the Beginner By A. Abdul Azeez.
This article is a primer to Safe Arrays and can be used by any beginner to Safe Arrays.

COM+ Basics - Creating your first COM+ Application By Martin Lapierre.

COM+ - the backbone of Windows DNA By Mahesh Bhide.

Exploring COM Threading and Apartments By Anthony Toivonen.
Anthony Toivonen wants you to figure it out for yourself: he guarantees success in threads and apartments after reading this article.

COM on Linux By Frank Rem.
A description of how to code a DCOM client for Linux without using Microsoft products.

ATL Server By Richard Grimes.
A description of ATL Server.

ATL Internals - Part 2 By Shivesh Viswanathan.
Following on from Shivesh's first article of this two part series, this article covers the details of the internals of ATL.

How to use DDX with WTL By Girish Bharadwaj.

COM Patterns By Tony Toivonen.

COM+ Object Pooling By Jeremiah Talkar.

What are COM Pipes? By Richard Grimes.

Java COM Integration - Use Visual J++ to implement COM Objects By Gopalan Suresh Raj.

Developing an MSMQ Server Application using VJ++ By Gopalan Suresh Raj.

Developing an MSMQ Client using VJ++ By Gopalan Suresh Raj.

What is Async COM? By Richard Grimes.

Coding a DCOM Server Component from IDL By Gopalan Suresh Raj.

Coding a DCOM Client By Gopalan Suresh Raj.

Threads and Apartments By Brad Wilson.
Brad's article gives detailed information about apartments and their relationship to threading and synchronization. The goal is to demystify what is a very important, yet under-documented system in COM.

WTL Architecture By Richard Grimes.
This article covers the basics of the WTL architecture, it describes the types of applications that you can create and how WTL manages threads. The example monitors the debug stream and uses WTL to present this data.

The MTS Series by Gopalan Suresh Raj:

Microsoft Transaction Server By Gopalan Suresh Raj.
Gopalans introductory article on Microsoft Transaction Server introduces the basics to MTS, and leads in to the example articles included in the series.

Developing a Simple MTS Server Component By Gopalan Suresh Raj.
Part 1 of a two part example.

Developing a Simple MTS Client Application By Gopalan Suresh Raj.
Part 2 of a two part example.

Developing The Bank Account IDL By Gopalan Suresh Raj.
A Three-Tier Architecture for a Bank Checking Account - Developing The Bank Account IDL is part 1 of a 3 part example.

MTS Server Component By Gopalan Suresh Raj.
A Three-Tier Architecture for a Bank Checking Account - MTS Server Component is the second part of this three part example.

MTS Client By Gopalan Suresh Raj.
A Three-Tier Architecture for a Bank Checking Account - MTS Server Component is the third part of this three part example.

Other Articles

Active Template Library: Architecture & Internals By Shivesh Viswanathan.
Active Template Library is basically a set of template classes provided by Microsoft for writing COM components. The time to write COM components can be considerably reduced if they are written using the ATL framework. The document here provides theory of what goes inside ATL to implement certain generic interfaces.

Microsoft Transaction Server By Richard Grimes.
Introduces Microsoft Transaction Server

What COM is all about By Richard Grimes.
An introductory article to COM, covering all basics including OLE, Activex and DLL's.

String Binding Moniker By Frank Rem.
This article shows how a SB Moniker resolves a connection with a DCE RP server running on Linux using a VB client.


Author: Gopalan Suresh Raj

Gopalan Suresh Raj is a Software Architect, Developer and an active Author.

He is contributing author to a couple of books "Enterprise JavaComputing-Applications and Architecture"(Cambridge University Press, June '99) and "TheAwesome Power of JavaBeans"(Manning, July'98).

His expertise spans enterprise component architectures and distributed object computing. Visit him at his Web Cornucopia site (https://www.execpc.com/~gopalan)or mail him at [email protected].

Go to Gopalan's pages in Author Central.


Power your site with idr newswire

Contribute to IDR:

To contribute an article to IDR, a click here.

To contact us at IDevResource.com, use our feedback form, or email us.

To comment on the site contact our webmaster.

Promoted by CyberSavvy UK - website promotion experts

All content © Copyright 2000 IDevResource.com, Disclaimer notice

Join the Developers Webring

Visit the IDR Forums

Visit the IDR Bookstore!



Learn C#