All set, type library created!
Homework Assignement
In this homework assignment, we will need to solidify your understanding of how to use the Type Library Editor. Since this is what I want you to be focusing on, we will skip any real Com Object code writing this week.
Homework Project
I want you to take the assignment from week 4 and move the entire Com server into a type library. You can recycle the code all you want, but I want you to build the structure as a TLB. This will require you to fool around with creating properties within the Type Library Editor so you may need to consult whatever books you have available in order to do it. There is no solution to this since you should have already have the source from lesson 4. It shouldnt take you more than an hour or two max.
A Look Into the TLB Abyss
Before we end today's lesson, I want to point out two really cool things. Fortunately, they are both in the same location. Open up the source code to your TLB and scroll to the bottom. There, you will find a function that resembles the one shown below:
//In COMTEST_TLB.PAS
CoMyCOMObject = class
class function Create: IMyCOMObject;
end;
...
class function CoMyCOMObject.Create: IMyCOMObject;
begin
Result := CreateComObject(CLASS_MyCOMObject) as IMyCOMObject;
end;
|
What is so cool about this code you might ask? Well, the first thing you should zoom in on is that Delphi declares this function as a class. When you declare a method that resides within a class as class, you can directly call that method without having to instantiate the class! That is a very strange thought. Why would we want to do this? Well, it is not completely necessary in the case of the TLB, but I agree with them that it is a wiser solution than to just have a free-floating function defined (I.E. function CreateMyComObject : IMyComObject).
This leads us to the second cool thing. You can get an interface to your Com object in a heartbeat. Check out the following code pulled from my Client program.
//In CLIENT Program
procedure TMyForm.ExecuteBtnClick(Sender: TObject);
var
MyComObject : IMyComObject;
begin
MyComObject := CoMyCOMObject.Create;
MyComObject.DisplaySomething('This is a Test!');
end;
|
Download the example code for this lesson.
Have fun!
First page > Type Library: Cons/Pros > 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.