从 Javascript 调用 C# BHO 方法 [英] Calling C# BHO methods from Javascript

查看:19
本文介绍了从 Javascript 调用 C# BHO 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试弄清楚如何从页面内的 Javascript 调用我的 BHO 对象中的 C# 方法.我发现了很多关于如何在 C++/ATL/Com 中执行此操作的页面,例如:

I'm trying to figure out how to call C# methods in my BHO object from Javascript within the page. I found numerous pages on how to do it in C++/ATL/Com such as:

浏览器帮助对象和脚本选项

从 Javascript 调用 BHO 方法?

我已经尝试在 C# 中正确实现它,但我无法让它工作,可能是因为我有一些我不完全理解的明显 COM 问题.

I have tried to follow along and implement it correctly in C#, but I can't get it to work probably due to some obvious COM problems that I have which I don't fully understand.

我使用的是 C# 4.0.

I am using C# 4.0.

以下是代码的相关部分:

Here are the relevant parts of the code:

using SHDocVw;
using mshtml;
using System.Runtime.InteropServices;

[ComVisible(true),
 Guid("300736C4-DCDA-4DB0-90AD-4510A12EBBC6"),
 ClassInterface(ClassInterfaceType.None),
 ProgId("My Extension")]
public class BrowserHelperObject : IObjectWithSite
{
    const int DISPATCH_PROPERTYPUT = 4;
    const int FDEX_NAME_ENSURE = 2;
    const uint LOCALE_USER_DEFAULT = 0x0400;

    WebBrowser browser;

    ...
    public void OnDocumentComplete(dynamic frame, ref dynamic url)
    {
        ...
        var window = browser.Document.parentWindow;

        int pid = 0;
        window.GetDispId("myExtension", FDEX_NAME_ENSURE, ref pid);

        System.Runtime.InteropServices.ComTypes.DISPPARAMS dispParms = new System.Runtime.InteropServices.ComTypes.DISPPARAMS();
        dispParms.cArgs = 1;
        dispParms.cNamedArgs = 0;
        dispParms.rgvarg = ???;
        dispParms.rgdispidNamedArgs = IntPtr.Zero;
        System.Runtime.InteropServices.ComTypes.EXCEPINFO einfo = new System.Runtime.InteropServices.ComTypes.EXCEPINFO();
        window.Invoke(pid, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYPUT, ref dispParms, this, ref einfo);            
        ...
    }

推荐答案

我不想回答我自己的问题,但我真的觉得社区应该知道答案,因为它简短、简单、漂亮,而且 C# 4.0 和很多人似乎都有这个问题.

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);
    ...
}

您可以这样在 javascript 中访问您的浏览器助手对象:

You can access your Browser Helper Object in javascript thus:

var result = window.myExtension.Foo("bar");

或者只是

var result = myExtension.Foo("bar");

就是这样.别用头撞墙,去庆祝吧!

That's it. Stop banging your head against the wall and go celebrate!

这篇关于从 Javascript 调用 C# BHO 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆