【问题标题】:How to allow ActiveX creating in WebBrowser control如何允许在 WebBrowser 控件中创建 ActiveX
【发布时间】:2011-08-05 08:39:30
【问题描述】:

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

所以我将一些 html/js 代码加载到 WebBrowser 中。此代码尝试创建一些 ActiveX 并失败。完全相同的代码在加载到完整的 IE 中时可以正常工作。但在 WebBrowser 中失败:new ActiveXObject("myprogid") throws "Automation server can't create object"。

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

更新:我添加了“

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

" 在加载到 WebBrowser 的 html 顶部。没用。

【问题讨论】:

  • 你解决过这个问题吗?我有完全相同的问题!问候。
  • 不,我没有找到问题的解决方案。但我使用了一种解决方法:我在 .net 主机(WebBrowser 所在的位置)中创建了 ActiveX,并使其可用于 WebBrowser 内的 JS 代码。
  • 您能否详细说明您的解决方法,包括代码示例?我有类似的问题!问候。
  • @AviramNetanel 请我回答

标签: activex webbrowser-control


【解决方案1】:

这是 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;}
}

我们需要添加本地库作为参考:

<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();

【讨论】:

    【解决方案2】:

    我最近遇到了同样的问题,我的解决方法(不需要任何额外的引用)是只拥有一个名为 ActiveXObject 的 javascript 函数和一个调用 Activator.CreateInstance 的 C# 函数,一个非常简单的说明:

    using System;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    [ComVisible(true)]
    public class TestForm :
        Form {
    
        public Object newActiveXObject(String progId) {
            return(Activator.CreateInstance(Type.GetTypeFromProgID(progId)));
        }
    
        public TestForm() {
            Controls.Add(
                new WebBrowser() {
                    ObjectForScripting = this,
                    DocumentText =
                        "<script>" +
                        "function ActiveXObject(progId) { return(window.external.newActiveXObject(progId)); }" +
                        "document.write('<pre>' + new ActiveXObject('WScript.Shell').exec('cmd /c help').stdOut.readAll() + '</pre>');" +
                        "</script>",
                    Dock = DockStyle.Fill
                }
            );
        }
    
        [STAThread]
        public static void Main() {
            Application.EnableVisualStyles();
            Application.Run(new TestForm());
        }
    
    }
    

    【讨论】:

    • 感谢分享!我正在寻找或多或少完全像这样的东西。我喜欢 HTA 的简单性,我用它们为我自己或家人做一些小型应用程序。但在 Windows 11 上,它们将被削弱,imo 主要是因为无法启动单独的 IEBrowser (iexplorer.exe) 实例。因此,我正在研究创建一个 WinForms 应用程序,该应用程序的行为类似于自定义 html 文件的运行时环境,就像当前的 mshta.exe 一样。这将是有趣的测试。
    猜你喜欢
    • 2014-02-12
    • 1970-01-01
    • 2023-03-14
    • 2012-02-28
    • 2017-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多