1. Home
  2. Computing & Technology
  3. Delphi Programming
An Introduction to COM Programming with Delphi (6 / 6)
Page 2: 5 steps to a Type Library
 More of this Feature
• Page 1: Type Library: Cons/Pros
• Page 3: A Look Into the TLB Abyss
 More Delphi COM Lessons
• TOC
• Lesson 1
• Lesson 2
• Lesson 3
• Lesson 4
• Lesson 5
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
Discuss!
 Related Resources
• COM / OLE / ActiveX programming with Delphi

How to make your first Type Library

Step 1
You should already be familiar with this step. We need to create an ActiveX Library and add a COM Object to it. There is a small difference however. This time, you will be checking the Include Type Library check box. Type in the information shown in Example 1.0. Click the OK button when finished. (Screen shot provided below)

Step 2
Immediately after you have clicked the OK button, the Type Library Editor will appear on your screen. Before you do anything in the editor, drop back to the source code level and do the following:

  1. Save your project as: ComTest
  2. Save the unit containing your COM Object as: ComObjectUnit
  3. Return to the Type Library Editor.

The Type Library Editor should now resemble the screen shot shown in example 2.0

Creating a Type Library using Delphi: step 1

Creating a Type Library using Delphi: step 2

Notice in the upper left corner of the example that the name of the Type Library has been changed to ComTest.TLB. This name change will mimic the name of project when you saved it. You can change the name of the Type Library by entering a different one in the Name: entry shown in the Attributes tab.

I want you to now look at the Help String entry. It contains the text Project1 Library. This little piece of text holds a lot of importance. Later on, when we register this Type Library into the Windows Registry, we will be able to go to Project | Import Type Library… and find the string you input into this entry. Essentially, the name you put in this entry should describe in a very technical sense what your COM Server does. For fun, go ahead and open up Project | Import Type Library… and take a look at some of the names people gave their Com servers. This should give you an idea of how to describe yours.

Notice that there are two sub-categories under ComTest. IMyCOMObject is obviously an interface and MyCOMObject is the name of the COM Object itself. Go ahead and select IMyCOMObject by clicking on it.

Step 3
When you select IMyCOMObject, you will notice that the attributes tab updates itself to reflect the attributes of the interface you have just selected. From here, you will be able to see the name of the interface, it’s GUID, the version, and who the parent is. There are other properties under the Flags tab, but I will not discuss those quite yet.

Creating a Type Library using Delphi: step 3

Step 4
Go ahead and add the three methods shown below. After you have added them, you can add parameters to the methods by selecting the Parameters tab. In here, you can define the type of parameter (I.E. var, out, in (blank), optional, etc), the name of the parameter, it’s type, (I.E. integer, single, HRESULT, etc), and the default value for the parameter.

Creating a Type Library using Delphi: step 4

Step 5
Go ahead and make some parameters for the functions and procedures you have added. You should also jump back to the Attributes page and type in a description for the method in the Help String: entry. After you have added your descriptions for your interface, coclass (I.E. Your COM Object), and methods, you will need to press the Refresh Implementation button and the Register Type Library button I have circled in red in example 5.0.

Creating a Type Library using Delphi: step 5

Refresh Implementation
Every time you click this, the editor will create the xxxxxxx_TLB.Pas file and automatically generate all of the code for you. This is why you never want to change the source yourself.

Register Type Library
This is essentially the same as selecting Run | Register ActiveX Server. This time, however, your type library will be put into the registry. It can be found by opening the registry and using the Type Libraries GUID when you search for it.

Please note that you can also create the following in the Type Library Editor:

  1. Properties
  2. Records
  3. Unions
  4. Enumerations
  5. Modules
  6. DispInterfaces

I highly recommend playing around in the editor to find what you can and cannot do. If you need help on a particular item, you can simply press F1. The help file is okay, but not super great. It seems to be more a reference than a "how to" help file.

Last page > A Look Into the TLB Abyss. Homework Assignement > Page 1, 2, 3

An Introduction to COM Programming with Delphi: Navigator
<< Previous Lesson (5): Marshaling Data. Behold the power of Variant Arrays. Using Variants and Variant Arrays.

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.