0
votes

I'm updating a program in vb6 calling a Winform C# dll through COM interop. In this C# dll, the winform contain a webbrowser that instantiate Internet Explorer (.NET default browser). For compatibility issues, I need to embed Chromium. I've decided to go with CefSharp.

I've set up a test project in C# calling this dll & it's working fine. I've read CefSharp Wiki and they precise to initialize/shutdown Cef from the main UI thread. So I declare two methods in C# that I call directly in VB6. Issue is: the form is loading fine and browser with CefSharp is running. When I close the form and want to launch it again, application just shuts down. I've also tried to intialize/shutdown CefSharp in the C# dll with no success.

Any idea on how to correctly use CefSharp in this case? Thanks

Public gTest as boolean
Option Explicit

Public Function LaunchDLL() As Boolean

Dim oExtApp: Set oExtApp = CreateObject("TestCefSharp.Class2")
Dim test As Boolean

Dim bResult     As Boolean

    LaunchDLL = False
    On Error GoTo Erreur

    If gTest = False Then
        oExtApp.InitializeChromium
        gTest = True
    End If
    LaunchDLL = oExtApp.LaunchingForm

    Set oExtApp = Nothing

    Exit Function

Erreur:
MsgBox Err.Description
End Function

Public Sub LaunchBrowser_Click()
Dim test As Boolean
Dim Chrome As Boolean

test = LaunchDLL()
End Sub

namespace TestCefSharp
{
    public interface IExternalApp
    {
        bool LaunchingForm();

        bool InitializeChromium();

        bool ShutdownChromium();
    }
    public abstract class CExternalApp : IExternalApp
    {

        public CExternalApp()
            : base()
        {

        }

        public bool LaunchingForm()
        {
            bool test = false;
            MessageBox.Show("Starting");
            Form1 form1 = new Form1();
            DialogResult result = form1.ShowDialog();
            if (result == DialogResult.OK)
            { test = true; }
            else
            { test = false; }
            return test ;
        }

        public bool InitializeChromium()
        {
            MessageBox.Show("Chromium initialized");
            bool result = false;
            try
            {
                CefSharpSettings.SubprocessExitIfParentProcessClosed = true;
                CefSharpSettings.ShutdownOnExit = false;
                Cef.EnableHighDPISupport();

                var settings = new CefSettings()
                { CachePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CefSharp\\Cache")};

                Cef.Initialize(settings);
                result = true;
            }
            catch
            { }
            return result;
        }

        public bool ShutdownChromium()
        {
            MessageBox.Show("Chromium ended");
            bool result = false;
            try
            {
                Cef.Shutdown();
                result = true;
            }
            catch
            { }
            return result;
        }
    }
}
1
As per github.com/cefsharp/CefSharp/wiki/… you can only initialize and shutdown once per process. - amaitland
Put code into a class which will make it a process. Each time you need to run code call the class constructor. - jdweng
@amaitland If I understand correctly, I only need to call once in VB6 the Cef.initialize function and then instantiate a ChromiumBrowser each time I need one? Even doing like this, I'm not able to launch a second ChromiumBrowser after closing the first one. - beguy
Yes, call Cef.Initialize a single time before creating a ChromiumWebBrowser instance, then you can create as many instances as required, then call Cef.Shutdown before your application exits. The default behaviour will be to hook the relevant WinForms event to call Cef.Shutdown as it's important, you might need to disable this cefsharp.github.io/api/73.1.x/html/… - amaitland
VB6 is not a supported platform, so there are no guarantees any of this works. If you require further assistance you will need to provide more detail to your example. - amaitland

1 Answers

0
votes

In fact, it's not working with the debug mode from VB6 editor... If I compile and launch directly the executable, all is working well.

I'll have to dig in a bit further but I think it comes from the fact that VB6 editor does not launch a separate thread in debug like VS2017 does in .NET.

Anyway, thanks for the help.