Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a C# class instance back to managed code from JavaScript via COM

The basic outline of my problem is shown in the code below. I'm hosting a WebBrowser control in a form and providing an ObjectForScripting with two methods: GiveMeAGizmo and GiveMeAGizmoUser. Both methods return the respective class instances:

[ComVisible]
public class Gizmo
{
    public string name { get; set; }
}

[ComVisible]
public class GizmoUser
{
    public void doSomethingWith(object oGizmo)
    {
        Gizmo g = (Gizmo) oGizmo;
        System.Diagnostics.Debug.WriteLine(g.name);
    }
}

In JavaScript, I create an instance of both classes, but I need to pass the first instance to a method on the second instance. The JS code looks a little like this:

var 
    // Returns a Gizmo instance
    gizmo = window.external.GiveMeAGizmo(),

    // Returns a GizmoUser instance
    gUser = window.external.GiveMeAGizmoUser();

gizmo.name = 'hello';

// Passes Gizmo instance back to C# code
gUser.doSomethingWith(gizmo);

This is where I've hit a wall. My C# method GizmoUser.doSomethingWith() cannot cast the object back to a Gizmo type. It throws the following error:

Unable to cast COM object of type 'System.__ComObject' to interface type 'Gizmo'

Unsure how to proceed, I tried a couple of other things:

  • Safe casting Gizmo g = oGizmo as Gizmo; (g is null)
  • Having the classes implement IDispatch and calling InvokeMember, as explained here. The member "name" is null.

I need this to work with .NET framework version lower than 4.0, so I cannot use dynamic. Does anybody know how I can get this working?

like image 896
Andy E Avatar asked Jan 14 '14 17:01

Andy E


1 Answers

How interesting. When we receive the oGizmo object back in doSomethingWith, it is of the type Windows Runtime Object. This behavior is consistent between JavaScript and VBScript.

Now, if we explicitly specify MarshalAs(UnmanagedType.IUnknown) on the return value of the GiveMeAGizmo() method, everything works fine, the object can be cast back to Gizmo inside doSomethingWith:

[ComVisible(true), ClassInterface(ClassInterfaceType.AutoDispatch)]
public class ObjectForScripting
{
    [return: MarshalAs(UnmanagedType.IUnknown)]
    public object GiveMeAGizmo()
    {
        return new Gizmo();
    }

    public object GiveMeAGizmoUser()
    {
        return new GizmoUser();
    }
}

Still, if we specify UnmanagedType.IDispatch or UnmanagedType.Struct (the default one which marshals the object as COM VARIANT), the problem is back.

Thus, there's a workaround, but no reasonable explanation for such COM interop behavior, so far.

[UPDATE] A few more experiments, below. Note how obtaining gizmo1 is successful, while gizmo2 is not:

C#:

// pass a Gizmo object to JavaScript
this.webBrowser.Document.InvokeScript("SetGizmo", new Object[] { new Gizmo()});

// get it back, this works
var gizmo1 = (Gizmo)this.webBrowser.Document.InvokeScript("GetGizmo");

// get a new Gizmo, via window.external.GiveMeAGizmo()
// this fails
var gizmo2 = (Gizmo)this.webBrowser.Document.InvokeScript("GetGizmo2");

JavaScript:

var _gizmo;

function SetGizmo(gizmo) { _gizmo = gizmo; }

function GetGizmo() { return _gizmo; }

function GetGizmo2() { return window.external.GiveMeAGizmo(); }

It's only a guess, but I think such behavior might have something to do with .NET security permission sets, imposed by WebBrowser.ObjectForScripting.

like image 181
noseratio Avatar answered Sep 28 '22 16:09

noseratio