none
Pinvoke and LoadLibrary behavior RRS feed

  • Question

  • Hi to all,
    I've read in a thread in this forum that pinvoke calls the LoadLibrary function every time the program calls an unmanaged function of a dll, even if the module has been already loaded into the application domain and even if the same function has been already called before. There's no mention to any use of the FreeLibrary function.
    Normally, without pinvoke, I call LoadLibrary one time only to load the module and, for every function to which I want to access, retrieve the pointer with GetProcAddress and assign a delegate to it . 
    When I need to call a function I just call the delegate and nothing more.
    For this reason I was wondering why pinvoke performs so much calls while delegates, pointers and anything else has already been created and stored in memory the first time.

    Thanks
    Thursday, May 3, 2012 12:47 AM

Answers

  • Hello mark_555,

    1. LoadLibrary()/FreeLibrary() works on reference counts.

    2. If a DLL has already been loaded into the memory space of a managed app, LoadLibrary() will simply increment the reference count of the DLL.

    3. FreeLibrary() will decrement this ref count but not necessarily unload the DLL. The ref count must reach zero before this happens.

    4. What likely happens when a DLL function is called is that the CLR will use LoadLibrary() to load the DLL. Thereafter, the HMODULE which is returned is cached and LoadLibrary() is not called again as long as this HMODULE value remains valid.

    5. At the end of the managed application, the CLR calls FreeLibrary() to free the DLL. Because of the use of reference counting, the DLL may remain in memory if the C# application code itself has called LoadLibrary(). The onus is then on the C# app to balance the reference count by calling FreeLibrary().

    - Bio.


    Please visit my blog : http://limbioliong.wordpress.com/

    • Proposed as answer by Mike FengModerator Thursday, May 3, 2012 12:11 PM
    • Marked as answer by mark_555 Thursday, May 3, 2012 2:11 PM
    Thursday, May 3, 2012 3:33 AM
  • Hello mark_555,

    >> Let me see if I understand...supposing I call 10 times "Function1". LoadLibrary will be called 10 times...

    No, LoadLibrary() will be called only one time. Then the returned HMODULE is stored for re-use.

    >> In the others 9 calls, the only thing done from the CLR it's something simple like tracking the number of calls done to "Function1"...

    In the other 9 calls, the CLR will check to see if the HMODULE for the DLL is still valid. If so, LoadLibrary() is not called again. GetProcAddress() is called, using the HMODULE that was saved.

    >> It looks like FreeLibrary is only called one time when the application terminates. If I called Function1 10 times and the reference counter is 10, why the CLR calls FreeLibrary only one time leaving alive the others 9 references?

    There is only one reference count.

    >> I've never seen any call to FreeLibrary in programs that use pinvoke, but after the above I was wondering if I should call FreeLibrary manually everytime an unmanaged function returns ?

    No there is no need to do this.

    - Bio.


    Please visit my blog : http://limbioliong.wordpress.com/

    • Marked as answer by mark_555 Thursday, May 3, 2012 2:11 PM
    Thursday, May 3, 2012 1:58 PM

All replies

  • Hello mark_555,

    1. LoadLibrary()/FreeLibrary() works on reference counts.

    2. If a DLL has already been loaded into the memory space of a managed app, LoadLibrary() will simply increment the reference count of the DLL.

    3. FreeLibrary() will decrement this ref count but not necessarily unload the DLL. The ref count must reach zero before this happens.

    4. What likely happens when a DLL function is called is that the CLR will use LoadLibrary() to load the DLL. Thereafter, the HMODULE which is returned is cached and LoadLibrary() is not called again as long as this HMODULE value remains valid.

    5. At the end of the managed application, the CLR calls FreeLibrary() to free the DLL. Because of the use of reference counting, the DLL may remain in memory if the C# application code itself has called LoadLibrary(). The onus is then on the C# app to balance the reference count by calling FreeLibrary().

    - Bio.


    Please visit my blog : http://limbioliong.wordpress.com/

    • Proposed as answer by Mike FengModerator Thursday, May 3, 2012 12:11 PM
    • Marked as answer by mark_555 Thursday, May 3, 2012 2:11 PM
    Thursday, May 3, 2012 3:33 AM
  • Hi Bio and thanks for your detailed explanation.
    In my application I don't make any use of LoadLibrary()/FreeLibrary() and I leave all the process handled from pinvoke.
    Let me see if I understand...supposing I call 10 times "Function1". LoadLibrary will be called 10 times, but only the first time the CLR really load the library on memory. In the others 9 calls, the only thing done from the CLR it's something simple like tracking the number of calls done to "Function1".
    It looks like FreeLibrary is only called one time when the application terminates. If I called Function1 10 times and the reference counter is 10, why the CLR calls FreeLibrary only one time leaving alive the others 9 references?
    I've never seen any call to FreeLibrary in programs that use pinvoke, but after the above I was wondering if I should call FreeLibrary manually everytime an unmanaged function returns ?

    Thanks
    Thursday, May 3, 2012 12:25 PM
  • Hello mark_555,

    >> Let me see if I understand...supposing I call 10 times "Function1". LoadLibrary will be called 10 times...

    No, LoadLibrary() will be called only one time. Then the returned HMODULE is stored for re-use.

    >> In the others 9 calls, the only thing done from the CLR it's something simple like tracking the number of calls done to "Function1"...

    In the other 9 calls, the CLR will check to see if the HMODULE for the DLL is still valid. If so, LoadLibrary() is not called again. GetProcAddress() is called, using the HMODULE that was saved.

    >> It looks like FreeLibrary is only called one time when the application terminates. If I called Function1 10 times and the reference counter is 10, why the CLR calls FreeLibrary only one time leaving alive the others 9 references?

    There is only one reference count.

    >> I've never seen any call to FreeLibrary in programs that use pinvoke, but after the above I was wondering if I should call FreeLibrary manually everytime an unmanaged function returns ?

    No there is no need to do this.

    - Bio.


    Please visit my blog : http://limbioliong.wordpress.com/

    • Marked as answer by mark_555 Thursday, May 3, 2012 2:11 PM
    Thursday, May 3, 2012 1:58 PM
  • Thanks Bio. Unfortunately the informations I've read in a thread were wrong and this confused me, but now it's all clear. Thanks again
    Thursday, May 3, 2012 2:10 PM