Tôi đã theo dõi this trả lời chính xác và đọc và đọc lại tất cả các kết quả của google. Thật không may, hầu hết tất cả chỉ đơn giản là bản sao và bột nhão của câu trả lời được tham chiếu (bao gồm câu 'Dừng đập đầu vào tường và đi ăn mừng!') Và nó không hiệu quả với tôi ... Vì vậy, sau nửa ngày làm việc, tôi thực sự sắp bắt đầu đập đầu của tôi ...Gọi phương thức C# BHO từ Javascript (vẫn không hoạt động)
Lỗi đơn giản của tôi: Đối tượng javascript windows.myExtension là 'không xác định' để gọi Foo trên đó phát sinh lỗi. Xem toàn bộ nguồn dưới đây. Có vẻ như tập hợp thuộc tính không thể xem được ở phía javascript.
Một số biết thêm thông tin:
- tôi sử dụng câu lệnh Debugger.Launch() cho cách thuận tiện để gỡ lỗi phần mở rộng của tôi, và các breakpoint là hit, và tất cả các chức năng mở rộng BHO được gọi và chạy đúng cách.
Cách khác nhận xét (với property.SetProperty) cũng không hoạt động, với các lỗi tương tự:
console.log (window.myExtension); // viết 'không xác định', tại sao?
Sử dụng VS 2010, Windows 7 x64, IE 9
Xin vui lòng cho tôi giúp chạy này ... Thx trước
Trang thử nghiệm đơn giản:
<!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>
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;
}
}
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);
}
}
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);
}
}
bài build bước được cấu hình như sau (và chạy đúng):
"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)"
Chỉ cần tham gia một đâm lúc này, nhưng có vẻ như nó chỉ thêm 'myExtension 'trên tài liệu hoàn thành. Hãy thử kiểm tra hack này: 'setTimeout (function() {console.log (window.myExtension);}, 1000);' sau 1 giây, nó sẽ xuất ra thứ gì đó trên bàn điều khiển. Tại thời điểm đó, tài liệu sẽ được tải và bạn sẽ có câu trả lời của mình. Vui lòng báo cáo lại sau bài kiểm tra này. –
Thx cho ý tưởng của bạn. Tôi đã thử nó và bây giờ một lần nữa. Nó không phải là một vấn đề thời gian. window.myExtension vẫn chưa được xác định trong cuộc gọi bị trì hoãn :-( –
@Dalorzo Vui lòng dừng lại với các chỉnh sửa tầm thường như thêm các thẻ mà không cần. Tôi đã liếc qua lịch sử của bạn và hầu hết các chỉnh sửa được đề xuất gần đây của bạn sẽ bị từ chối. 're unnarilyarily bumping câu hỏi cũ để trang trước bằng cách làm như vậy tầm thường (và thường sai và/hoặc không đầy đủ) chỉnh sửa –