Improve company productivity with a Business Account.Sign Up


COM newbie

Posted on 2000-04-18
Medium Priority
Last Modified: 2010-04-04
Could someone please give me a step by step example of creating a COM object for Delphi 5?
Question by:fwickes

Expert Comment

ID: 2729799
Wanna learn too :-)

Expert Comment

ID: 2729966
Try reading this. I had to write a document a few months back for beginners and it covers some of this stuff:

Interfaces and COM
To utilise our Delphi code, we need to define a mechanism that allows the outside world to talk to it. This is referred to as an interface. So we just need to write our Delphi code, define the interface and it will all work? Well, not quite. Obviously we can’t create a standalone Delphi application because of the nature of our overall system, which means that we need to create an Automation COM object. The Automation COM object is a little different to the usual standalone Delphi application but when it comes down to the nuts and bolts, it’s still just Delphi code.

Creating our Automation COM object is a simple task, and not that much different to creating a standalone application. As per usual, go to the New… command on the File menu. Instead of the New page, switch to the ActiveX page and select ActiveX Library. Save the project in the normal Delphi way, and bring up the New… dialog again (DON’T close the project first, keep it open). Once again, skip to the ActiveX page but this time select the Automation Object. Set the class name to whatever you want to call it (but obviously sticking to the agreed prefix standards/naming conventions). The Instancing field can be set to one of three values (Multiple Instance, Single Instance, or Internal), and as our Automation COM object is going to be used in a multi-object instance environment, we have the potential for several instances to be running simultaneously – it all means that Multiple Instance is the option to go for. The Threading Model value needs to be set to ‘Apartment’. Why this value? Well it all comes back down to our multi-object instance environment. We could have set the Threading Model to Apartment, Single, or Free, but we need to understand what is actually going on a little before we can explain why.

For each user being logged onto the system we need a unique version of the Automation COM object to be run (to store the user’s data, settings, status etc.). There will also be the side effect of a unique thread being created by Windows (this just happens and allows the multi-tasking effect in Windows to take place).  The Threading Model value defines HOW threading will be handled. ‘Single’ threading results in requests being handled totally sequentially (threading is effectively disabled). If we’ve got several users all needing things, do we really want to make them wait in line? We’ve got a multi-tasking OS so let’s use it (imagine the case where it will take several minutes to handle a particular user – all the other users will have to wait). What about ‘Free’ then? Delphi’s help defines Free as allowing a single COM object to be called by several threads. Well in our case we’ve got several users running simultaneously, but if we have a single Automation COM object then we lose the individuality that we want – we need each user to have his/her own unique instance of the object. Sorry, ‘Free’ just isn’t good enough. Luckily the ‘Apartment’ setting does exactly what we need to do – it makes sure that each instance of the COM object can only be called by one thread AND allows multiple threads and instances of the COM object to exist (and run) in memory simultaneously.

Note: There is also the ‘Both’ setting that allows the system to support both Apartment and Free settings. In our system though that might introduce the problems highlighted in the assessment of ‘Free’ so for safety’s sake stick to ‘Apartment’.

The Type Library Editor should open automatically and it’s here that we define the methods and properties used in the communication between client objects (if the Type Library Editor does NOT open (or you need to access it later), you can open it manually by opening the type library file which Delphi will have created automatically. Looking inside the folder where your project was saved will reveal that Delphi has created a file ending in _TLB – this is the type library file and it can be opened like any other part of the project).

Wait a minute, what’s this Type Library thing? Well, the Type Library is a way to extend the amount of information available to the OS about an object. The OS can retrieve a lot of information just by examining the object’s basic interface but not everything – this is where the Type Library comes into play. By using a Type Library we can increase the available information, therefore informing the OS about EVERYTHING that it needs to know, and in turn getting that information passed onto whatever calls our Automation COM object.

The Type Library Dialog will have a tree view to the left with two objects in it, an interface (the small red table tennis bat) and a the CoClass (which can be ignored). Select the interface element of the tree and either right click and select the ‘New’ menu, or hit one of the speed buttons at the top of the dialog. For now we’ll create a method so select ‘Method’ (or hit the green arrow Method button). Enter the name of your new method and it’s now defined and ready to use (in a basic sense anyway). Like any other routine in Delphi though, we might want to pass parameters in and out and this is no exception. Open the Parameters tab and then create the variables that you need by defining them in the table. Creating a property that can be seen in the outside world is just as easy, select the interface branch of the tree and then either right click, select the ‘New’ menu, and ‘Property’, (or hit the ‘Property’ speed button next to the ‘Method’ speed button). Delphi will define an input and output version of your property but you can delete any that isn’t needed (alternatively you can define exactly the sort of property required by using the drop down list on the New Property speed button). Enter the name of your property and then define the fields that it has in the same way we defined the parameters for the method.

Saving the project again not only saves the project but also forces the properties and methods that we’ve defined to be applied to the code (this can also be achieved by hitting the Refresh Implementation speed button). Without doing this, your Delphi code will lack the definitions for your properties and methods.

Switching to your Pascal code will show that the changes have been applied and you can start writing your actual code.

That covers developing the COM object itself and to actually USE the thing, I use the following:

  vObject : OleVariant;
  vObject := CreateOLEObject(<GUID>);
  IF VarIsEmpty(vObject)
  vObject.<Method name>
  vObject := Unassigned;

Just make sure you add ComServ unit to your uses clause

Hope that helps a bit

The Neil =:)
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.


Author Comment

ID: 2732372
Thank you The Neil :)
Just one more thing i'm not clear on...what is the difference between an Automation Object and a COM object?

Expert Comment

ID: 2733529
An object whose class implements the IDispatch interface (declared in the System unit) is an Automation object.




Accepted Solution

TheNeil earned 400 total points
ID: 2733628
Not much, as dentener says an Automation object includes an IDispatch interface. From a calling point of view I don't think it makes any difference but I've only used Automation objects so maybe I'm talking twaddle

The Neil =;)

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
When you have multiple client accounts to manage, it often feels like there aren’t enough hours in the day. With too many applications to juggle, you can’t focus on your clients, much less your growing to-do list. But that doesn’t have to be the cas…
Did you know PowerShell can save you time with SaaS platforms? Simply leverage RESTfulAPIs to build your own PowerShell modules. These will kill repetitive tickets and tabs, using the command Invoke-RestMethod. Tune into this webinar to learn how…

595 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question