- Jan 19, 2006
- 1,117
- 1
- 0
any way I can check reference count on a dll i load using afxloadlibrary in vc++? didn't find anything on google.
Originally posted by: programmer
Just curious, but can you explain why you would need the reference count? But in answer to your question, no there is no API to get the ref count. You'll need to add a static member var to your process/class/file (depending on where the load/unload occurs). Just ++ each time you successfully call AfxLoadLibrary and -- each time you call AfxFreeLibrary to keep track of it.
From the Visual C++ Developer Center
"Each process maintains a reference count for each loaded library module. This reference count is incremented each time AfxLoadLibrary is called and is decremented each time AfxFreeLibrary is called. When the reference count reaches zero, the module is unmapped from the address space of the calling process and the handle is no longer valid."
A real problem can occur if you mismanage the calls to Load/Free by Free'ing more than you've Loaded -- in which case the runtime may unload the module. You still might be holding a handle (now invalid) and when you make a call -- kaboom, process is dead (Access Violation).
