如何将控件转换为IOleObject [英] How to cast a control to IOleObject

查看:246
本文介绍了如何将控件转换为IOleObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在.net控制台上调用 GetClientSite 。为此,我试图将一个控件(例如Windows.Forms.Form)转换为返回null的 IOleObject

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.

做到得到IOleObject?

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();
    }
}


推荐答案

IOleObject 接口是 System.Windows.Forms中的内部 UnsafeNativeMethods 类中的嵌套接口程序集。 System.Windows.Forms.Control 在内部实现显式

The IOleObject interface is a nested interface inside the internal UnsafeNativeMethods class in the System.Windows.Forms assembly. The System.Windows.Forms.Control implements it internally, and explicitly.

创建具有相同名称和guid的另一个界面不会使其在托管级别上成为相同的界面。

Creating another interface with the same name and guid will not make it "the same interface" on a managed level.

此行

IOleObject obj = (IOleObject) this;

表示托管的.net转换,与COM无关。此强制转换只适用于不是公开的winforms程序集中的完全相同界面。

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.

您可以尝试使用反射 InterfaceMapping 结构来获取方法(但请注意这是不推荐的):

You could try to use reflection via the InterfaceMapping structure to get the method (but note that this is not recommendable):

Type thisType = this.GetType();

Type oleInterface = thisType.GetInterface("IOleObject");

MethodInfo getSiteMethod = oleInterface.GetMethod("GetClientSite");

//InterfaceMapping is used to get more complex interface scenarios
InterfaceMapping map = thisType.GetInterfaceMap(oleInterface);

//at which index is the explicit implementation
int index = Array.IndexOf(map.InterfaceMethods, getSiteMethod);
MethodInfo actualExplicitMethod = map.TargetMethods[index];

//late-bound call (slow)
object o = actualExplicitMethod.Invoke(this, new object[] { });

现在,首先你得到一个内部类型包裹在 System.Object ,因为原始界面是内部的,所以不能投射到你的界面,所以只要你打算使用该对象,你就可以有更多的乐趣反射。

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.

第二,我试过了,这个技术工作,但在你的特定情况下,这个方法调用一个窗体窗体抛出一个异常 - 顶级Windows窗体控件不能暴露为ActiveX控件。

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.".

这篇关于如何将控件转换为IOleObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆