Activation Context lost when working with WinForm?
I have an application that uses some classes from a native COM dll in Isolated Deployment.
Simplified:
In run-time I download the dll with its manifest file to a certain directory without registering it.
I then create an Activation Context pointing to that directory and afterwards create instances of classes from the dll.
Let’s say creating class A and after a while class B.
In this flow all goes well.
The problem started when I changed my application to be a WinForm. When "button 1" is pressed I create the Activation Context as before and then create an instance of class A. This works well and the flow returns to my WinForm. However when "button 2" is pressed, I fail to create class B. I get an exception saying that the class cannot be found!
So it seems like the WinForm is somehow messing my Activation context.
Why is that? What is happening there?
Is there a way around it?
Few notes:
I tried following Activation Context creations using sxstrace.exe, however it logged only my Activation Context creation.
I tried commenting out Application.EnableVisu开发者_如何学CalStyles() but it didn't help.
If I wrap each call to my dll with create and destroy Activation Context it works but naturally I don't want to go there...
The CLR does not guarantee to maintain/preserve the Win32 Activation Context when passing through managed code.
A workaround is to call native code, set the activation context there, do what you need to, and then restore the context. You probably only need it for loading and binding to the object, so once you have an IUnknown you can return that.
Martyn
精彩评论