Solved

ATL, COM and coinitialize

Posted on 2001-06-02
3
1,386 Views
Last Modified: 2013-12-14
I'm pretty new to ATL COM programming so bare with my ignorance, please...

I've written an ATL COM .Dll that populates a recordset and returns it to a VB caller.  Everything works great.  However, I'm not sure where I should (properly) call CoInitialize and ::CoUninitialize().

At first I called them in the beginning (and end) of each method but that seemed redundant.  

So I started to call CoInitialize in InitInstance() and subsequently, ::CoUninitialize() in ExitInstance().

Will this be ok?  Any memory leak problems with this style?  Any problem with multiple instances? It seems to work but I need to be sure that this is solid programming!

Please advise and THANKS IN ADVANCE!
0
Comment
Question by:khampton
3 Comments
 
LVL 5

Expert Comment

by:djbusychild
ID: 6150074
you only need to call CoInitialize once per thread and you'll need a matching pair of CoUninitialize in that thread.. If you call CoInitialize multiple times it'll simply succeed ( unless you try to change the threading ).. you'll get S_FALSE returned for those calls, though.. you DO need to match those CoInitialize calls with CoUninitialize if you do call it.

You're not caling this in DllMain, though, right?
0
 
LVL 1

Author Comment

by:khampton
ID: 6150433
Appearently, we read the same MS-KnowledgeBase material.  I do not have an explicit DllMain routine (I guess that's because this is an ATL ActiveX component).  

Because I'm new to this stuff, and because I've not seen any examples of programs that do the CoInitialize in the InitExistance routine, I wanted verification that this was ok.  In fact, I want to know how (where) the experts do it.

Thanks.
0
 

Accepted Solution

by:
robertmitchell_99 earned 50 total points
ID: 6152487
Hi,

From your question it appears that youre asking where in your ATL code you need to call CoInitialize()and CoUninitialize()....?

If that is your question theres no need.  Check out the implementation, in atlbase.h and youll see that the COM initialization methods are called in CComApartment which is used by CComModule to handle the COM threading.

ATL does it for you.

If your question is where in a non-com client module you need to call them then the following code is what i use:

In InitInstance() or as early in a thread as possible
     if(FAILED(CoInitialize(NULL)) )
     // do some error handling

In ExitInstance() or as late in the thread as possible
     CoUninitialize();


best of luck,

Robert.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

746 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

12 Experts available now in Live!

Get 1:1 Help Now