从 CLSID 动态加载 ActiveX 控件
我正在解析一些第三方软件“脚本”,它根据其内容生成 GUI 表单,我想在 C# 程序中读取该脚本并生成类似的输出,这是一个示例脚本:
BEGIN SECTION Intro
BACKPICTURE=xxxx.gif
PICTUREPOSN=Center
BEGIN CONTROL CLI
IS RADIO=NO
CLSID={49EBC3A3-727D-11CF-9BB9-080000001311}
POSITION=(24,16,250,45)
QUESTION=@0:232
BEGIN PROPERTY Title
DISPID=2
SETTING=CLI :
TYPE=BSTR
END PROPERTY
BEGIN PROPERTY Arrangement
DISPID=3
SETTING=1
TYPE=I4
END PROPERTY
BEGIN PROPERTY EditBoxLength
DISPID=4
SETTING=3
TYPE=I4
END PROPERTY
我感兴趣的属性是 BEGIN CONTROL
和 BEGIN PROPERTY
,因为它们指示 ActiveX 控件及其属性的开始。
我的问题:如何通过其 CLSID 加载此 ActiveX 控件并设置其属性? Type.GetTypeFromCLSID
似乎是我想要的,当我使用 Activator.CreateInstance(Type)
时它不会抛出任何异常,因此它必须创建一个有效的实例,但是如何是否可以设置属性,然后将该控件“绘制”到 Windows 窗体?
谢谢。
I'm parsing some third-party's software "script" which generates a GUI form based on it's contents and I want to read the script within a C# program and produce a similar output, here's an example script:
BEGIN SECTION Intro
BACKPICTURE=xxxx.gif
PICTUREPOSN=Center
BEGIN CONTROL CLI
IS RADIO=NO
CLSID={49EBC3A3-727D-11CF-9BB9-080000001311}
POSITION=(24,16,250,45)
QUESTION=@0:232
BEGIN PROPERTY Title
DISPID=2
SETTING=CLI :
TYPE=BSTR
END PROPERTY
BEGIN PROPERTY Arrangement
DISPID=3
SETTING=1
TYPE=I4
END PROPERTY
BEGIN PROPERTY EditBoxLength
DISPID=4
SETTING=3
TYPE=I4
END PROPERTY
The properties I'm interested in is BEGIN CONTROL
and BEGIN PROPERTY
as these indicate the start of an ActiveX control and it's properties.
My question: how would I load this ActiveX control by it's CLSID and set its properties? Type.GetTypeFromCLSID
seems to be what I want and it doesn't throw any exceptions when I use Activator.CreateInstance(Type)
so it must be creating a valid instance but how would one set is properties and then "draw" this control to a Windows form?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这在 .NET 中很难。 ActiveX 控件需要一个包装器来给它一个好客的家。该包装器由 AxHost 类实现。不幸的是,您不能在代码中直接使用此类,它的构造函数受到保护。它被设计为由 AxImp.exe 工具使用。该工具自动生成一个派生自 AxHost 的 .NET 类。生成的类可以很容易地用作控件。问题是,在设计表单时需要预先运行该工具。这从来都不是一个真正的问题,除了这里。
您能做的最好的事情就是使用 AxImp 为您可能在该脚本中找到的任何 ActiveX 控件创建包装器。它可能是一个有限的子集。然后让脚本解释器根据 clsid 选择正确的包装器。按照您的预期动态执行此操作需要您创建自己的包装器。然而,AxHost 并不是一个小类,ActiveX 托管非常令人不愉快,需要处理许多细节。
That's difficult in .NET. An ActiveX control requires a wrapper to give it a hospitable home. That wrapper is implemented by the AxHost class. Unfortunately you cannot use this class directly in code, its constructors are protected. It was designed to be used by the AxImp.exe tool. That tool auto-generates a .NET class that derives from AxHost. The resulting class is then readily usable as a control. Problem is, that tool needs to be run up front, while you design your form. That's never a real problem, except here.
The best you could do is create wrappers with AxImp for any of the ActiveX controls you might ever find in that script up front. It is likely to be a limited subset. Then have the script interpreter select the correct wrapper, based on the clsid. Doing it dynamically like you intended requires you to create your own wrapper. AxHost is however not a small class and ActiveX hosting is quite unpleasant with many details to take care of.