locked
Cannot pass a GCHandle across AppDomains using unit testing.

    Question

  • Got a manage C++ class in a class library that uses unmanaged C++ lib. The managed C++ class handles callbacks from the unmanage C++ lib by a delegate "gcroot<MessageDelegate^> m_MessageDelegate". To invoke the delegate I use the call "m_MessageDelegate->Invoke(nMsgId);". This works without any problem when running applications using this class library but when testing the class library using unit testing I got the error "Cannot pass a GCHandle across AppDomains" when I invoke the delegate.
    Friday, October 13, 2006 1:53 PM

Answers

  • Hello, what do you use for /clr when you compile both managed and unmanaged modules? how dow you define your unmanaged function? Do you use __clrcall? You may have a problem described here and you may need to use CrossAppdomainDelegate. If you can send me your repro VS solution I can take a look.

    Thank you,
    Michael Koltachev
    VSTS

    Sunday, October 15, 2006 6:06 PM
  • When running Unit Tests by default we create an App Domain per test assembly. It seems that MC++ has its own app domain for unmanaged code and gcroot does not allow marshaling across app domains. To walk around from command line you can use /noisolation switch to mstest. Can you try it out? With this switch we do not create app domains. But when running from IDE we always create app domains.

    I would expect that you would get same error if you create App Domain in your managed code and then create/run managed component in that app domain.

    Thanks,
    Michael

    Monday, October 16, 2006 3:35 PM

All replies

  • Hello, what do you use for /clr when you compile both managed and unmanaged modules? how dow you define your unmanaged function? Do you use __clrcall? You may have a problem described here and you may need to use CrossAppdomainDelegate. If you can send me your repro VS solution I can take a look.

    Thank you,
    Michael Koltachev
    VSTS

    Sunday, October 15, 2006 6:06 PM
  • I'm using /clr for the manage module and "No Common Language Runtime support" for the unmanaged native c++ lib. I'm not using __clrcall. I don't think CrossAppdomainDelegate is an option here. I've tried diffrent solutions with this and it seems that CossAppdomainDelegate wants a member function. I don't think I'm able to provide this, that's why I'm using a delegate that's mapped to a function in the manage code. I'm working on a VS solution so you can have a look. But I think is strange that everything works perfect when I'm not running my module under unit test.

    Monday, October 16, 2006 3:14 PM
  • When running Unit Tests by default we create an App Domain per test assembly. It seems that MC++ has its own app domain for unmanaged code and gcroot does not allow marshaling across app domains. To walk around from command line you can use /noisolation switch to mstest. Can you try it out? With this switch we do not create app domains. But when running from IDE we always create app domains.

    I would expect that you would get same error if you create App Domain in your managed code and then create/run managed component in that app domain.

    Thanks,
    Michael

    Monday, October 16, 2006 3:35 PM
  • Thanks Michael ! This seems to do the trick.

     

    Tuesday, October 17, 2006 9:47 AM