COM newbie

Posted on 2000-04-18
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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 =:)
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


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 100 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

696 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