Solved

ATL, COM and coinitialize

Posted on 2001-06-02
3
1,396 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

911 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

19 Experts available now in Live!

Get 1:1 Help Now