gpt4 book ai didi

c# - 在本地主机中加载站点时,c# bho 中的函数未公开

转载 作者:太空宇宙 更新时间:2023-11-03 10:50:28 24 4
gpt4 key购买 nike

我使用 c# 创建了一个示例 bho 并根据这篇文章公开了 1 个函数

Calling C# BHO methods from Javascript

bho 已成功注册到 IE 9,我可以在网络托管的站点上访问 javascript 中公开的函数。

我使用本地服务器创建了一个测试页面,但我的 bho 函数现在未定义。

我尝试将 url 从 localhost 更改为 ip 地址,但仍然没有效果。

下面是我的代码。如果有人可以检查我的代码是否有问题,那就太好了;

谢谢。

BHO.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms.Design;
using SHDocVw;
using mshtml;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Expando;
using Microsoft.Win32;

namespace MySampleBHO
{
[
ComVisible(true),
Guid("7bb16759-fdfc-4e3f-a081-eb65e4683640"),
ClassInterface(ClassInterfaceType.None),
ProgId("SIV"), ComDefaultInterface(typeof(IExtension))
]
public class BHO:IObjectWithSite, IExtension
{
WebBrowser webBrowser;
HTMLDocument document;

public void OnDocumentComplete(object pDisp, ref object URL) {
document = (HTMLDocument)webBrowser.Document;

dynamic window = webBrowser.Document.parentWindow;
IExpando windowEx = (IExpando)window;
windowEx.AddProperty("sEnhancer");
window.sEnhancer = this;
}

public String goEnhance(String ImageId, int Width, int Height) {
var img = document.getElementById(ImageId);
var src = img.getAttribute("src");
return src.ToString();
}

public int SetSite(object site)
{
if (site != null)
{
webBrowser = (WebBrowser)site;
webBrowser.DocumentComplete +=
new DWebBrowserEvents2_DocumentCompleteEventHandler(
this.OnDocumentComplete);
}
else
{
webBrowser.DocumentComplete -=
new DWebBrowserEvents2_DocumentCompleteEventHandler(
this.OnDocumentComplete);
webBrowser = null;
}

return 0;
}

public int GetSite(ref Guid guid, out IntPtr ppvSite)
{
IntPtr punk = Marshal.GetIUnknownForObject(webBrowser);
int hr = Marshal.QueryInterface(punk, ref guid, out ppvSite);
Marshal.Release(punk);

return hr;
}

public static string BHOKEYNAME =
"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects";

[ComRegisterFunction]
public static void RegisterBHO(Type type)
{
RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(BHOKEYNAME, true);

if (registryKey == null)
registryKey = Registry.LocalMachine.CreateSubKey(BHOKEYNAME);

string guid = type.GUID.ToString("B");
RegistryKey ourKey = registryKey.OpenSubKey(guid);

if (ourKey == null)
ourKey = registryKey.CreateSubKey(guid);

ourKey.SetValue("Alright", 1);
registryKey.Close();
ourKey.Close();
}

[ComUnregisterFunction]
public static void UnregisterBHO(Type type)
{
RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(BHOKEYNAME, true);
string guid = type.GUID.ToString("B");

if (registryKey != null)
registryKey.DeleteSubKey(guid, false);
}
}
}

IObjectWithSite.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace MySampleBHO
{
[
ComVisible(true),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
Guid("FC4801A3-2BA9-11CF-A229-00AA003D7352")
]
public interface IObjectWithSite
{
[PreserveSig]
int SetSite([MarshalAs(UnmanagedType.IUnknown)]object site);
[PreserveSig]
int GetSite(ref Guid guid, out IntPtr ppvSite);

}
}

IExtension.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace MySampleBHO
{
[
ComVisible(true),
InterfaceType(ComInterfaceType.InterfaceIsDual),
Guid("FC4801A3-2BA9-11CF-A229-00AA003D7352")
]
public interface IExtension
{
String goEnhance(String ImageId, int Width, int Height);
}
}

最佳答案

我已经找到了我的问题的解决方案...只需要更改 onDocumentComplete 函数中的某些部分...

解决方案

更改以下代码:

public void OnDocumentComplete(object pDisp, ref object URL) {
document = (HTMLDocument)webBrowser.Document;

dynamic window = webBrowser.Document.parentWindow;
IExpando windowEx = (IExpando)window;
windowEx.AddProperty("sEnhancer");
window.sEnhancer = this;
}

到:


public void OnDocumentComplete(object pDisp, ref object URL) {
document = (HTMLDocument)webBrowser.Document;

dynamic window = webBrowser.Document.parentWindow;
IExpando windowEx = (IExpando)window;
PropertyInfo p = windowEx.AddProperty("sEnhancer");
p.SetValue(windowEx, this);
}

解决方案基于这篇文章 BHO exposing javascript method works in IE 9+ but fails in earlier versions

关于c# - 在本地主机中加载站点时,c# bho 中的函数未公开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21606173/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com