Thread View: borland.public.delphi.com.activex.using
7 messages
7 total messages
Started by Yogi Yang
Mon, 28 Jul 2008 12:56
What CoInitialization?
Author: Yogi Yang
Date: Mon, 28 Jul 2008 12:56
Date: Mon, 28 Jul 2008 12:56
10 lines
1004 bytes
1004 bytes
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
Re: What CoInitialization?
Author: Marc Rohloff [TeamB]
Date: Mon, 28 Jul 2008 13:25
Date: Mon, 28 Jul 2008 13:25
14 lines
1570 bytes
1570 bytes
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
Re: What CoInitialization?
Author: Yogi Yang
Date: Tue, 29 Jul 2008 04:12
Date: Tue, 29 Jul 2008 04:12
32 lines
1943 bytes
1943 bytes
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
Re: What CoInitialization?
Author: Remy Lebeau (TeamB)
Date: Tue, 29 Jul 2008 16:22
Date: Tue, 29 Jul 2008 16:22
49 lines
2608 bytes
2608 bytes
"Yogi Yang" <yogiyang007@gmail.com> wrote in message
news:488e98b2@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
Re: What CoInitialization?
Author: Yogi Yang 007
Date: Wed, 30 Jul 2008 12:35
Date: Wed, 30 Jul 2008 12:35
62 lines
3034 bytes
3034 bytes
Remy Lebeau (TeamB) wrote: > "Yogi Yang" <yogiyang007@gmail.com> wrote in message > news:488e98b2@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 > > 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
Re: What CoInitialization?
Author: Z
Date: Wed, 30 Jul 2008 15:07
Date: Wed, 30 Jul 2008 15:07
39 lines
2316 bytes
2316 bytes
"Yogi Yang" <yogiyang007@gmail.com> a �crit dans le message de news:
488e98b2@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
Re: What CoInitialization?
Author: Yogi Yang
Date: Thu, 31 Jul 2008 04:22
Date: Thu, 31 Jul 2008 04:22
49 lines
2530 bytes
2530 bytes
Z wrote: > "Yogi Yang" <yogiyang007@gmail.com> a �crit dans le message de news: > 488e98b2@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 > > Z, Thanks for your help. Really appreciated. Gambit's hint solved the problem though. Regards, Yogi Yang
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads