using ATL object from dll (DllMain)

35 views
Skip to first unread message

Pasi Kokko

unread,
Dec 11, 2003, 8:51:10 AM12/11/03
to
Hello everybody,

I'm new in ATL business, and I'm sorry if I use incorrect terms. I'm facing
a problem with ATL object in dll. I've written both dll and activeX-server,
but I don't have access to source code of the program that is using this
dll.
I've been searching information about how to uninitialize COM library in
dll. I noticed, that you can't call CoInitialize and CoUninitialize them
from DllMain, and I found a way to initialize it. How can I uninitialize it
when dll is no longer used? Only logical way I can think of is to do it in
DllMain in DLL_PROCESS_DETACH.

Can someone help me?

Thank You

pasi k


Kim Gräsman

unread,
Dec 11, 2003, 10:28:27 AM12/11/03
to
Hi Pasi,

> I've been searching information about how to uninitialize COM library in
> dll.

You shouldn't. In order to get anywhere close to your DLL, the client must
already have called CoInitialize, since it generally uses the COM run-time
to activate objects inside it.

The exception is DLLs which are creating threads - those must join an
apartment - but that is generally frowned upon anyway.

So, clients must call CoInitialize and CoUninitialize once per thread.

Hope that helps.

--
Best regards,
Kim Gräsman


Alexander Nickolov

unread,
Dec 11, 2003, 1:44:09 PM12/11/03
to
I think he means a client that is not COM-aware.

To the OP: your client must call initialization and uninitialization
functions exported by your DLL. Depending on the thread
organization within the client and where are you to be called,
you may have to do different things in your DLL, perhaps even
spawining a thread and doing all COM activity from within it...

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnic...@mvps.org
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================

"Kim Gräsman" <k...@mvps.org> wrote in message news:eT9ytt$vDHA...@tk2msftngp13.phx.gbl...

Pasi Kokko

unread,
Dec 15, 2003, 1:55:04 AM12/15/03
to
Hi Alexander

The problem here is, that I can't change client program to call
initialization and uninitialization functions because program is already
done by different company. I had to create fake-dll that uses real dll
through ATL COM server. This is done because only one instance of (real)dll
is allowed, and it is used by three different clients.

"Alexander Nickolov" <agnic...@mvps.org> wrote in message
news:e4KCiaBw...@TK2MSFTNGP09.phx.gbl...

Kim Gräsman

unread,
Dec 15, 2003, 3:50:16 AM12/15/03
to
Hi Pasi,

> The problem here is, that I can't change client program to call
> initialization and uninitialization functions because program is already
> done by different company.

In that case, you should probably spawn a thread as suggested by Alexander.
That way, you can guarantee that your COM object's threading requirements
are met.

Of course, it depends on what the COM object does, and what its threading
model is.

Pasi Kokko

unread,
Dec 15, 2003, 7:03:55 AM12/15/03
to
Hi

Thank you both for answers. Problem is now solved.

pasi k


"Kim Gräsman" <k...@mvps.org> wrote in message

news:ew6v3huw...@TK2MSFTNGP10.phx.gbl...

ajca...@gmail.com

unread,
Jan 23, 2015, 9:44:47 AM1/23/15
to
Hello Pasi,

I do not want to revive an old thread, but I have encountered a situation that is very similar to what you had over here and would like to ask if you can give me an indication to how you solved the problem.

Just to give you a heads-up, I have a DLL that is loaded from applications written by other people (the applications may not be COM-aware), and I need to use a COM object within my DLL. Moreover, I can only release the COM object and uninitialize COM when the DLL is about to be unloaded and, as in the situation you had, I cannot add a function "TerminateDLL" to my DLL.

Best Regards
Alan Caruana

On Monday, December 15, 2003 at 1:10:03 PM UTC+1, Pasi Kokko wrote:
> Hi
>
> Thank you both for answers. Problem is now solved.
>
> pasi k
>
>
> "Kim Gräsman" <> wrote in message
> news:
Reply all
Reply to author
Forward
0 new messages