Creating a Type Library ActiveX DLL with Delphi 10.4.2

Sometimes I’ve found it handy to implement certain functions within an ActiveX DLL to be able to call from my application. This can be helpful when trying to do functionality with the app being in a newer version of Delphi and needing functionality of business logic that can’t be upgraded yet.

Creating DLL

First step is to create a new ActiveX library by clicking File > New > Other > Delphi > Windows > ActiveX Library. This will create a starter DLL and RIDL file. For this example, I’m going to rename Project1 in the RIDL editor to MyThing, which will then change the corresponding _TLB.PAS file to be MyThing_TLB.PAS.

In the project Group, I’m going to rename the DLL Project MyThing as well. I’ll right-click on the DLL project and click SAVE. Delphi will first want to save the RIDL file, so I’ll create a folder called MyThing and rename the RIDL file to MyThing.RIDL and save in the MyThing project folder. It will then want to save the MyThing_TLB.PAS, so make sure it’s in the project folder. Next, Delphi will want to save the DLL project, so I make sure it says MyThing.DPROJ and its in the project folder and click Save.

I then want to save the project group, so I’ll right-click on the project group and click Save Project Group As and then change to MyThingGroup and save it in the project folder.

Creating an Object

The next thing is to add a COM Object to the RIDL. In order to make sure the correct registration for the object happens, click File > New > Other > Delphi > Individual Files > COM Object.

The CoClass Name will be the name of the object. So lets enter Stuff for the CoClass name. Let’s put “This is a test” in the description. For this purpose, we will leave Apartment and Multiple Instance options selected. The interface will be named IStuff after entering in the CoClass name. Mark interface Oleautomation and Include Type Library should be checked. Click OK

The RIDL now will be updated with the object and an implementation file will be created, named Unit1.pas. Right-Click and rename to MyThing_Imp.pas and then Right-Click and select Save and save in project folder.

Creating a Method

Now let’s add a test function to the object. From the RIDL editor, right click on IStuff and select New > Method and rename to Test. Select the Parameters tab for this method and change the return type to BSTR to get a text result. For parameters, edit the first one and enter Value for the name and BSTR for the type and leave the Modifier to [IN]. Click Save All in the project. Click Refresh Implementation in the RIDL Editor. The implementation file should have updated now, adding the new function. If it doesn’t, open the MyThing_Imp file and then refresh the implementation again.

In the TStuff.Test function, lets put some simple code:

function TStuff.Test(const Value: WideString): WideString;
 begin
   result := Value + ' - Goodbye Cruel World';
 end;

Click Save All and then Compile the DLL Project. There should be no errors, hints or warnings.

Making Test App

Now, lets create a test application that will call the DLL. Right-Click on the Project Group and select Add New Project and then select Windows VCL Application. After Delphi creates the new project, right-click on the new project and select Save As. Delphi will want to save the Unit1 first, so rename Unit1 to Main.Pas and save in the MyThing project folder. Next, save the project as MyApp.DPROJ in the project folder.

We now need to add the reference to the Type Library, so right-click on MyApp and click Add and select MyThing_TLB.

Add a button to the form and double-click on the form to bring up the code and the click event.

Let’s change the event code to the following:

uses
   MyThing_TLB; // We need to add to reference the items in the library
 procedure TForm1.Button1Click(Sender: TObject);
 var
   StuffObj: IStuff; // Use the Interface
   ValOut: string; // What will be passed into the method
   ResultBack: string; // The result back from the method
 begin
   StuffObj:= CoStuff.Create; // Create the object by calling the CoInitialize name
   ValOut:= 'Hello World';
   ResultBack:=StuffObj.Test(ValOut);
   ShowMessage(ResultBack);
 end;

Now lets compile and run the test app. There shouldn’t be any errors, warnings or hints. When the test app runs, click the button. You should see the following dialog – “Hello World – Goodbye Cruel World.”.

Wait… Did you get a “Class not registered error”? So did I. What the heck! That’s right. Remember, you’re making a type library and as such, it needs to be registered with Windows using the RegSvr32 utility.

Select the DLL project to activate. Then select Run > ActiveX Server > Register for Current User. This will register the DLL for the current user. Now switch back to MyApp project to activate and run the project and click the button. Now you should correctly get the dialog without error.

Hopefully this helps. I wanted to document the process so I could remember myself. The options and further how to’s with the RIDL and Type Library is beyond the scope of this introduction article.

Leave a Comment

Scroll to Top