SteveSandersonMS / WebWindow

.NET Core library to open native OS windows containing web UI on Windows, Mac, and Linux. Experimental.
Apache License 2.0
1.99k stars 215 forks source link

Implement IWebView2WebView5->AddRemoteObject #63

Open ITAgnesmeyer opened 4 years ago

ITAgnesmeyer commented 4 years ago

Hi There, WebView2 inteface IWebView2WebView5 supports AddRemoteObject. this can be a ComVisible DotNet-Class. So it would be nice to implement it.

I try it on WebWindow.Windows.cpp and it works:

//WebWindow.h
void AddRemoteObject(AutoString name, IDispatch* obj);
void RemoveRemoteObject(AutoString name);
//WebWindow.Windows.cpp
void WebWindow::AddRemoteObject(AutoString name, IDispatch* obj)
{
    VARIANT v ={};
    v.pdispVal = obj;
    v.vt = VT_DISPATCH;

    HRESULT rs = _webviewWindow5->AddRemoteObject(name, &v);
    if(rs == S_OK)
    {
        printf("OK");
    }
    else
    {
        printf("NOK");
    }
}
void WebWindow::RemoveRemoteObject(AutoString name)
{
    _webviewWindow5->RemoveRemoteObject(name);
}

//Exports.cpp
EXPORTED void WebWindow_AddRemoteObject(WebWindow* instance,AutoString name, 
      IDispatch* obj)
{
      instance->AddRemoteObject(name, obj);
}
EXPORTED void WebWindow_RemoveRemoteObject(WebWindow* instance,AutoString name)
{
      instance->RemoveRemoteObject(name);
}

Dotnet Site:


//Declare
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Auto)]
#pragma warning disable 618
public static extern void WebWindow_AddRemoteObject(IntPtr instance, 
           string name,
           [In][MarshalAs(UnmanagedType.IDispatch)] object obj);
#pragma warning restore 618

public void AddRemoteObject(string name, object obj)
{
   WebWindow_AddRemoteObject(this._nativeWebWindow, name, obj);
   _remoteObjectList.Add(name);
}
//~WebWindow
string[] remoteNames = this._remoteObjectList.ToArray();
foreach (string name in remoteNames)
{
   try
   {
      this.RemoveRemoteObject(name);
   }
   catch (Exception )
   {
      //
   }
}

Client side:

//the Script Object
    [ComVisible(true)]
    [Guid("21A6CC64-8E9A-4659-85AA-32A07B2BDA0B")]
    public class TestObject
    {
        public string FirstMessage {get;set;}
        public string SencondMessage{get;set;}

        public int GetStringLen(string input)
        {
            return input.Length;
        }
    }

//program.cs
 class Program
    {
        private static TestObject _TestObj;
        static void Main(string[] args)
        {

            _TestObj = new TestObject();
            _TestObj.FirstMessage = "This is a message from Com!";
          var window = new WebWindow("My great app", options =>
            {  ...

            //At the end!
             window.AddRemoteObject("testObject", _TestObj);
            window.NavigateToLocalFile("wwwroot/index.html");
            window.WaitForExit();
        }
... 

The only question is whether this also works on Mac and Linux. Unfortunately I don't have the possibility to test it. I also don't know if this function is available for Mac and Linux at all? So it would be good if one could do this.

Best regards