none
Loading the same object type into multiple AppDomains RRS feed

  • Question

  • Problem (simplified to make things clearer):

     

    1. there is one statically-linked static.lib that has a function that increments:

    extern int CallCount = 0;

    int TheFunction()

    {

    void *p = &CallCount;

    printf("Function called");

    return CallCount++;

    }

    2. static.lib is linked into a managed C++/CLI managed.dll that wraps TheFunction method:

    int Managed::CallLibFunc()

    {

    return TheFunction();

    }

    3. Test app has a reference to managed.dll and creates multiple domains that call C++/CLI wrapper:

    static void Main(string[] args)

    {

    Managed c1 = new Managed();

    int val1 = c1.CallLibFunc();

    // value is zero

     

    AppDomain ad = AppDomain.CreateDomain("NewDomain");

    Managed c = ad.CreateInstanceAndUnwrap(a.FullName, typeof(Managed).FullName) as Managed;

    int val2 = c.CallLibFunc();

    // value is one

    }

    Question:

     

    Based on what I have read in Essential .NET  Vol1 The CLR by Don Box, I would expect val2 to be zero since a brand new copy of managed.dll/static.lib is loaded when CreateInstanceAndUnwrap is called. Am I misunderstanding what is happening? Is there a way to load a brand new Managed.dll for a new appdomain?

     

    Thank you very much everyone!

     

    All the best,

     

    Leonid Nikolayev

    Tuesday, May 13, 2008 9:18 PM

Answers

  • > Based on what I have read in Essential .NET  Vol1 The CLR by Don Box, I would expect val2 to be zero since a brand new copy of managed.dll/static.lib is loaded when CreateInstanceAndUnwrap is called.

     

    The AppDomain abstraction applies only to managed DLLs.  For managed DLLs, each AppDomain gets its own copy of static variables.  This is not the case for unmanged DLLs because the Windows OS does not natively understand AppDomains.

     

     

    Tuesday, May 13, 2008 10:55 PM

All replies

  • > Based on what I have read in Essential .NET  Vol1 The CLR by Don Box, I would expect val2 to be zero since a brand new copy of managed.dll/static.lib is loaded when CreateInstanceAndUnwrap is called.

     

    The AppDomain abstraction applies only to managed DLLs.  For managed DLLs, each AppDomain gets its own copy of static variables.  This is not the case for unmanged DLLs because the Windows OS does not natively understand AppDomains.

     

     

    Tuesday, May 13, 2008 10:55 PM
  • thank you BinaryCoder,

    If I were to spawn off a separate process, my code sample would work, or am I incorrect?

    Wednesday, May 14, 2008 2:25 PM