Gracefully shutting down a thread in a DLL

Tech-Archive recommends: Speed Up your PC by fixing your registry



XP/Server 2003.

We have a DLL, very old, and being used by hundreds of applications. I'm in
the process of adding a maintenance thread (the first) to that DLL. It has
to be done in a way that won't require me changing hundreds of other
programs. Many of these are customer programs outside of my control.

I wrote the thread and am starting it when the DllMain is called with
DLL_PROCESS_ATTACH. So far so good.

The thread starts, runs, and everything works, until it's time to shut down.

I can't get the thread to gracefully exit. When the host exe exits, Windows
calls DllMain with DLL_PROCESS_DETACH. In that I set a terminate flag for
the thread to see, but thread in the dll doesn't run long enough to see it,
so it never completes, and never cleans up.

In researching this on the web, it turns out that DLL_PROCESS_DETACH isn't
called until after all process threads have already been halted,
ungracefully if necessary. That makes it sound impossible to signal the
thread at that time to have it exit.

So is there any good way to get the thread in a dll to gracefully exit
without modifying the exe's that use the dll?

Thanks for any help.

Bruce.


.



Relevant Pages

  • Re: Gracefully shutting down a thread in a DLL
    ... to have your APIs/functions to express an Initialize/Deinitialize paradigm. ... First of all, creating a thread in DllMain is unsafe, because if your ... We have a DLL, very old, and being used by hundreds of applications. ... So is there any good way to get the thread in a dll to gracefully exit ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Gracefully shutting down a thread in a DLL
    ... old dll to gracefully exit before everything else is shut down. ... I'm just not getting this extra dll and thread thing. ... I'm pursuing hooking ExitProcess to see if that will do ...
    (microsoft.public.win32.programmer.kernel)
  • Re: How to get imagebase after a DLL gets loaded
    ... and William provided a sensible answer. ... assigned-to during DllMain PROCESS_ATTACH." ... The OP asked "how to get imagebase after a DLL gets loaded". ...
    (microsoft.public.win32.programmer.kernel)
  • Re: How to get imagebase after a DLL gets loaded
    ... and William provided a sensible answer. ... assigned-to during DllMain PROCESS_ATTACH." ... The OP asked "how to get imagebase after a DLL gets loaded". ...
    (microsoft.public.win32.programmer.kernel)
  • Re: How to get imagebase after a DLL gets loaded
    ... and William provided a sensible answer. ... assigned-to during DllMain PROCESS_ATTACH." ... The OP asked "how to get imagebase after a DLL gets loaded". ...
    (microsoft.public.win32.programmer.kernel)