更改用 C# 编写的 Active-X 插件的用户类型名称
我目前的任务是为我们的一位客户创建 Active-X 插件。现在我已经在 C#/.NET 中成功创建了 Active-X 插件(一个继承自 System.Windows.Forms.UserControl 类的 Windows 窗体控件),但是托管该插件的应用程序-in 在显示 Active-X 插件的对话框窗口的标题中显示控件的类名称。
经过大量搜索和反汇编,我发现方法 IOleObject.GetUserType
是由主机调用的,并且主机使用该方法的返回值作为标题对话窗口。查看 System.Windows.Forms.UserControl 类,我发现该类继承自 System.Windows.Forms.Control 类,而该类又显式实现了 System.Windows.Forms.UnsafeNativeMethods.IOleObject 接口。
我想知道是否有办法重写 GetUserType
方法 在 UserControl
类中或者是否有另一种方法可以完成我想要的(也许解决方案非常简单,但到目前为止我还没有看到它)。到目前为止,我已经尝试了各种“可能”的解决方案:
我尝试重新实现
IOleObject
接口,但自从System.Windows.Forms.UnsafeNativeMethods.IOleObject
是内部的,不可能执行此操作(您必须使用完全相同的接口,并且重新定义接口不会产生完全相同的接口)。我尝试使用 Ziad Elmalki 在 CodeProject.
我尝试过使用某种形式的 AOP。由于
Control
类通过System.ComponentModel.Component
继承自System.MarshalByRefObject
我认为也许可以让我的用户控件返回一些一种代理,可以拦截发送到GetUserType
方法的调用。
不幸的是我没能让它发挥作用。有效的是更改类的名称,但由于类名称不允许有空格或其他特殊字符,这不是一个可接受的解决方案(下划线不一样)。
为了详细说明,这里有一个我想要完成的代码示例(请注意,它并不完整):
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;
[ ComVisible(true)
, ClassInterface(ClassInterfaceType.AutoDual)
, Description("My Active-X plug-in")
, Guid("...")
]
public partial class MyControl : UserControl
{
public override int GetUserType(int dwFromOfType, out string userType)
{
userType = "The caption to show in the host";
// Return S_OK
return 0;
}
}
希望这里有人可以帮助我。
提前致谢!
I'm currently appointed the task of creating an Active-X plug-in for one of our clients. Now I've successfully created Active-X plug-in in C#/.NET (a Windows Form control which inherits from the System.Windows.Forms.UserControl
class), however the application that hosts the plug-in shows the class name of the control in the caption of the dialog window that displays the Active-X plug-in.
After a lot of searching and disassembling I've found that the method IOleObject.GetUserType
is called by the host and that it is the return value of this method that is used by the host as the caption of the dialog window. Looking at the System.Windows.Forms.UserControl
class I found that this class inherits from the System.Windows.Forms.Control
class which in turn explicitly implements the System.Windows.Forms.UnsafeNativeMethods.IOleObject
interface.
What I would like to know is if there is someway to override the GetUserType
method
in the UserControl
class or if there is another way to accomplish what I want (maybe the solution is very simple, but I've failed to see it so far). I've already tried various 'possible' solutions so far:
I've tried to re-implement the
IOleObject
interface, but since theSystem.Windows.Forms.UnsafeNativeMethods.IOleObject
is internal it is not possible to do this (you must use the exact same interface, and redefining an interface does not result in the exact same interface).I've tried to use CLR injection as described by Ziad Elmalki on CodeProject.
I've tried to use some form of AOP. Since the
Control
class inherits fromSystem.MarshalByRefObject
throughSystem.ComponentModel.Component
I thought it might be possible to get my user control to return some kind of proxy that would intercept calls send to theGetUserType
method.
Unfortunately I've not been able to get this to work. What does work is changing the name of the class, but since class names are not allowed to have spaces or other special characters this is not an acceptable solution (underscores are just not the same).
To elaborate, here's a code example of what I want to accomplish (note that it's not complete):
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;
[ ComVisible(true)
, ClassInterface(ClassInterfaceType.AutoDual)
, Description("My Active-X plug-in")
, Guid("...")
]
public partial class MyControl : UserControl
{
public override int GetUserType(int dwFromOfType, out string userType)
{
userType = "The caption to show in the host";
// Return S_OK
return 0;
}
}
Hopefully someone here could help me.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试实现 ICustomQueryInterface (.Net 4.0): http ://msdn.microsoft.com/en-us/library/system.runtime.interopservices.icustomqueryinterface.aspx
当系统查询 IOleObject 时,您可以返回您自己的实现 IOleObject 的自定义对象,将您不关心的方法转发给 UserControl 实现,并将自定义对象上的 QueryInterface 调用正确委托回 UserControl(聚合)。
You could attempt to implement ICustomQueryInterface (.Net 4.0): http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.icustomqueryinterface.aspx
When the system queries for IOleObject, you could return your own custom object that implements IOleObject, forwards the methods you don't care about to the UserControl implementation, and properly delegate QueryInterface calls on the custom object back to the UserControl (aggregation).