Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ATL, COM and coinitialize

Posted on 2001-06-02
3
Medium Priority
?
1,477 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 200 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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 …
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

604 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