none
How to get .Net object from HTML page RRS feed

  • Question

  • Sorry the title is a bit vague but wasnt quite sure how to word it.

    We have a managed C++ application that has an html view.  This app used to talk to a VB6 dll and the HTML page obtained VB6 objects via QueryInterface - i.e.

    in html :

    var myObject = window.external.Engine.GetObject(Id)    (with 'Engine' being a VB6 class & GetObect being one of its methods)

    the C++ used QueryInterface to get the Engine object:

                    HRESULT hResult = g_App.GetEngine()->QueryInterface(IID_IDispatch,
                        reinterpret_cast<void**>(&piDispatch));

                    piDispatch->AddRef();
                    pvResult->vt = VT_DISPATCH;
                    pvResult->pdispVal= piDispatch;

    Now, the VB6 dll has been converted to VB.Net and of course this QueryInterface code no longer works.

    What would be the appropriate method for the HTML page to obtain the VB6.Net object?

    Thanks
    Thursday, April 17, 2008 8:48 AM

Answers

  • Actually I wouldn't think you'd need to use QueryInterface in a 100% managed enviroment...

    If you set the managed web browser control's "ObjectForScripting" property to a COM visable class you can access the methods of that class directly with window.external.methodName

    If you wanted to call a function in a VB.NET DLL just add the referance to the DLL and call it's method directly from the managed C++ seems like an easier solution. I guess you could set the whole dll to COM visable and point the Web Browser control at it, but that wouldn't seem safe to me...
    Friday, April 18, 2008 1:49 PM
  • Thanks for the reply,

     

    I cant use QueryInterface any more as this is only for COM objects.

     

    windows.external.methodname works fine in that the appropriate method is called, but i need to return a reference to the managed object back to the JS so that it can then call methods on that object.

     

    We have since found a solution:

     

    STDMETHODIMP CControlDispatch::Invoke(DISPID DispId, REFIID /*rIId*/, LCID /*LcId*/, WORD wFlags,

    DISPPARAMS* pDispParams, VARIANT* pvResult, EXCEPINFO* /*pExcepInfo*/, UINT* /*puArgumentError*/)

    {

     switch(DispId)

     {

       case DISPID_Engine:

       {

    BO::Engine* engine = g_App.GetEngine(); //Get the managed object

    //Get an IDispatch interface for it

    System::IntPtr dispatch = System::Runtime::InteropServices::Marshal::GetIDispatchForObject(engine);

    pvResult->vt = VT_DISPATCH;

    //The IntPtr can now be cast to an IDispatch*

    pvResult->pdispVal = reinterpret_cast<IDispatch*>(dispatch.ToPointer());

    return S_OK;

       }

       case DISPID_SomeOtherObject:

       {

          etc...

       }

    }

     

    This works fine (now have a minor problem when trying to pass parameters from js to managed object but probably just more marshalling issues).

    Friday, April 18, 2008 2:41 PM

All replies

  • Actually I wouldn't think you'd need to use QueryInterface in a 100% managed enviroment...

    If you set the managed web browser control's "ObjectForScripting" property to a COM visable class you can access the methods of that class directly with window.external.methodName

    If you wanted to call a function in a VB.NET DLL just add the referance to the DLL and call it's method directly from the managed C++ seems like an easier solution. I guess you could set the whole dll to COM visable and point the Web Browser control at it, but that wouldn't seem safe to me...
    Friday, April 18, 2008 1:49 PM
  • Thanks for the reply,

     

    I cant use QueryInterface any more as this is only for COM objects.

     

    windows.external.methodname works fine in that the appropriate method is called, but i need to return a reference to the managed object back to the JS so that it can then call methods on that object.

     

    We have since found a solution:

     

    STDMETHODIMP CControlDispatch::Invoke(DISPID DispId, REFIID /*rIId*/, LCID /*LcId*/, WORD wFlags,

    DISPPARAMS* pDispParams, VARIANT* pvResult, EXCEPINFO* /*pExcepInfo*/, UINT* /*puArgumentError*/)

    {

     switch(DispId)

     {

       case DISPID_Engine:

       {

    BO::Engine* engine = g_App.GetEngine(); //Get the managed object

    //Get an IDispatch interface for it

    System::IntPtr dispatch = System::Runtime::InteropServices::Marshal::GetIDispatchForObject(engine);

    pvResult->vt = VT_DISPATCH;

    //The IntPtr can now be cast to an IDispatch*

    pvResult->pdispVal = reinterpret_cast<IDispatch*>(dispatch.ToPointer());

    return S_OK;

       }

       case DISPID_SomeOtherObject:

       {

          etc...

       }

    }

     

    This works fine (now have a minor problem when trying to pass parameters from js to managed object but probably just more marshalling issues).

    Friday, April 18, 2008 2:41 PM