Using a Proxy in C# WebBrowserControl and Making Calls from Javascript to C#

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
}

Answer №1

When replacing the client site, webBrowser1.ObjectForScripting will have no effect. The default client site in WinForms implements IDocHostUIHandler::GetExternal and retrieves the value of ObjectForScripting. To achieve the same functionality, you can implement this in your Proxy class.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

A clever JavaScript function generator encapsulated within an instant function nested within a jQuery ready statement

This code snippet features an immediate function enclosed within a jQuery ready function. $((function(_this) { return function() { document.write('called!'); }; })(this)); I am puzzled by where the resultant function ...

Accessing the URL causes malfunctioning of the dynamic routing in Angular 2

I am currently working on implementing dynamic routing functionality in my Angular application. So far, I have successfully achieved the following functionalities: Addition of routing to an existing angular component based on user input Removal of routin ...

When making a jQuery + AJAX request, IE8 is causing a null return value

I'm completely stumped as to why this issue is happening. To start, the code has been checked and validated by the W3C validator as HTML5, except for some URL encoding problems (such as & needing to be &), but I don't have the ability ...

What's causing the navigation anchor tags to malfunction?

My website consists of two pages. The home page features a one-page navigation system where clicking on 'about' scrolls down to the corresponding section within the same page. PART 1 - On index.html However, when clicking on the 'blog&apo ...

How to sync two carousels in Bootstrap 5 to slide simultaneously with just one click on the next and previous buttons

I am trying to implement dual sliding carousels using Bootstrap 5, but I am encountering issues with getting them to slide simultaneously. Despite incorporating data-bs-target=".carousel", the synchronization isn't working as intended in my ...

JavaScript Object Notation, asynchronous JavaScript and XML, and Active Server Pages

In my client-side file, there is an ajax call within jQuery that sends JSON data to a WebMethod on the server-side. The code snippet below demonstrates how it can be achieved: Server-side WebMethod (C#) [System.Web.Services.WebMethod] public static strin ...

Is it possible to execute my tests while using incognito mode?

Currently, the solution I'm testing automatically saves the login information, causing my tests to fail when opening a new browser. Even browser.Dispose() doesn't resolve this issue. It seems like testing in incognito mode would be more reliable, ...

What is the essential Angular 2 script that must be included for a simple Angular 2 application to function properly?

I'm currently working through the latest Tuts+ tutorial on Angular 2 In the tutorial, the author references adding this script: <script src="node_modules/angular2/bundles/angular2.sfx.dev.js"></script> However, in the most recent beta re ...

The CSS styling is not being rendered correctly on the AngularJS HTML page

Encountering a puzzling situation here. Our angular controller is successfully delivering data to the page, but we are facing an issue with rendering a table due to an unknown number of columns: <table border="1" ng-repeat="table in xc.tables"> ...

The active tabs or placeholders do not update properly when I click on them

Is there anyone who can help figure out why the tabs are not functioning as expected? The goal is for the tabs to change the input field placeholder and perform a search based on the active tab. If you're able to assist, please review my complete cod ...

Retrieve information by utilizing query string parameters in Next.js

When using my getInitialProps function, I make two requests to fetch data. Now, I want to add a condition to make an additional request if the query params key is for a legal person. For example, if the URL is https://www.example.com.br/?legal-person, I n ...

Clicking on the FLOT graph will trigger an expansion of the visualization

Currently, I am utilizing the flot library for data plotting functionality. Flot utilizes the height and width properties of a div to create the plotted data, as shown below: <div id="graph" style="width: 300px; height: 300px;"></div> I am in ...

How can we ensure that Protractor's ElementArrayFinder 'each' function pauses until the current action has finished before moving on to the next iteration?

Currently, I am facing an issue while trying to utilize an 'each' loop in my Angular 8 app's end-to-end tests using protractor. Within my page object, I have created a method that returns an ElementArrayFinder. public getCards(): ElementArr ...

Pass data between JavaScript and PHP using the Phaser framework

I am trying to pass a JavaScript variable to PHP and then store it in a database. Despite searching for solutions on Google, I have not been successful. Most suggestions involve using AJAX, but the code doesn't seem to work when I try it. I attempted ...

Ways to handle parsing of curly brackets JSON in Google Apps Script?

How can I use Google Apps Script to parse through the output of an API and convert it into a table in Google Sheets? I attempted the following code, but it only returns Null values. var response = UrlFetchApp.fetch(url, options); var text = response.getCo ...

Complete interaction with child processes in Node.js

I have a basic C++ program compiled using the command gcc 1.cpp -o 1.exe. // 1.cpp #include <stdio.h> int main(){ int num = 0; scanf("%d", &num); printf("%d", num + 1000); scanf("%d", &num); printf("\n%d", num + 1000); ...

What is the best way to filter an array of objects using an array of strings?

Suppose we have an array filled with objects : people = [ {id: "1", name: "abc", gender: "m", age:"15", country:"USA" }, {id: "2", name: "def", gender: "m", age:"2 ...

Exploring Unanchored Substring Queries in Loopback API

Searching for a solution to implement a basic substring query using the loopback API for a typeahead field. Despite my efforts, I have not been able to find a clear answer. I simply want to input a substring and retrieve all brands that contain that subst ...

Deliver search findings that are determined by matching criteria, rather than by identification numbers

I am seeking to return a JSON match upon form submission, rather than simply searching for a specific ID. However, I am uncertain about how to structure this. I have the ability to search for the necessary match in a JavaScript document within one of my n ...

Having trouble getting Laravel Full Calendar to function properly with a JQuery and Bootstrap theme

Using the Laravel full calendar package maddhatter/laravel-fullcalendar, I am facing an issue where the package is not recognizing my theme's jQuery, Bootstrap, and Moment. I have included all these in the master blade and extended it in this blade. ...