In my project, I successfully merged the capabilities of webbrowsercontrol ObjectForScripting to invoke C# functions from JavaScript, as explained in this source. Additionally, I utilized a proxy for the webbrowser control, following the guidance provided in this source.
Individually, each function works flawlessly. However, when combined, the calls from JavaScript cease to function. The error message displayed is:
"Object doesn't support property or method 'Test'"
At the bottom, you can find the source code for an example.
To test both functionalities, you can comment and uncomment the block in Form1_Load
:
// Comment this block for Callback working
object obj = webBrowser1.ActiveXInstance;
IOleObject oc = obj as IOleObject;
oc.SetClientSite(Proxy as IOleClientSite);
I suspect that the issue lies within the QueryService
function from the IServiceProvider
interface. Despite attempting solutions with GUIDs from Scriptmanager
, I have not been able to resolve it.
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace ProxyTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
proxy Proxy = new proxy();
private void Form1_Load(object sender, EventArgs e)
{
// Implementation remains unchanged
}
}
[ComVisible(true)]
// Other classes untouched
}
Update 01.11.2016: Following Sheng Jiang's suggestion, I made considerable progress towards achieving a functional example. Though the messagebox successfully appears upon clicking, an InvalidVariant exception follows. Any insights on resolving this issue would be greatly appreciated. Below is the updated code snippet:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace ProxyTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
proxy Proxy = new proxy();
private void Form1_Load(object sender, EventArgs e)
{
// Implementation remains unchanged
}
}
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
// Additional interfaces and classes introduced here
}