简体   繁体   English

在VB6应用程序中将C#dll与Cefsharp一起使用

[英]Using C# dll with Cefsharp in VB6 application

I'm updating a program in vb6 calling a Winform C# dll through COM interop. 我正在更新vb6中通过COM互操作调用Winform C#dll的程序。 In this C# dll, the winform contain a webbrowser that instantiate Internet Explorer (.NET default browser). 在此C#dll中,winform包含一个实例化Internet Explorer(.NET默认浏览器)的Web浏览器。 For compatibility issues, I need to embed Chromium. 对于兼容性问题,我需要嵌入Chromium。 I've decided to go with CefSharp. 我决定选择CefSharp。

I've set up a test project in C# calling this dll & it's working fine. 我已经在C#中建立了一个名为dll的测试项目,并且工作正常。 I've read CefSharp Wiki and they precise to initialize/shutdown Cef from the main UI thread. 我已经阅读了CefSharp Wiki,它们精确地从主UI线程初始化/关闭Cef。 So I declare two methods in C# that I call directly in VB6. 因此,我在C#中声明了两个在VB6中直接调用的方法。 Issue is: the form is loading fine and browser with CefSharp is running. 问题是:表单可以很好地加载并且带有CefSharp的浏览器正在运行。 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. 我也尝试在C#dll中初始化/关闭CefSharp,但没有成功。

Any idea on how to correctly use CefSharp in this case? 关于如何在这种情况下正确使用Ce​​fSharp的任何想法? 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;
        }
    }
}

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. 实际上,它不能与VB6编辑器中的调试模式一起使用...如果我直接编译并启动可执行文件,则一切运行良好。

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. 我将不得不进一步挖掘,但是我认为这是由于VB6编辑器没有像VS2017在.NET中那样在调试中启动单独的线程这一事实。

Anyway, thanks for the help. 无论如何,感谢您的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM