The Type Library Editor

The type-library editor is the tool you can use to define a type library in Delphi. Figure 20.3 shows its window after I've added some elements to it. The type-library editor allows you to add methods and properties to the OLE Automation server object we've just created. Once this is done, it can generate both the type library (TLB) file and the corresponding Object Pascal source code.

FIGURE 20.3:

The type-library editor, showing the details of an interface

FIGURE 20.3:

The type-library editor, showing the details of an interface

To build a first example, we can add to the server a property and a method. In the editor, we actually add these two elements to the interface, which should be called IFirstServer. Select it, and then click the Method button of the toolbar. (The names of these buttons can be displayed by using the shortcut menu of the toolbar.) Now you have to give it a name, such as ChangeColor. You can type the name either in the Tree View control on the left side of the window or in the Name edit box on the right side. Delphi automatically defines the new method as a function in the Invoke Kind box and (as you'll see on the Parameters page) assigns it an HRESULT return value and no parameters. This corresponds to the Pascal definition: procedure ChangeColor; safecall;

There are two reasons for this difference in the type of method. The first is that in the IDL language used by COM, all methods are indicated as functions (following the C language style); the second is that Delphi handles the HRESULT error codes automatically in every method that uses the safecall calling convention.

The methods contained in OLE Automation interfaces in Delphi generally use the safecall calling convention. This wraps a try/except block around each method and provides a default return value indicating error or success.

Now we can add a property to the interface by clicking the Property button of the typelibrary editor's toolbar. Again, we can type a name for it, such as Value, and select a data type in the Type combo box. Besides selecting one of the many types already listed, you can also enter other types directly, particularly interfaces of other objects. Keep in mind, however, that OLE Automation supports only a subset of Delphi types. In this example, we can select the long type, which corresponds to Delphi's Integer type.

If you look again in the Parameters page for this example (see Figure 20.4), you can see that both the Set and Get (actually called Put and Get in the COM jargon) methods have the HRESULT return value. You can also see that while the Put method uses the property's data type as its parameter (as with Delphi properties), the Get method uses a pointer to the type as its out parameter. This definition corresponds to the following elements of the Pascal interface:

function Get_Value: Integer; safecall; procedure Set_Value(Value: Integer); safecall; property Value: Integer read Get_Value write Set_Value;

FIGURE 20.4:

The Parameters page of the type-library editor

FIGURE 20.4:

The Parameters page of the type-library editor

Clicking the Refresh button on the type-library editor toolbar generates the Pascal version of the interface. We'll examine it shortly, but first I want you to focus on the Text page of the editor, which includes the definition we've just created, written in the IDL language:

interface IFirstServer: IDispatch {

[id(0x00000001)]

HRESULT _stdcall ChangeColor( void ); [propget, id(0x00000002)]

HRESULT _stdcall Value([out, retval] long * Value );

[propput, id(0x00000002)]

HRESULT _stdcall Value([in] long Value );

Fortunately, Delphi's type-library editor saves you from writing similar code by hand, and the Delphi environment options (in the Type Library page) include a radio button to select Pascal or IDL in the text displayed by the type-library editor.

Was this article helpful?

+1 0
Self Publishing a Book The Easy Way

Self Publishing a Book The Easy Way

This comprehensive guide will present you with a variety of self-publishing options and explore their viability. Well take a look at all types of books and book-delivery systems.

Get My Free Ebook


Post a comment