0

I'm having a rough go of it getting started. First, I tried using the NuGet package(s). When I installed them none of the necessary references to the CefSharp DLLs showed up in my References list. So I added them by hand by searching the /packages sub-directory. After getting my project to build, I discovered that when I executed a URL load nothing happened. No web page loaded, no error messages, no exceptions triggered. Nothing.

I removed the CefSharp Nuget package(s) completely and pulled down the latest repo source from GitHub. I first made sure that the URL load worked fine from the CefSharp.WinForms.Example project. It worked great. I then copied the necessary set of projects from the CefSharp solution into my project's solution until I had my solution build properly. I added the BrowserForm form from the CefSharp.WinForms.Example project to make sure it wasn't my code. However, when I open that form at run-time, enter a URL into the address bar, and click the Go button nothing happens. Just like before.

I traced out the code and found the failure point in this C++ method in ManagedCefBrowserAdapter.h:

    void LoadUrl(String^ address)
    {
        _address = address;
        auto cefFrame = _renderClientAdapter->TryGetCefMainFrame();

        // This is where cefFrame is treated qual to nullptr despite 
        //  having the value of {CefRefPtr<CefFrame>}, thereby defeating
        //  the call to LoadURL().
        if (cefFrame != nullptr)
        {
            cefFrame->LoadURL(StringUtils::ToNative(address));
        }
    }

It turns out that there's a really strange problem. When I trace the code up to the "if" statement I see that value of cefFrame is:

{CefRefPtr<CefFrame>}

However, it apparently is not NULL and yet it is being teated as if it is. (Or my analysis of the {CefRefPtr} value is wrong and so is my analysis.)

In either case the "if" statement treats the current value of cefFrame as equal to nullptr and skips over the line that calls the LoadURL() method on the cefFrame referenced object. I trace this exact same code while in my copy of the GitHub CefSharp repo's solution and it does enter the "if" block and execute the LoadURL() method. I checked the value of cefFrame and it is the same as the value I have in my app. For some truly odd reason either there's a more insidious problem here or in my app, or it really does treat cefFrame's value of {CefRefPtr} the same as nullptr's when it shouldn't.

I would really appreciate it if anyone has any idea on how I can get things working in my code.

UPDATE: I triple-checked and the Build Configurations are identical between the GitHub CefSharp solution that does work and my project that doesn't. Everything is set to x86 except for the Core project which are set to Win32 (in both case).

I just did a rebuild and on top of that I went through each included project and manually deleted both the /bin and /obj directories completely. Now when I run the project something different is happening. Whenever I the code attempts to create a new BrowserTabUserControl, I get an Unhandled Exception:

System.IO.FileNotFoundException was unhandled
  _HResult=-2147024770
  _message=Could not load file or assembly 'CefSharp.Core.dll' or one of its dependencies. The specified module could not be found.
  HResult=-2147024770
  IsTransient=false
  Message=Could not load file or assembly 'CefSharp.Core.dll' or one of its dependencies. The specified module could not be found.
  Source=HiveMind3D_proto
  FileName=CefSharp.Core.dll
  FusionLog=""
  StackTrace:
       at HiveMind3D_proto.BrowserTabUserControl..ctor(String url)
       at HiveMind3D_proto.BrowserForm.AddTab(String url, Nullable`1 insertIndex) in c:\Users\Robert\Documents\Visual Studio 2012\Projects\Windows Forms\HiveMind3D_proto\HiveMind3D_proto\BrowserForm.cs:line 35
       at HiveMind3D_proto.BrowserForm..ctor() in c:\Users\Robert\Documents\Visual Studio 2012\Projects\Windows Forms\HiveMind3D_proto\HiveMind3D_proto\BrowserForm.cs:line 24
       at HiveMind3D_proto.Program.Main() in c:\Users\Robert\Documents\Visual Studio 2012\Projects\Windows Forms\HiveMind3D_proto\HiveMind3D_proto\Program.cs:line 26
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

I tried removing the CefSharp.Core project reference in the main project and re-adding it by re-adding a reference to the CefSharp.Core project. But I still get that Exception.

Robert Oschler
  • 14,153
  • 18
  • 94
  • 227

1 Answers1

0

Ok, I'm running now. Albeit with the full source instead of the NuGet package. I turned out I needed to do a few things:

As per the FAQ:

https://github.com/cefsharp/CefSharp/wiki/Frequently-asked-questions#Runtime_dependencies

  • Make sure ALL projects were set to build and to build as x86 projects
  • Copy over the following files into both the /bin/Debug and /bin/Release directories. It is not enough to simply reference them in your main app:

    • libcef.dll

    • icudtl.dat

    • CefSharp.dll

    • CefSharp.WinForms.dll

  • Initialize the Cef library using code such as that shown below in a method arbitrarily called Init(). Make sure you call your Init() method as the very first thing in your Main() method in Program.cs:

    public static void Init()
    {
        var settings = new CefSettings
        {
            LogSeverity = LogSeverity.Verbose,
            LogFile = "debug.log",
            RemoteDebuggingPort = 8088,
        };
    
        if (DebuggingSubProcess)
        {
            var architecture = Environment.Is64BitProcess ? "x64" : "x86";
            settings.BrowserSubprocessPath = "..\\..\\..\\..\\CefSharp.BrowserSubprocess\\bin\\" + architecture + "\\Debug\\CefSharp.BrowserSubprocess.exe";
        }
    
        settings.RegisterScheme(new CefCustomScheme
        {
            SchemeName = CefSharpSchemeHandlerFactory.SchemeName,
            SchemeHandlerFactory = new CefSharpSchemeHandlerFactory()
        });
    
        if (!Cef.Initialize(settings))
        {
            if (Environment.GetCommandLineArgs().Contains("--type=renderer"))
            {
                Environment.Exit(0);
            }
            else
            {
                return;
            }
        }
    }
    

Note, it's a good idea to set your log file verbosity to Verbose so you see any errors in the log file. They can be a lifesaver.

Robert Oschler
  • 14,153
  • 18
  • 94
  • 227
  • When you say "Make sure ALL projects were set to build and to build as x86 projects" do you mean all projects that reference cefsharp? Or do you mean all projects in the solution? I have a solution with ~150 projects and I cannot randomly change them all to x86. Also the name of the "Active solution platform" is just a name. If I call it "x86CefSharp" but set the project platforms to x86 should this work? – James Gardner Feb 22 '16 at 21:45