cefsharp/CefSharp · error · Exception

IBrowserHost is null, you've likely call this method before

Error message

IBrowserHost is null, you've likely call this method before the underlying browser has been created.

What it means

Thrown by ChromiumRenderWidgetHandleFinder.TryFindHandle(IWebBrowser, ...) when GetBrowserHost() returns null, i.e. the underlying CEF browser/host has not yet been created. The helper needs the native HWND (obtained via IBrowserHost.GetWindowHandle) to enumerate child windows for the render widget, so a missing host makes the lookup impossible. The XML doc warns Chromium's message-loop window is created asynchronously, so the host may be null early in the lifecycle.

Source

Thrown at CefSharp.WinForms/Experimental/ChromiumRenderWidgetHandleFinder.cs:49

        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr lParam);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

        /// <summary>
        /// Chromium's message-loop Window isn't created synchronously, so this may not find it.
        /// If so, you need to wait and try again later.
        /// </summary>
        /// <param name="chromiumWebBrowser">ChromiumWebBrowser instance</param>
        /// <param name="chromerRenderWidgetHostHandle">Handle of the child HWND with the name <see cref="ChromeRenderWidgetHostClassName"/></param>
        /// <returns>returns true if the HWND was found otherwise false.</returns>
        public static bool TryFindHandle(IWebBrowser chromiumWebBrowser, out IntPtr chromerRenderWidgetHostHandle)
        {
            var host = chromiumWebBrowser.GetBrowserHost();
            if (host == null)
            {
                throw new Exception("IBrowserHost is null, you've likely call this method before the underlying browser has been created.");
            }

            var hwnd = host.GetWindowHandle();

            return TryFindHandle(hwnd, ChromeRenderWidgetHostClassName, out chromerRenderWidgetHostHandle);
        }

        /// <summary>
        /// Chromium's message-loop Window isn't created synchronously, so this may not find it.
        /// If so, you need to wait and try again later.
        /// </summary>
        /// <param name="browser">IBrowser instance</param>
        /// <param name="chromerRenderWidgetHostHandle">Handle of the child HWND with the name <see cref="ChromeRenderWidgetHostClassName"/></param>
        /// <returns>returns true if the HWND was found otherwise false.</returns>
        public static bool TryFindHandle(IBrowser browser, out IntPtr chromerRenderWidgetHostHandle)
        {
            var host = browser.GetHost();
            if (host == null)

View on GitHub (pinned to 16bc6e0711)

Solutions

  1. Wait until the browser is initialized (subscribe to IsBrowserInitializedChanged / await initialization) before calling TryFindHandle.
  2. Guard with `if (browser.GetBrowserHost() != null)` and retry on a timer if null, as the XML doc suggests.
  3. Use the IBrowser overload (TryFindHandle(IBrowser,...)) from within a handler that already has a valid IBrowser, e.g. LoadingStateChanged or RenderProcessMessageHandler.
  4. Return false / defer rather than throwing by checking the host yourself before delegating.

Example fix

// before
var found = ChromiumRenderWidgetHandleFinder.TryFindHandle(browser, out var h); // throws if not init

// after
browser.IsBrowserInitializedChanged += (s, e) =>
{
    if (browser.IsBrowserInitialized && browser.GetBrowserHost() != null)
    {
        ChromiumRenderWidgetHandleFinder.TryFindHandle(browser, out var h);
    }
};
Defensive patterns

Strategy: validation

Validate before calling

if (browser.GetBrowserHost() is { } host)
{
    ChromiumRenderWidgetHandleFinder.TryFindHandle(browser, out var h);
}

Type guard

static bool IsHostReady(IWebBrowser b) => b.GetBrowserHost() != null;

Prevention

When it happens

Trigger: Calling TryFindHandle in the constructor, Form.Load, or right after Controls.Add before the browser finished initializing; calling it before IsBrowserInitialized became true; calling during disposal.

Common situations: Subclassing input handling that needs the render HWND and wiring it up too early; invoking immediately after setting the Address; racing the async CEF creation.

Related errors


AI-assisted analysis of cefsharp/CefSharp@16bc6e0711 (2026-08-13). Data as JSON: /api/errors/b8d3423ab3470500. Report an issue: GitHub.