gpt4 book ai didi

activex - 如何允许在 WebBrowser 控件中创建 ActiveX

转载 作者:行者123 更新时间:2023-12-02 16:48:00 25 4
gpt4 key购买 nike

我的 .NET 程序中有 WebBrowser 控件。实际上使用哪个 .net 包装器(wpf 或 winforms)并不重要,因为它们都包装 ActiveX 组件“Microsoft Internet Controls”(ieframe.dll)。

所以我将一些 html/js 代码加载到 WebBrowser 中。此代码尝试创建一些 ActiveX 但失败。当加载到完整的 IE 中时,完全相同的代码可以正常工作。但在 WebBrowser 中失败: new ActiveXObject("myprogid") 抛出“自动化服务器无法创建对象”。

WebBrowser 控件是否具有允许创建 ActiveX 的能力?

更新:我添加了“

<!-- saved from url=(0014)about:internet -->

”位于加载到 WebBrowser 中的 html 的顶部。这没有帮助。

最佳答案

这是 WPF 的解决方法。
MainWindow.xaml:

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=system.windows.forms"
>
<Grid>
<WebBrowser x:Name="webBrowser" SnapsToDevicePixels="True" >
</WebBrowser>
</Grid>
</Window>

MainWindow.xaml.cs:

public void Run(Uri uri)
{
m_gateway = new HostGateway
{
MyComponent = SomeNativeLib.SomeNativeComponent
};

webBrowser.ObjectForScripting = m_gateway;

webBrowser.Navigate("about:blank");
webBrowser.Navigate(uri);
}

[ComVisible(true)]
public class HostGateway
{
public SomeNativeLib.SomeNativeComponent MyComponent {get;set;}
}

我们需要添加 native 库作为引用:

<Reference Include="SomeNativeLib, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<EmbedInteropTypes>True</EmbedInteropTypes>
<HintPath>..\..\..\References\SomeNativeLib.dll</HintPath>
</Reference>

然后在我们的客户端 js 代码中,我们必须通过 window.external 访问 HostGateway 实例:

window.external.MyComponent.foo();

关于activex - 如何允许在 WebBrowser 控件中创建 ActiveX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5660620/

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