Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activation Context lost when working with WinForm?

I have an application that uses some classes from a native COM dll in Isolated Deployment.

Simplified:

  1. In run-time I download the dll with its manifest file to a certain directory without registering it.

  2. I then create an Activation Context pointing to that directory and afterwards create instances of classes from the dll.

  3. 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.EnableVisualStyles() 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...

like image 997
Erez Cohen Avatar asked Nov 14 '22 04:11

Erez Cohen


1 Answers

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

like image 133
Martyn Lovell Avatar answered Dec 12 '22 00:12

Martyn Lovell