Customize DllImport attribute?



  • Does anyone know if it is possible to extend or customize the DllImport attribute?  I have a situation where I occasionally need to unload a library, replace it, and then reload it.  Internally DllImport loads the library (LoadLibrary) when an imported function is first called, and then gets the address of the function (GetProcAddress) when each function is first called.  I can FreeLibrary() and LoadLibrary() just fine, but if the library is loaded in a different address space than when it was first loaded all function calls will result in access violations because DllImport doesn't reload the function addresses.  Currently I do it manually for about 10 functions each in 4 or 5 libraries, but it would be FANTASTIC if I could extend DllImport to do this automatically.  However, I imagine that DllImport must have some special consideration in the CLR to let it do its thing, but I don't know.  I have not been able to find anything about this anywhere, so I have a bad feeling it can't be done.



  • AFAIK it's not possible. I found this when looking to do something similar
    http://www.codeproject.com/csharp/dyninvok.asp

    I never used it as my case only used a single export so I just wrote a C dll to call it and called the C dll from .net.



  • Clickable link (dam edit timeout).
    http://www.codeproject.com/csharp/dyninvok.asp

     


Log in to reply