mobileFX WebKitX CEF3 ActiveX 4.x
WebKitXCEF3Lib ActiveX Control / WebKitXCEF3 Object / AddObject Method

The programmatic identifier of the object instance.

The object is exposed to JavaScript through this global identifier, also accessible as named property of the window object.

An IDispatch interface pointer of the COM object instance you wish to expose to JavaScript.

In This Topic
    AddObject Method
    In This Topic
    Description

    Exports a native COM object to JavaScript.

    Syntax
    Visual Basic
    Public Sub AddObject( _
       ByVal Name As String, _
       ByVal Object As Object _
    ) 
    Parameters
    Name

    The programmatic identifier of the object instance.

    The object is exposed to JavaScript through this global identifier, also accessible as named property of the window object.

    Object

    An IDispatch interface pointer of the COM object instance you wish to expose to JavaScript.

    Remarks

    This technology allows you to use headless COM objects (non-controls) from JavaScript.

    Any simple in-process or out-of-process apartment threaded COM object with scalar properties and/or methods with scalar arguments and scalar return value can be wrapped into an V8 object and exposed through a global identifier (and window dot identifier) to the V8 JavaScript context of the main frame. The method will not work for COM objects that have non-scalar properties or method arguments or method return values.

    Visual Basic 6.0 developers, please note that if you wish to expose a local VB6 Class to JavaScript, you must compile your executable as ActiveX EXE. Standard executables will only work while in Visual Basic 6.0 IDE but not if you compile them.

    WebKitX in-process ActiveX wraps the IDispatch interface pointer you provide inside an in-process Proxy object and signals CEF Rendering Process to generate a Stub V8 object, which is exported to the Global object of the main frame's V8 context and its window DOM object. Thus, in JavaScript you can call your COM object directly from any scope.

    The communication between the Proxy and Stub counterparts is synchronous, meaning that when you make a call to your COM object from JavaScript, the rendering process is blocked. For that reason, avoid making utility calls while accessing properties or invoking methods of the COM object from JavaScript, such as console.log().

    You must make sure that your COM object stays alive while it is used by WebKitX. To release the object from WebKitX please use ReleaseObject method.

    OLE/COM Variants to V8 Values Conversion

    WebKitX has a powerful OLE/COM Variant to Google V8 Value bi-directional converter that works both for Scalar and Array values, including nested array values (arrays of arrays of variants). This conversion mechanism is available to any WebKitX method transmitting and/or returning an OLE/COM Variant, such as CallByName(), JSCallBack(), DispatchEvent() and AddObject.

    WebKitX can serialize variant scalars VT_BSTR, VT_UINT, VT_INT, VT_NULL, VT_BOOL, VT_R4, VT_R8, VT_CY, VT_DATE as well as variant safearray of variants VT_ARRAY|VT_VARIANT and even typed arrays VT_ARRAY|VT_BSTR, VT_ARRAY|VT_UINT, VT_ARRAY|VT_INT, VT_ARRAY|VT_BOOL, VT_ARRAY|VT_R4, VT_ARRAY|VT_R8, VT_ARRAY|VT_CY, VT_ARRAY|VT_DATE to Google V8 value objects. Currently serialization of VT_DISPATCH is not supported and therefore you cannot pass OLE/COM Objects to JavaScript with IDispatch members or arguments.

    Example
    Option Explicit
    
    Private Sub Form_Resize()
        
        On Error Resume Next
        WebKitX1.Move 0, 0, ScaleWidth, ScaleHeight
        Err.Clear
        
    End Sub
    
    Private Sub WebKitX1_OnCreate(ByVal Settings As WebKitXCEF3Lib.ISettings, CommandLineSwitches As String)
        
        Settings.plugins = False
        Settings.cache_path = App.Path + "\MyCache"
        Settings.application_cache = App.Path + "\MyAppCache"
        Settings.persist_session_cookies = 1
        Settings.persist_user_preferences = 1
        CommandLineSwitches = ""
        
    End Sub
    
    Private Sub WebKitX1_OnBrowserReady()
        WebKitX1.Open App.Path + "\test.html"
    End Sub
    
    Private Sub WebKitX1_OnLoadEnd()
    
        ' Add an instance of Class1 as window.myobject to JavaScript
        WebKitX1.AddObject "myobject", New Class1
        
    End Sub
    Option Explicit
    
    Private m_Goo As String
    
    Public Xoo As String
    
    Private Sub Class_Initialize()
        m_Goo = "this is goo"
        Xoo = "this is Xoo"
    End Sub
    
    Public Sub Foo()
        Debug.Print "Function Foo"
        MsgBox "foo"
    End Sub
    
    Public Function Zoo(ByVal a As String, ByVal b As String) As String
        Zoo = "Zoo Result = " + a + " " + b
        Debug.Print "Function Zoo(" + a + "," + b + ") = " + Zoo
    End Function
    
    Public Property Get Goo() As String
        Goo = m_Goo
        Debug.Print "Property Get Goo: " + m_Goo
    End Property
    
    Public Property Let Goo(ByVal s As String)
        m_Goo = s
        Debug.Print "Property Let Goo: " + m_Goo
    End Property
    See Also