gpt4 book ai didi

c# - 更改用C#编写的Active-X插件的用户类型名称

转载 作者:行者123 更新时间:2023-11-30 13:05:05 24 4
gpt4 key购买 nike

我目前被任命为我们的一位客户创建 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 接口(interface)。

我想知道是否有办法重写 GetUserType 方法在 UserControl 类中,或者如果有另一种方法可以完成我想要的(也许解决方案非常简单,但到目前为止我还没有看到)。到目前为止,我已经尝试过各种“可能”的解决方案:

  1. 我已尝试重新实现 IOleObject 接口(interface),但由于 System.Windows.Forms.UnsafeNativeMethods.IOleObject 是内部的,因此不可能为此(您必须使用完全相同的接口(interface),并且重新定义接口(interface)不会导致完全相同的接口(interface))。

  2. 我已尝试使用 Ziad Elmalki 在 CodeProject 上描述的 CLR 注入(inject).

  3. 我尝试过使用某种形式的 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;
}
}

希望这里有人能帮助我。

提前致谢!

最佳答案

您可以尝试实现 ICustomQueryInterface (.Net 4.0):http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.icustomqueryinterface.aspx

当系统查询 IOleObject 时,您可以返回自己实现 IOleObject 的自定义对象,将您不关心的方法转发给 UserControl 实现,并适本地将自定义对象上的 QueryInterface 调用委托(delegate)回 UserControl(聚合).

关于c# - 更改用C#编写的Active-X插件的用户类型名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6276584/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com