Can C-style DLLs be isolated inside a .NET AppDomain? RRS feed

  • Question

  • Do .NET AppDomains only isolate .NET assemblies; that is, do C-style DLLs always get shared across AppDomains?

    I have a C# framework that uses Nuget package HDF.PInvoke to create a simpler API for reading/writing .h5 files. Under the hood, that HDF.PInvoke wrapper calls into C-style HDF5.DLL. All framework interaction with HDF.PInvoke is done in one class in its own assembly, HDFWrapper. HDFWrapper is loaded in a separate AppDomain and AppDomain.CurrentDomain.GetAssemblies() verifies that HDFWrapper is not loaded into the primary AppDomain.

    However, an app built on this framework that has its own direct dependencies on a different version of HDF5.dll crashes when it tries to access that DLL. Suspecting version conflict, I temporarily replaced the framework's copy of HDF5.dll with the one needed by the app and voila the app works (but the framework's use of HDF.PInvoke breaks).

    The framework loads its copy of HDF5.dll before the app does. Unfortunately, I can't tell what the visibility of that module is from the list returned by ProcessModuleCollection processModuleCollection = Process.GetCurrentProcess().Modules; since it doesn't indicate anything about AppDomains. And C-style DLLs aren't included in the list returned by Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

    For performance reasons, I can't move HDFWrapper into its own process.

    Friday, March 13, 2020 5:57 PM


  • native code doesn't know and doesn't care about AppDomains.
    So there is only one instance of the native dll loaded, and when it contains static objects: they are only initialized once.

    • Marked as answer by JimC66 Friday, March 13, 2020 6:11 PM
    Friday, March 13, 2020 6:08 PM