Controlling how P/Invoked assemblies are loaded into different app domains

我怕爱的太早我们不能终老 提交于 2019-12-11 10:25:45

问题


I've got an application which needs to call some unmanaged code in a dll. I need to do these calls from multiple app domains and specifically want the assembly to be loaded into memory multiple times (once per app domain).

I've tried doing the following:

    Dim AppDomainSetup As New AppDomainSetup
    With AppDomainSetup
        .PrivateBinPath = "<Blah>"
        .LoaderOptimization = LoaderOptimization.MultiDomainHost
    End With

    Dim AppDomain As AppDomain = AppDomain.CreateDomain(String.Format("AppDomain-{0}", AppDomainCounter), Nothing, AppDomainSetup)
    AppDomainCounter += 1

    Dim Manager = CType(
        AppDomain.
        CreateInstanceAndUnwrap(
            System.Reflection.Assembly.
            GetExecutingAssembly.FullName,
        "<My Manager Class>"), AppDomainManager)
    Return Manager

AppDomainManager inherits from MarshalByRefobject and has a method on it which (eventually) calls

<DllImport("<Path>",
    EntryPoint:="<MethodName>",
    CallingConvention:=CallingConvention.Cdecl)>
Private Shared Function <MethodName>(
                                    <InAttribute(),
                                    MarshalAsAttribute(UnmanagedType.LPStr)>
                                    ByVal sig1 As String,
                                    <InAttribute(),
                                    MarshalAsAttribute(UnmanagedType.LPStr)>
                                    ByVal sig2 As String) As Integer

However, after doing some testing, it seems that a single (instance?) of the assembly is being loaded and shared between app domains. I'd hoped that the AppDomain.LoaderOptimization setting would've forced a unique copy per domain.

Is there any way I can force the CLR to load the assembly multiple times?

来源:https://stackoverflow.com/questions/12053567/controlling-how-p-invoked-assemblies-are-loaded-into-different-app-domains

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!