3

I need to call a C# BHO method from javascript

I have followed the solution given in this question

I get the error:

'mshtml.HTMLWindow2Class' does not contain a definition for 'myExtension'.

I cannot assign:

dynamic window = browser.Document.parentWindow; 

as parentWindow is undefined, I have to cast to (mshtml.IHTMLDocument2) and (mshtml.IHTMLWindow2)

Does anyone have a full working example I could refer to or any help or alternative solutions

1 Answer 1

6

I've the same problem. And the following fix seems work in my BHO:

dynamic window = _webBrowser.Document.parentWindow;
var windowEx = (IExpando)window;
PropertyInfo p = windowEx.AddProperty("myExtension");
p.SetValue(windowEx, this);

instead of:

dynamic window = _webBrowser.Document.parentWindow;
var windowEx = (IExpando)window;
PropertyInfo p = windowEx.AddProperty("myExtension");
window.myExtension = this;
Sign up to request clarification or add additional context in comments.

2 Comments

Super useful, taught me something and saved me an hour of time :) If you're ever back in SO - reply to this comment and I'll award you a bounty for this answer. Short note - in .NET <4.5 it's p.SetValue(windowEx, this,null);
On another note: the _webBrowser object needs to be an instance of 'IWebBrowser' not 'IWebBrowser2'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.