Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What CoInitialization?

18 views
Skip to first unread message

Yogi Yang

unread,
Jul 28, 2008, 8:56:49 AM7/28/08
to
I have been getting this message when ever I run a software in which I
have used an ActiveX.

What is this CoInitialization?

Can someone enlighten me on this please?

TIA

Yogi Yang

Marc Rohloff [TeamB]

unread,
Jul 28, 2008, 9:25:36 AM7/28/08
to
On Mon, 28 Jul 2008 18:26:49 +0530, Yogi Yang wrote:

> What is this CoInitialization?
>
> Can someone enlighten me on this please?

Initialization of the COM (ActiveX/DCOM) framework. You need to make
sure that CoInitialize* is called on every thread that uses COM. You
also need to make sure that every successful call to CoInitialize* is
matched with a call to CoUninitialize.

--
Marc Rohloff [TeamB]
marc -at- marc rohloff -dot- com

Yogi Yang

unread,
Jul 29, 2008, 12:12:35 AM7/29/08
to
Marc Rohloff [TeamB] wrote:
> On Mon, 28 Jul 2008 18:26:49 +0530, Yogi Yang wrote:
>
>> What is this CoInitialization?
>>
>> Can someone enlighten me on this please?
>
> Initialization of the COM (ActiveX/DCOM) framework. You need to make
> sure that CoInitialize* is called on every thread that uses COM. You
> also need to make sure that every successful call to CoInitialize* is
> matched with a call to CoUninitialize.
>
Marc,

Thanks for the info but this sounds absurd.

I have imported an ActiveX in the Delphi IDE.

Now if I start a new blank project and place this ActiveX on its form
and run it (by pressing F9) it runs and the ActiveX behaves as documented.

But when I use this ActiveX in an existing project I keep getting this
message.

What must be the problem?

Can you give me code snipper as to how to call CoInitialize and
CoUnInitialize?

TIA

Yogi Yang

Remy Lebeau (TeamB)

unread,
Jul 29, 2008, 12:22:13 PM7/29/08
to

"Yogi Yang" <yogiy...@gmail.com> wrote in message
news:488e...@newsgroups.borland.com...

> Thanks for the info but this sounds absurd.

A thread has to be assigned to a COM apartment (whether that be a
single-threaded apartment or a multi-threaded apartment) before it can
access COM objects. The apartment dicates how the thread can access the COM
objects (directly or via proxies). The OS can't detemine ahead of time how
you want to manage your threads. So you have to explitically
initialize/uninitialize your threads in your code instead. Why is that
absurd?

> I have imported an ActiveX in the Delphi IDE.
>
> Now if I start a new blank project and place this ActiveX
> on its form and run it (by pressing F9) it runs and the ActiveX
> behaves as documented.

Because you are using it in the main thread, and the main thread has already
been initialized for you at startup.

> But when I use this ActiveX in an existing project I keep
> getting this message.

Because your existing project is not doing the same things to initialize the
main thread at startup.

> Can you give me code snipper as to how to call CoInitialize and
> CoUnInitialize?

Did you look at the documentation for them yet? They are just function
calls, and not very difficult ones at that, ie:

unit Unit1;

interface
...

implementation
...
initialization
CoInitialize(nil);
finalization
CoUninitialize();


Gambit


Yogi Yang 007

unread,
Jul 30, 2008, 8:35:10 AM7/30/08
to

Gambit,

Thanks for this insight. The reason why I said this is absurd is that
the ActiveX works in a blank project but will work in an existing project.

I think, I got the point.

Regards,

Yogi Yang

Z

unread,
Jul 30, 2008, 11:07:21 AM7/30/08
to

"Yogi Yang" <yogiy...@gmail.com> a écrit dans le message de news:
488e...@newsgroups.borland.com...
> Marc Rohloff [TeamB] wrote:

>> Initialization of the COM (ActiveX/DCOM) framework. You need to make
>> sure that CoInitialize* is called on every thread that uses COM.

> Now if I start a new blank project and place this ActiveX on its form and
> run it (by pressing F9) it runs and the ActiveX behaves as documented.
>
> But when I use this ActiveX in an existing project I keep getting this
> message.
>
> What must be the problem?
>
> Can you give me code snipper as to how to call CoInitialize and
> CoUnInitialize?

In the new blank project, the main process is using (creating, calling, etc)
the COM object. So it works.
In the existing project, a thread is doing it.

So in the thread's Execute procedure, you must enclose your existing code
within a CoInitializeEx call at the beginning, and then a CoUninitialize at
the end, with a "try-finally" as shown:

TYogisThread.Execute()
begin
CoInitializeEx(nil,COINIT_APARTMENTTHREADED);
try
//the rest of your existing code
finally
CoUninitialize;
end;

For more about the COINIT_APARTMENTTHREADED parameter, and other choices,
look up the CoInitialize function in the Win32 Help.

Z


Yogi Yang

unread,
Jul 31, 2008, 12:22:48 AM7/31/08
to
Z,
Thanks for your help. Really appreciated.

Gambit's hint solved the problem though.

Regards,

Yogi Yang

0 new messages