I hate to answer my own question, but I really feel like the community ought to know the answer because it is short, simple, and beautiful with C# 4.0 and SO many people seem to have this problem.
Make sure that you correctly expose the Browser Helper Object:
[ComVisible(true),
Guid("DA8EA345-02AE-434E-82E9-448E3DB7629E"),
ClassInterface(ClassInterfaceType.None), ProgId("MyExtension"),
ComDefaultInterface(typeof(IExtension))]
public class BrowserHelperObject : IObjectWithSite, IExtension
{
...
public int Foo(string s) { ... }
...
public void OnDocumentComplete(dynamic frame, ref dynamic url)
{
...
dynamic window = browser.Document.parentWindow;
IExpando windowEx = (IExpando)window;
windowEx.AddProperty("myExtension");
window.myExtension = this;
...
}
...
}
And you will need a definition for your extensions:
[ComVisible(true),
Guid("4C1D2E51-018B-4A7C-8A07-618452573E42"),
InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IExtension
{
[DispId(1)]
int Foo(string s);
...
}
You can access your Browser Helper Object in javascript thus:
var result = window.myExtension.Foo("bar");
or just
var result = myExtension.Foo("bar");
That's it. Stop banging your head against the wall and go celebrate!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…