Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>IOleObject</code> interface is a nested interface inside the internal <code>UnsafeNativeMethods</code> class in the <code>System.Windows.Forms</code> assembly. The <code>System.Windows.Forms.Control</code> implements it <em>internally</em>, and <em>explicitly</em>.</p> <p>Creating another interface with the same name and guid will not make it "the same interface" on a managed level.</p> <p>This line</p> <pre><code>IOleObject obj = (IOleObject) this; </code></pre> <p>represents managed .net casting, and has nothing to do with COM. This cast will work only with the <strong>exact same</strong> interface from the winforms assembly, which isn't public.</p> <p>You could try to use reflection via the <a href="http://msdn.microsoft.com/en-us/library/system.reflection.interfacemapping.aspx" rel="nofollow noreferrer">InterfaceMapping</a> structure to get the method (but note that this is not recommendable):</p> <pre><code>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[] { }); </code></pre> <p>Now, first, you get an internal type wrapped in <code>System.Object</code> 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.</p> <p>Second, I've tried it, the technique works, but in your specific scenario this method called on a windows Form throws an exception - <code>"Top-level Windows Forms control cannot be exposed as an ActiveX control."</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload