g.p*_*dou 6 c# com internet-explorer bho
我完全按照这个答案阅读并重读了所有谷歌的调查结果.不幸的是,大部分都只是所引用答案的复制和粘贴(包括"停止撞墙而去庆祝!")这句话对我不起作用......所以经过半天的工作我就是真的要开始敲我的脑袋了......
我的简单错误:javascript windows.myExtension对象是'undefined'所以在它上面调用Foo会引发错误.请参阅下面的完整资源.看起来属性集在javascript方面是不可见的.
更多信息:
注释的替代(使用property.SetProperty)也不起作用,具有相同的错误:
的console.log(window.myExtension); //写'undefined',为什么?
使用VS 2010,Windows 7 x64,IE 9
请让我帮忙运行这个... Thx提前
简单的测试页面:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
console.log(window.myExtension); // Writes undefined why? It should be an object...
var result = window.myExtension.Foo("bar"); // Obviously throws and error if window.myExtension is undefined
</script>
<title></title>
</head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
BrowserHelperObject.cs
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Expando;
using Microsoft.Win32;
using SHDocVw;
namespace IEExtensionTest
{
[ComVisible(true)]
[Guid("DA8EA345-02AE-434E-82E9-448E3DB7629E")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("MyExtension")]
[ComDefaultInterface(typeof(IExtension))]
public class BrowserHelperObject : IObjectWithSite, IExtension
{
private WebBrowser webBrowser;
public int Foo(string s)
{
return 0;
}
public void OnDocumentComplete(dynamic frame, ref dynamic url)
{
Debugger.Launch();
dynamic window = webBrowser.Document.parentWindow;
var windowEx = (IExpando)window;
windowEx.AddProperty("myExtension");
window.myExtension = this;
//var property = windowEx.AddProperty("MyExtension");
//property.SetValue(windowEx, this, null);
}
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);
}
public int SetSite(object site)
{
if (site != null)
{
webBrowser = (WebBrowser)site;
webBrowser.DocumentComplete += OnDocumentComplete;
}
else
{
webBrowser.DocumentComplete -= 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;
}
}
Run Code Online (Sandbox Code Playgroud)
IObjectWithSite.cs
using System;
using System.Runtime.InteropServices;
namespace IEExtensionTest
{
[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);
}
}
Run Code Online (Sandbox Code Playgroud)
IExtension.cs
using System;
using System.Runtime.InteropServices;
namespace IEExtensionTest
{
[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);
}
}
Run Code Online (Sandbox Code Playgroud)
后构建步骤配置如下(并正确运行):
"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\gacutil.exe" /f /i "$(TargetDir)$(TargetFileName)"
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe" /unregister "$(TargetDir)$(TargetFileName)"
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe" "$(TargetDir)$(TargetFileName)"
Run Code Online (Sandbox Code Playgroud)
尝试用窗户。外部.myExtension(); 据我所知,外部是指承载你形态的物体。另外,如果这不起作用——一定要先尝试简单的事情,然后进行相反的工作。
这是一个应该适合您的简单表格:
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")] // Note full trust.
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public partial class BasicJSScriptableForm : Form
{
private void BasicJSScriptableForm _Load(object sender, EventArgs e){
this.WebBrowser1.Navigate("yourpage");
}
public string TestMethod(string input){
return string.Format("echo: {0}", input);
}
}
Run Code Online (Sandbox Code Playgroud)
然后在页面中:
$(document).ready(function() {
alert(window.external.TestMethod("It will hopefully work."));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1701 次 |
| 最近记录: |