Solved

ATL, COM and coinitialize

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Eclipse Neon and jdk 1.8.0 11 201
egit plugin on eclipse 8 64
Installshield for Embarcadero EX 10.1 Berlin 4 42
VIDEO DATASOURCE Control Resize issue 2 20
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

810 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