Get any interface supported by an object

tarunlalwani

Member
Joined
Oct 14, 2007
Messages
7
Programming Experience
Beginner
Hi All,

I have to create a function that would allow me to return a object of a non default - interface from a object. I am pretty new to VB.NET and have tried below code. But i am not sure how can i convert back IntPtr to actual object. I don't want to reference the actual library of the object because this function would be used at run-time for any object and for any Interface.

VB.NET:
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim IID_IOleContainer = New Guid("{0000011b-0000-0000-C000-000000000046}")

        Dim IID_IWebBrowser2 = New Guid("D30C1661-CDAF-11D0-8A3E-00C04FC9E26E")
        Dim IID_IHTMLDocument2 = New Guid("332C4425-26CB-11D0-B483-00C04FD90119")
        Dim IID_IHTMLElement = New Guid("3050F1FF-98B5-11CF-BB82-00AA00BDCE0B")
        Dim IID_IEnumUnknown = New Guid("00000100-0000-0000-C000-000000000046")
        Dim IID_IDispatch = New Guid("00020400-0000-0000-C000-000000000046")
        Dim IID_IUnknown = New Guid("00000000-0000-0000-C000-000000000046")
        Dim t As System.Type
        t = System.Type.GetTypeFromCLSID(IID_IHTMLDocument2)
        Dim u As mshtml.IHTMLElement
        Dim x
        x = CreateObject("InternetExplorer.Application")
        x.visible = True
        x.navigate2("www.google.com")
        Do

        Loop While x.busy

        Dim y
        y = Marshal.GetIUnknownForObject(x.document)
        Dim z, b
        Dim A As IntPtr
        Dim f As Object
        Dim g As mshtml.IHTMLDocument2
        Dim c As Type, d
        A = Nothing
        z = Marshal.QueryInterface(y, IID_IOleContainer, A)
        'Not sure how to convert this A intPtr to a callable object
        'So that i can use A.EnumObjects
        'b = t.InvokeMember("title", Reflection.BindingFlags.IgnoreCase + Reflection.BindingFlags.GetProperty, Nothing, A, Nothing)
        'c = a.GetType()
        'c.InvokeMember("title", Reflection.BindingFlags.GetProperty)
        'DirectCast(x.document, )
    End Sub

Any pointers or help is realy appreciated
 
Why not use the WebBrowser control included in Framework? No additional references needed. Just grab the DocumentTitle from DocumentCompleted event.

Incidentally, if you do this call:
VB.NET:
Dim o As Object = Marshal.GetUniqueObjectForIUnknown(A)
and use the o Object in your b = t.InvokeMember call (instead of A), then b variable will hold the title.
 
The problem is the A is pointer to IID_IOleContainer interface but when i use Marshal.GetUniqueObjectForIUnknown(a) it is returning the default interface supported by the object we created. In this case it is HTMLDocument. BUt i want object which is for IOleContainer interface.

I can't use a web browser control because the library i am working on work with external browsers.
 
It does not return the default interface, it is returning an Object (which can contain any type) based on the queried interface wrapped as base type IUnknown. Documentation explains more about this:
The first parameter, pUnk, represents an IUnknown interface pointer; however, because all COM interfaces derive directly or indirectly from IUnknown, you can pass any COM interface to this method. The object returned by GetObjectForIUnknown is a runtime callable wrapper, which the common language runtime manages as it does any other managed object. The type of this wrapper is often a generic System.__ComObject type, which is a hidden type used when the wrapper type is ambiguous. You can still make late-bound calls to such a generic type as long as the COM object implements the IDispatch interface. Likewise, you can cast the returned object to an appropriate COM interface.
 
Back
Top