How to enable ActiveX creation in a WebBrowser control

I have a WebBrowser control in my .NET program. It really doesn't matter which .net shell is used (wpf or winforms), because they all carry the Microsoft Internet Controls ActiveX component (ieframe.dll).

So I am loading some html / js code in WebBrowser. This code is trying to create some ActiveX and crash. Exactly the same code works fine when loading into full IE. But WebBrowser crashes: a new ActiveXObject ("myprogid") throws "Automation server cannot create the object."

Does the WebBrowser control have the ability to enable the creation of ActiveX's?

UPDATE: I added "

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

"at the top of the html that loads into WebBrowser. This does not help.

+5
source share
2 answers

Here is a WPF workaround.
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;} } 

And we need to add our own lib as a reference:

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

Then, in our client, the js code we will need to access the HostGateway instance through window.external:

 window.external.MyComponent.foo(); 
+1
source

I recently ran into the same problem, and my workaround (which does not require any additional references) is to just have a javascript function called ActiveXObject and a C # function that calls Activator.CreateInstance, a very simple illustration:

 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()); } } 
0
source

Source: https://habr.com/ru/post/1348052/


All Articles