What is this CoInitialization?
Can someone enlighten me on this please?
TIA
Yogi Yang
> 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
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
> 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
>> 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
Gambit's hint solved the problem though.
Regards,
Yogi Yang