1

I have a DLL which invokes an application running underneath. This DLL is loaded by several other applications/processes simultaneously. So, Basically Architecture is:

enter image description here

My Problem is if Application(s) using DLL is crashed, I want to execute an exit sequence in my Base Application and Exit it. How can I detect that this DLL is no longer used by any application? Is there any thing like Load Count of DLL which I can keep track of? Another glitch is I may have to monitor this via a C# application but that is a further thing.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Swanand
  • 4,027
  • 10
  • 41
  • 69
  • possible duplicate of [How to check dll's reference count? How to know where the dll was loaded?](http://stackoverflow.com/questions/3553231/how-to-check-dlls-reference-count-how-to-know-where-the-dll-was-loaded) – Adriano Repetti Jul 19 '12 at 07:22
  • If an application crashes OR If I kill it from Task Manager, Does it execute "FreeLibrary"? – Swanand Jul 19 '12 at 07:30
  • 1
    You could host the DLL in the Base Application either and implement your own ref count using shared sections, easy, and often used. – mox Jul 19 '12 at 07:47
  • @mox Thanks Mox... Somewhat Similar implementation of using own ref count helped me to solve the problem. If you can post it as answer with some more details, I can mark it as accepted answer. – Swanand Aug 09 '12 at 12:12
  • @SwanandPurankar: ok thanks, I put this comment as answer. – mox Aug 10 '12 at 05:58

3 Answers3

1

If you can shell an external program to do the check, you can use this: http://technet.microsoft.com/en-us/sysinternals/bb896656

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
1

Your image is not a good model for what really happens in Windows. Every process gets its own copy of the DLL. The code inside the DLL is shared in RAM but not its data. There are ways to share data as well but that's not otherwise common, a memory mapped file is the far more typical approach.

Windows doesn't give cheap way to find out if a DLL is loaded into a process. There is no notification mechanism either. Whatever you do, it has to start with the processes first. That works in C# too, you could use the Process.Modules property.

Just keeping track of the processes you know that load the DLL is probably sufficient, when the process no longer runs then you can safely assume it doesn't have the DLL loaded anymore either. Use the Process.Exited event or use WMI as shown in this answer.

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

You could host the DLL in the Base Application either and implement your own ref count using shared sections, easy, and often used. As already mentioned, one possibility would be to implemented your ref counting inside your DLL entry point and detect DLL_PROCESS_DETACH, DLL_THREAD_DETACH, DLL_PROCESS_DETACH, etc according to you specifications.

mox
  • 6,084
  • 2
  • 23
  • 35