Michel Deslierres

AHSDK - Importing the SDK's Type Library

FIX ME UP

What is a Type Library ?

As we shall see, ahscript.dll has only two methods of interest to us. But we need to know the entry points for these functions, their parameters and so on. One can look at the AHSDK's examples to try to do this, but Federico Morales provides the best clue about how to go about it with Delphi on the X10 forum:

federico morales
Newbie


Helpful Post Rating: 0
Posts: 1
Reply #2 on: August 14, 2005, 09:07:01 AM

the right way is: in the menu go to [project] then import type library and choose the component :-)

And a few months later dbaxter adds a bit more information with a warning about the receive method that turns out to be correct:

dbaxter
Newbie


Helpful Post Rating: 0
Posts: 1


Reply #1 on: March 11, 2006, 01:29:31 PM

You need to install it as an ActiveX component. Then it will appear on your tool pallet and you can drop it on your project form. You probably want to make it invisible. Then it's as simple as:
procedure TForm1.DoPower(Address: string; PwrOn: boolean);
var
    s: string;
begin
     If PwrOn then s := Address + ' on' else s := Address + ' off';
     ActiveHome1.SendAction('sendplc', s);
end;

The receive function that shows in the components event list is problematic, however.

If you know how to add ActiveX components by importing a type library then go ahead and import the ActiveHomeScript 1.0 Type Library right now and then go directly to the next page. If you have never done that, then, like me, you'll find the task a bit daunting. So I looked up some information on the Web and finally managed to do it for the first time. Below you will find a step by step procedure to help you. But first, what is a type library?

A type library is a file which contains such information. More precisely, here is Microsoft's definition

Type Library

A type library (.tlb) is a binary file that stores information about a COM or DCOM object's properties and methods in a form that is accessible to other applications at runtime. Using a type library, an application or browser can determine which interfaces an object supports, and invoke an object's interface methods. This can occur even if the object and client applications were written in different programming languages.

Luckily, Delphi can "import" a type library. This consists essentially of reading the information and creating a Delphi unit which will contain a class defining ahscript's only object and its methods.

Below are the steps required to import a type library, first in Delphi2010 and after in Delphi 7. Just a reminder: either the AHSDK or the ActiveHomePro software must be installed. During either of these installations, ahscript.dll is registered (added under the appropriate key in the Windows registry) and that is how Delphi can find the type library.

Importing the ActiveHome Type Library in Delphi 2010

  1. Start Delphi 2010.
  2. Click on Import Component... in the Component menu :
  3. Check Import a Type Library and click on Next:
  4. Find and select the ActiveHomeScript 1.0 Type Library and then click Next:
  5. Choose a Palette Page where the component will be made available (here ActiveX), make sure that the check box Generate Component Wrappers is checked and then click on Next.
  6. Check Install to New Package in the next window and click on Finish:
  7. In the New Package window, add a Package name and a Description. For the package name, I wrote x10Server, Delphi expanded it to C:\Program Files (x86)\Embarcadero\RAD Studio\7.0\OCX\Servers\x10Server.dpk. The description is up to you. Then click Finish.
  8. The unit ActiveHomeScriptLib_TLB is created, saved in the Import folder {userdocs}\RAD Studio\7.0\Imports and opened in Delphi's source editor. An information dialogue is then displayed telling you that all the work has been done for you! Click on OK:

If you scroll through the source, you will find a number of constants and type declarations ending with the definition of a single class TActiveHome = class(TOleControl) and a Register procedure. Pay attention to the warning. We will come back to that later.

Actually Delphi did quite a bit more than create the  ActiveHomeScriptLib_TLB.pas file. It created a dcr (a bitmap to be shown on the component palette)  in the same folder. In addition to the file x10Server.dpk the files x10Server.res and x10Server.dproj were created in the folder C:\Program Files (x86)\Embarcadero\RAD Studio\7.0\OCX\Servers\. And as promised a bpl file was created and installed in Delphi.


If you have Delphi2010 you can now move on to the next page to see how the component can be used. If you have Delphi 7 than you can see below how to import the type library. If you have a different version of Delphi, hopefully these two examples will guide you and you will be able to import the type library.

Importing the ActiveHome Type Library in Delphi 7

Again, Delphi 7 does most of the leg work for us, albeit in a slightly different way.

1. Start Delphi 7

2. Click on Import Type Library in the Project menu as shown below (while I use a French version of Delphi 7, I trust that the image below should tell you where these choices live on the IDE.)

3. Select the ActiveHomeScript 1.0 Type Library (Version 1.0) and then choose a Palette Page where the component will be made available (here ActiveX), make sure that the check box Generate Component Wrappers is checked and then click  on the Install button


4. In the New Package tab, add File (package name) and a Description. For the package name, I wrote x10Server. The description is up to you. Then click OK

 

5. Delphi will then warn you that it is about to build the package. Click on Yes.

6. Delphi now tells you that it has built the package. The unit ActiveHomeScriptLib_TLB.pas and the bitmap ActiveHomeScriptLib_TLB.dcr were created and saved in the Import folder C:\Program Files (x86)\Borland\Delphi7\Imports\. Click on OK.

7. Do not forget to save changes when you close the x10Server.dpk window; click on Yes.

Dcp and bpl files will also be constructed and save in Delphi's Bpl folder:

C:\Program Files (x86)\Borland\Delphi7\Projects\Bpl\x10Server.bpl
C:\Program Files (x86)\Borland\Delphi7\Projects\Bpl\x10Server.dcp

while the package source and associated files will be place in Delphi's lib folder:

C:\Program Files (x86)\Borland\Delphi7\Lib\x10Server.cfg
C:\Program Files (x86)\Borland\Delphi7\Lib\x10Server.dof
C:\Program Files (x86)\Borland\Delphi7\Lib\x10Server.dpk
C:\Program Files (x86)\Borland\Delphi7\Lib\x10Server.res

There is only one difference in the two versions of ActiveHomeScriptLib_TLB.pas generated by Delphi 7 and Delphi 2010. Just before the reserved word interface, there are 5 compiler directives inserted by Delphi 2010, and only 4 by Delphi 7:

{$TYPEDADDRESS OFF} // Unit must be compiled without type-checked pointers.
{$WARN SYMBOL_PLATFORM OFF}
{$WRITEABLECONST ON}
{$VARPROPSETTER ON}
{$ALIGN 4} // <-- not included in Delphi 7 version !!
interface

We are now ready to talk to the CM15A.