在C#中动态加载非托管OCX(Dynamically loading unmanaged OCX in C#)

编程入门 行业动态 更新时间:2024-10-27 00:27:32
在C#中动态加载非托管OCX(Dynamically loading unmanaged OCX in C#)

我在VC ++中创建了ocx如果它是静态加载它可以正常工作但是当我从程序集动态加载它时它会给出异常。

public Assembly assembly; public Form1() { InitializeComponent(); assembly = Assembly.LoadFile(Application.StartupPath + "\\abc.ocx"); }

它给出了以下异常该模块应该包含一个程序集清单。

(HRESULT异常:0x80131018)

I have ocx created in VC++ If it is loaded statically it works fine but when I load it dynamically from assembly it gives exception .

public Assembly assembly; public Form1() { InitializeComponent(); assembly = Assembly.LoadFile(Application.StartupPath + "\\abc.ocx"); }

it gives following exception The module was expected to contain an assembly manifest.

(Exception from HRESULT: 0x80131018)

最满意答案

您必须执行许多步骤,这些步骤通常在您使用工具箱时自动执行。 首先,您必须运行Aximp.exe实用程序来生成.NET互操作程序集。 使用Visual Studio命令提示符运行该工具。 你会得到两个程序集,axinterop.foo.dll包含一个从AxHost派生的包装类,并允许你将控件放在一个表单上。 和interop.foo.dll,互操作程序集,它使控件实现的COM接口可以从.NET程序调用。

接下来,您必须确保这些DLL存在于构建目录中。 最好的办法是将它们添加到项目中,并将其Copy to Output Directory设置为“Copy if newer”。

现在,您可以在代码中使用Assembly.Load(“axinterop.foo.dll”)来动态加载互操作程序集。

接下来,您必须创建控件的实例,使用Assembly.CreateInstance()并传递AxHost包装类的类型名称。 如果你不知道它的名字是什么,那就不明显了,然后使用ildasm.exe来查看axinterop.foo.dll程序集。 将返回的对象强制转换为AxHost。

接下来,您必须将控件添加到窗体的Controls集合中,以使其可见且可用。 在创建控件实例之前,您无法调用任何接口方法,直到您添加控件并且表单的Load事件被触发后才会发生。

接下来,您需要使用reflection或dynamic关键字来获取对控件实现的接口的引用,以防您需要设置属性或调用方法。 这很难做到,你首先需要使用工具箱中添加的控件来编写代码,这样你就不必过于苛刻地使用正确的名称。


这显然都非常痛苦,很难做到。 通过向直接使用OCX的项目添加类库并通过接口公开其重要属性来提高获得良好结果的几率。 并在那个上使用Assembly.Load()。

You'll have to perform a number of steps that are normally taken of automatically when you use the toolbox. First and foremost, you have to run the Aximp.exe utility to generate the .NET interop assemblies. Use the Visual Studio Command Prompt to run the tool. You'll get two assemblies, axinterop.foo.dll contains a wrapper class that's derived from AxHost and allows you to put the control on a form. And interop.foo.dll, the interop assembly that makes the COM interfaces implemented by the control callable from a .NET program.

Next you have to ensure these DLLs are present in the build directory. Best thing to do is to add them to your project and set their Copy to Output Directory to "Copy if newer".

Now you can use Assembly.Load("axinterop.foo.dll") in your code to dynamically load the interop assembly.

Next you have to create an instance of the control, use Assembly.CreateInstance() and pass the type name of the AxHost wrapper class. If you have no idea what its name might be, it isn't obvious, then use ildasm.exe to look at the axinterop.foo.dll assembly. Cast the returned object to AxHost.

Next you have to add the control to the form's Controls collection so it is visible and usable. You cannot call any interface methods until the control instance is created, that doesn't happen until you've added the control and the form's Load event has fired.

Next you have to use reflection or the dynamic keyword to obtain a reference to the interfaces implemented by the control, in case you need to set properties or call methods. That's difficult to get right, you'll want to write that code first with the control added from the toolbox so you don't have to guess too hard at the proper names.


This is obviously all quite painful and hard to get right. Improve the odds for a good outcome by adding a class library to your project that uses the OCX directly and exposes its important properties through an interface. And use Assembly.Load() on that one.

更多推荐

本文发布于:2023-08-07 07:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1462060.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   动态   OCX   unmanaged   loading

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!