none
How to delete unmanaged dll after it was used with DLLImport

    Question

  •  

    Code below works perfectly BEFORE i call some.dll
    if
    (System.IO.File.Exists(@"some.dll"))
        File.Delete(@"some.dll");

    But if I use any function function from this dll (using DLLImport),
    I'm getting Access to the path '<Path>\some.dll' is denied.

    If I rerun the same code, but comment out dll function's call - I'm able to delete it.

    I guess OS knows my process used (loaded?) DLL (I didn't use LoadLibrary!!) - is there any way to "unload" it?

    Wednesday, December 09, 2009 11:41 PM

Answers

  • You might be better off just P/Invoking LoadLibrary and FreeLibrary.

    For sample code, see this article.  Note that, when you use DLLImport, it internally increases the reference count an additional time, so you'll need 2 calls to FreeLibrary in order to unload it.  You should be albe to just free it until FreeLibrary returns a fail code, wait a short time (the OS doesn't unload immediately), then delete your DLL.
    Reed Copsey, Jr. - http://reedcopsey.com
    Thursday, December 10, 2009 7:57 PM
    Moderator

All replies

  • No.  Once you import the DLL, and use it directly like this, it will stay loaded until your process terminates.

    There are two options:

    1) You can use P/Invoke on LoadLibrary/FreeLibrary to load the DLL instead of DLLImport.  THis lets you unload the library.
    2) You can load the library in a separate AppDomain.  If you then unload that AppDomain, you'll unload the DLL as well.
    Reed Copsey, Jr. - http://reedcopsey.com
    Thursday, December 10, 2009 12:32 AM
    Moderator
  • #2 does not work.
    So I did:

    System.AppDomain newDomain = System.AppDomain.CreateDomain("NewApplicationDomain"); 
    
    newDomain.ExecuteAssembly(@"<path>\some.exe"); //some.exe has some function calls which have [DLLImport("someOther.dll")] 
    System.AppDomain.Unload(newDomain); 
     
    if (System.IO.File.Exists(@"someOther.dll")) 
     File.Delete(@"someOther.dll");


     I am getting UnauthorizedAccessException.

    If comment outExecuteAssembly, delete work fine...


    Any suggestions?

    Thank you

     

     

     

    Thursday, December 10, 2009 7:01 PM
  • You might be better off just P/Invoking LoadLibrary and FreeLibrary.

    For sample code, see this article.  Note that, when you use DLLImport, it internally increases the reference count an additional time, so you'll need 2 calls to FreeLibrary in order to unload it.  You should be albe to just free it until FreeLibrary returns a fail code, wait a short time (the OS doesn't unload immediately), then delete your DLL.
    Reed Copsey, Jr. - http://reedcopsey.com
    Thursday, December 10, 2009 7:57 PM
    Moderator
  • Hello

    Regarding the suggestion "1) You can use P/Invoke on LoadLibrary/FreeLibrary to load the DLL instead of DLLImport.  THis lets you unload the library.", you can find a working example in my team's project All-In-One Code Framework. Please check out the sample CSLoadLibrary. If you have any questions or feedback regarding the sample of the entire project, please feel free to tell me.
    Regards,
    Jialiang Ge
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, December 11, 2009 9:49 AM
    Moderator