如何将控件转换为 IOleObject
我想在 .net 控件上调用 GetClientSite。 为此,我尝试将控件(例如 Windows.Forms.Form)转换为返回 null 的 IOleObject 。
我应该怎么做才能获取 IOleObject?
using System;
using System.Runtime.InteropServices;
using System.Security;
using System.Windows.Forms;
namespace Test001
{
public class Form001 : Form
{
public Form001()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.SuspendLayout();
this.Name = "Form001";
this.Text = "Form001";
this.Load += new System.EventHandler(this.Form001_Load);
this.ResumeLayout(false);
}
private void Form001_Load(object sender, EventArgs e)
{
IOleObject obj = (IOleObject) this;
//IOleClientSite site = obj.GetClientSite();
}
}
[ComImport, Guid("00000112-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), SuppressUnmanagedCodeSecurity]
public interface IOleObject
{
[PreserveSig]
int SetClientSite([In, MarshalAs(UnmanagedType.Interface)] IOleClientSite pClientSite);
IOleClientSite GetClientSite();
[PreserveSig]
int SetHostNames([In, MarshalAs(UnmanagedType.LPWStr)] string szContainerApp, [In, MarshalAs(UnmanagedType.LPWStr)] string szContainerObj);
[PreserveSig]
int Close(int dwSaveOption);
[PreserveSig]
int SetMoniker([In, MarshalAs(UnmanagedType.U4)] int dwWhichMoniker, [In, MarshalAs(UnmanagedType.Interface)] object pmk);
[PreserveSig]
int GetMoniker([In, MarshalAs(UnmanagedType.U4)] int dwAssign, [In, MarshalAs(UnmanagedType.U4)] int dwWhichMoniker, [MarshalAs(UnmanagedType.Interface)] out object moniker);
[PreserveSig]
int InitFromData([In, MarshalAs(UnmanagedType.Interface)] IDataObject pDataObject, int fCreation, [In, MarshalAs(UnmanagedType.U4)] int dwReserved);
[PreserveSig]
int GetClipboardData([In, MarshalAs(UnmanagedType.U4)] int dwReserved, out IDataObject data);
[PreserveSig]
int DoVerb(int iVerb, [In] IntPtr lpmsg, [In, MarshalAs(UnmanagedType.Interface)] IOleClientSite pActiveSite, int lindex, IntPtr hwndParent, [In] object lprcPosRect);
[PreserveSig]
int EnumVerbs(out object e);
[PreserveSig]
int OleUpdate();
[PreserveSig]
int IsUpToDate();
[PreserveSig]
int GetUserClassID([In, Out] ref Guid pClsid);
[PreserveSig]
int GetUserType([In, MarshalAs(UnmanagedType.U4)] int dwFormOfType, [MarshalAs(UnmanagedType.LPWStr)] out string userType);
[PreserveSig]
int SetExtent([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect, [In] object pSizel);
[PreserveSig]
int GetExtent([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect, [Out] object pSizel);
[PreserveSig]
int Advise(object pAdvSink, out int cookie);
[PreserveSig]
int Unadvise([In, MarshalAs(UnmanagedType.U4)] int dwConnection);
[PreserveSig]
int EnumAdvise(out object e);
[PreserveSig]
int GetMiscStatus([In, MarshalAs(UnmanagedType.U4)] int dwAspect, out int misc);
[PreserveSig]
int SetColorScheme([In] object pLogpal);
}
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("00000118-0000-0000-C000-000000000046")]
public interface IOleClientSite
{
[PreserveSig]
int SaveObject();
[PreserveSig]
int GetMoniker([In, MarshalAs(UnmanagedType.U4)] int dwAssign, [In, MarshalAs(UnmanagedType.U4)] int dwWhichMoniker, [MarshalAs(UnmanagedType.Interface)] out object moniker);
[PreserveSig]
int GetContainer(out object container);
[PreserveSig]
int ShowObject();
[PreserveSig]
int OnShowWindow(int fShow);
[PreserveSig]
int RequestNewObjectLayout();
}
}
I want to invoke GetClientSite on a .net control. For this purpose I am trying to cast a control (in example Windows.Forms.Form) to IOleObject which returns null.
What should I do to get IOleObject?
using System;
using System.Runtime.InteropServices;
using System.Security;
using System.Windows.Forms;
namespace Test001
{
public class Form001 : Form
{
public Form001()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.SuspendLayout();
this.Name = "Form001";
this.Text = "Form001";
this.Load += new System.EventHandler(this.Form001_Load);
this.ResumeLayout(false);
}
private void Form001_Load(object sender, EventArgs e)
{
IOleObject obj = (IOleObject) this;
//IOleClientSite site = obj.GetClientSite();
}
}
[ComImport, Guid("00000112-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), SuppressUnmanagedCodeSecurity]
public interface IOleObject
{
[PreserveSig]
int SetClientSite([In, MarshalAs(UnmanagedType.Interface)] IOleClientSite pClientSite);
IOleClientSite GetClientSite();
[PreserveSig]
int SetHostNames([In, MarshalAs(UnmanagedType.LPWStr)] string szContainerApp, [In, MarshalAs(UnmanagedType.LPWStr)] string szContainerObj);
[PreserveSig]
int Close(int dwSaveOption);
[PreserveSig]
int SetMoniker([In, MarshalAs(UnmanagedType.U4)] int dwWhichMoniker, [In, MarshalAs(UnmanagedType.Interface)] object pmk);
[PreserveSig]
int GetMoniker([In, MarshalAs(UnmanagedType.U4)] int dwAssign, [In, MarshalAs(UnmanagedType.U4)] int dwWhichMoniker, [MarshalAs(UnmanagedType.Interface)] out object moniker);
[PreserveSig]
int InitFromData([In, MarshalAs(UnmanagedType.Interface)] IDataObject pDataObject, int fCreation, [In, MarshalAs(UnmanagedType.U4)] int dwReserved);
[PreserveSig]
int GetClipboardData([In, MarshalAs(UnmanagedType.U4)] int dwReserved, out IDataObject data);
[PreserveSig]
int DoVerb(int iVerb, [In] IntPtr lpmsg, [In, MarshalAs(UnmanagedType.Interface)] IOleClientSite pActiveSite, int lindex, IntPtr hwndParent, [In] object lprcPosRect);
[PreserveSig]
int EnumVerbs(out object e);
[PreserveSig]
int OleUpdate();
[PreserveSig]
int IsUpToDate();
[PreserveSig]
int GetUserClassID([In, Out] ref Guid pClsid);
[PreserveSig]
int GetUserType([In, MarshalAs(UnmanagedType.U4)] int dwFormOfType, [MarshalAs(UnmanagedType.LPWStr)] out string userType);
[PreserveSig]
int SetExtent([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect, [In] object pSizel);
[PreserveSig]
int GetExtent([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect, [Out] object pSizel);
[PreserveSig]
int Advise(object pAdvSink, out int cookie);
[PreserveSig]
int Unadvise([In, MarshalAs(UnmanagedType.U4)] int dwConnection);
[PreserveSig]
int EnumAdvise(out object e);
[PreserveSig]
int GetMiscStatus([In, MarshalAs(UnmanagedType.U4)] int dwAspect, out int misc);
[PreserveSig]
int SetColorScheme([In] object pLogpal);
}
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("00000118-0000-0000-C000-000000000046")]
public interface IOleClientSite
{
[PreserveSig]
int SaveObject();
[PreserveSig]
int GetMoniker([In, MarshalAs(UnmanagedType.U4)] int dwAssign, [In, MarshalAs(UnmanagedType.U4)] int dwWhichMoniker, [MarshalAs(UnmanagedType.Interface)] out object moniker);
[PreserveSig]
int GetContainer(out object container);
[PreserveSig]
int ShowObject();
[PreserveSig]
int OnShowWindow(int fShow);
[PreserveSig]
int RequestNewObjectLayout();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
IOleObject
接口是System.Windows.Forms
程序集中内部UnsafeNativeMethods
类内的嵌套接口。System.Windows.Forms.Control
在内部和显式实现它。创建具有相同名称和 guid 的另一个接口不会使其在托管级别上成为“相同的接口”。
此行
代表托管 .net 转换,与 COM 无关。 此转换仅适用于 winforms 程序集中完全相同的界面,该界面不是公开的。
您可以尝试通过 InterfaceMapping 结构使用反射获取方法(但请注意,这是不推荐的):
现在,首先,您将获得一个包装在 System.Object 中的内部类型,该类型无法转换为您的接口,因为原始接口是内部的,因此只要您打算使用该对象,您就可以从反射中获得更多乐趣。
其次,我已经尝试过,该技术有效,但在您的特定场景中,在 Windows 窗体上调用的此方法会引发异常 -
“顶级 Windows 窗体控件无法公开为 ActiveX 控件。”
。The
IOleObject
interface is a nested interface inside the internalUnsafeNativeMethods
class in theSystem.Windows.Forms
assembly. TheSystem.Windows.Forms.Control
implements it internally, and explicitly.Creating another interface with the same name and guid will not make it "the same interface" on a managed level.
This line
represents managed .net casting, and has nothing to do with COM. This cast will work only with the exact same interface from the winforms assembly, which isn't public.
You could try to use reflection via the InterfaceMapping structure to get the method (but note that this is not recommendable):
Now, first, you get an internal type wrapped in
System.Object
which cannot be cast to your interface since the original interface is internal, so you get to have more fun with reflection as long as you intend to use that object.Second, I've tried it, the technique works, but in your specific scenario this method called on a windows Form throws an exception -
"Top-level Windows Forms control cannot be exposed as an ActiveX control."
.我正在使用这种方法:
例如定义
AntiMoniker
。 目前,不需要详细信息。 只需获取 System.__ComObject 的实例即可。它适用于 .NET Framework 2.0/IE8/WinXP SP3
谢谢
I'm using this method:
Define
AntiMoniker
for example. For now, detail is not required. Just get a instance of System.__ComObject.It'll work on .NET Framework 2.0/IE8/WinXP SP3
Thanks
这是另一种方法,使用
Marshal.CreateAggreatedObject
来访问私有COM接口。Here is another method, using
Marshal.CreateAggregatedObject
to get to the private COM interfaces.