Solved

COM newbie

Posted on 2000-04-18
6
210 Views
Last Modified: 2010-04-04
Could someone please give me a step by step example of creating a COM object for Delphi 5?
Thanks.
0
Comment
Question by:fwickes
6 Comments
 

Expert Comment

by:dentener
ID: 2729742
0
 
LVL 2

Expert Comment

by:PeterLarsen
ID: 2729799
Listening..
Wanna learn too :-)
0
 
LVL 5

Expert Comment

by:TheNeil
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:

VAR
  vObject : OleVariant;
BEGIN
  vObject := CreateOLEObject(<GUID>);
  IF VarIsEmpty(vObject)
  THEN
    EXIT;
  ...
  vObject.<Method name>
  ...
  vObject := Unassigned;
END;

Just make sure you add ComServ unit to your uses clause

Hope that helps a bit

The Neil =:)
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:fwickes
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?
0
 

Expert Comment

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


Regards,

Paul

0
 
LVL 5

Accepted Solution

by:
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 =;)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now