Your full service technology partner! 
-Collapse +Expand
Search Delphi Group:

-Collapse +Expand Delphi To/From
-Collapse +Expand Delphi Store

Prestwood eMagazine

October Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingDelphi for W...Coding Tasks   Print This     
  From the August 2009 Issue of Prestwood eMag
Delphi Coding Tasks:
Working with COM Servers: A Little Delphi
Posted 12 years ago on 5/20/2009
Take Away:

Microsoft's various COM technologies allow developers to write programs that can interoperate with other programs.  This is great stuff!

Not surprisingly, Delphi makes it pretty easy to work with OLE, Active-X, OCX, etc.

But there's a nasty surprise awaiting you when you try to do this in a DLL you're creating, possibly even in a different thread of an executable, too.

Here we expose the problem - and the solution.


A few months ago, we wrote a small, proof of concept executable to test the viability of making a Delphi program push data into another program; a program that acts as a COM server.

The details of the POC are unimportant, as is the identity of the COM server program.  Everything was straightforward, and our executable worked perfectly.

Later we needed to "wrap" this technology into a DLL so that a client could use a development tool other than Delphi to accomplish pretty much the same thing as our POC.

And our DLL blew up gloriously!  What worked flawlessly in a Delphi executable, failed miserably when ported to a DLL.

The error message we received said that CoInitialize had not been called.  This was puzzling on two fronts;

First, our executable ran fine, and there wasn't a single call in it to CoInitialize.

Second, the API for the COM server had no CoInitialize method to call!

After much Googling and exploring Delphi's source code, we found the answer.  In the Delphi ActiveX unit, there are two relevant calls: OLEInitialize and OLEUnitialize.  (In our executable, we hadn't had to use the ActiveX unit at all.)

Adding the ActiveX unit to our DLL's uses clause, then invoking OLEInitialize (before starting the rest of our work with the COM server), took care of the problem.  OLEInitialize performs the necessary CoInitialize.

Why the difference?  I'm not sure, yet.  But my suspicion is that, when you create a Win32 executable with Delphi, Delphi's default executable code includes the necessary calls. But it does not do so in the context of a DLL. It may also fail to do so if you create a multi-threaded executable, and try working with a COM server in a thread other than the main one.  I haven't yet tested to find out.

Many hours would have been saved if Dephi's makers had documented this difference. Maybe they did, and it's too obscure for me to find.

So, if your COM project throws an error that says CoInitialize hasn't been called, add ActiveX.pas to your uses clause, then call OLEInitialize before you begin your use of the COM server.


Share a thought or comment...
Comment 1 of 1

I have ordered an essay with read company a few months ago as I was overwhelmed with other essays and exams coming up. They delivered my work a few days in advance and when I asked if the writer could add something else that I needed, my writer helped me straight away. They even fixed my order on a Sunday and that tells a lot about the customer service that they have. I will definitely recommend this to any of you who is looking for a little help with essays at a stressful time.

Posted 19 months ago
Write a Comment...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.

Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1274A1
Enter key:
Article Contributed By Wes Peterson:

Wes Peterson is a Senior Programmer Analyst with Prestwood IT Solutions where he develops custom Windows software and custom websites using .Net and Delphi. When Wes is not coding for clients, he participates in this online community. Prior to his 10-year love-affair with Delphi, he worked with several other tools and databases. Currently he specializes in VS.Net using C# and VB.Net. To Wes, the .NET revolution is as exciting as the birth of Delphi.

Visit Profile

 KB Article #101997 Counter
Since 5/20/2009
Sales Website: Or visit our legacy sales site:

©1995-2021 Prestwood IT Solutions.   [Security & Privacy]