使用 WinForm 时激活上下文丢失?
我有一个应用程序在隔离部署中使用本机 COM dll 中的一些类。
简化:
在运行时,我将 dll 及其清单文件下载到某个特定位置 目录而不注册它。
然后我创建一个指向该目录的激活上下文并 然后从 dll 创建类的实例。
假设创建类 A,稍后创建类 B。
在此流程中,一切顺利。
当我将应用程序更改为 WinForm 时,问题就开始了。 当按下“按钮 1”时,我像以前一样创建激活上下文,然后创建 A 类的实例。 这很有效,流程返回到我的 WinForm。 但是,当按下“按钮 2”时,我无法创建 B 类。 我收到一个异常,说找不到该类!
所以看来 WinForm 在某种程度上弄乱了我的激活上下文。
这是为什么呢?那里发生了什么?
有办法解决吗?
几点说明:
我尝试使用 sxstrace.exe 跟踪激活上下文创建,但它仅记录我的激活上下文创建。
我尝试注释掉 Application.EnableVisualStyles() 但没有帮助。
如果我用创建和销毁激活上下文包装对我的 dll 的每个调用,它可以工作,但我自然不想去那里...
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.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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CLR 不保证在传递托管代码时维护/保留 Win32 激活上下文。
解决方法是调用本机代码,在那里设置激活上下文,执行您需要的操作,然后恢复上下文。您可能只需要它来加载和绑定到对象,因此一旦您拥有 IUnknown,您就可以返回它。
马丁
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