Embed in a Windows app
Build BakeNative on Windows, show a bundle in an existing Win32 app, and ship updates.
On Windows, a BakeNative screen is BakeNativeView, a child window (HWND). You put it in any Win32 window, and the rest of your app stays native. WinUI 3 and WPF apps can host it through their HWND interop.
The Windows host is a Rust library, bake-host-windows. Your app links it as a Cargo dependency. There is no C, C++ or C# API yet.
Try it without building BakeNative
When the scoped renderer and matching Windows toolchain are released, install @bake-native/react and run bake create my-app. In the generated app, pnpm exec bake dev --target web,windows opens the Windows host. Its platform package supplies hermesc, the snapshot baker and bakenative-app.exe. See Install from npm.
To put the app in your own Win32 program, you build the host from source, as the rest of this page shows.
Requirements
| Windows 10 or 11, x64 | Builds and runs the host |
| Visual Studio 2022 with Desktop development with C++ | MSVC (cl, link) and the Windows SDK |
| cmake, ninja, git | Build Hermes |
| Python 3 | Hermes' CMake. The python.exe alias that opens the Microsoft Store does not work |
| Rust 1.85 or newer (MSVC toolchain) | The host and the native tools are edition 2024 crates. Run rustup update stable if rustc --version is older |
| Node 22.22.2+ and pnpm 10 | Runs the bake CLI |
Build the toolchain
Clone and install
git clone https://github.com/gronxb/bake-native.git bakenative
cd bakenative
corepack pnpm installBuild Hermes
corepack pnpm hermesOn Windows, this runs tools/build-hermes/windows.ps1. It writes hermesvm.dll, hermesc.exe and bakenative_hermes.dll to native/hermes/artifacts/windows/x64, and runs a smoke test that ends with SMOKE OK.
The script needs the MSVC environment. If cl is not on PATH, it uses the newest Visual Studio's x64 developer environment. To use another environment script, set $env:BAKE_MSVC_ENV to it (a .ps1 or .bat). Set $env:BAKE_PYTHON if Python 3 is not on PATH.
Check it works
corepack pnpm windowsThis builds the example app (examples/app) for Windows, builds the example host (examples/windows), copies both into examples/windows/dist/BakeNative and runs bakenative-app.exe: a Win32 window with a native header and an BakeNativeView below it.
Build a bundle
In your BakeNative app (see Installation for adding the CLI):
pnpm bake build --target windows --bakeThis writes dist/windows:
| File | Content |
|---|---|
app.js | The JavaScript that app.hbc was compiled from. The app does not need it at run time |
app.hbc | Hermes bytecode |
app.baked | First-screen snapshot (--bake) |
app.<tag>.baked | First-screen snapshot for each additional locale |
app.css | Styles |
manifest.json | Host protocol, renderer batch digest and a SHA-256 per file |
assets/ | Images, when the app has any |
The first bake build in a checkout compiles the native tools (bake-css-lint, bake-frame) with Cargo. It prints nothing while it does, and it takes a few minutes.
Windows Hermes is built without Intl. bake build fails when your code uses Intl or passes locales to toLocaleString, toLocaleDateString, toLocaleTimeString or localeCompare. typeof Intl checks are allowed.
The host resolves the app locale from the saved app choice and the user's preferred Windows UI languages. setLocale(tag) saves the choice in %LOCALAPPDATA%\BakeNative\<exe name>\settings.json. Call setLocale(null) to clear it. Both take effect the next time the process starts. Windows builds reject right-to-left locales in locales.
Embed in a Win32 app
Add the host to your app's Cargo.toml. The paths point at your BakeNative checkout:
[dependencies]
bake-host-windows = { path = "../bakenative/native/rust/crates/bake-host-windows", features = ["hermes-dlopen"] }
windows = { version = "=0.62.2", features = [
"Win32_Foundation",
"Win32_Graphics_Gdi",
"Win32_System_LibraryLoader",
"Win32_UI_WindowsAndMessaging",
] }
[patch.crates-io]
taffy = { path = "../bakenative/native/rust/vendor/taffy" }hermes-dlopen loads Hermes on the JavaScript thread at run time, so your process starts without Hermes' DLLs. The taffy patch is required: the layout engine uses a patched copy.
Then create the view as a child of your window, resize it with the window, and call shutdown before the process exits:
use std::cell::RefCell;
use std::time::Duration;
use bake_host_windows::{BakeNativeView, ViewOptions, enable_per_monitor_dpi};
use windows::Win32::Foundation::*;
use windows::Win32::UI::WindowsAndMessaging::*;
thread_local! {
static VIEW: RefCell<Option<BakeNativeView>> = const { RefCell::new(None) };
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
bake_host_windows::prestart(); // creates the rendering devices on a worker
enable_per_monitor_dpi(); // or declare PerMonitorV2 in the app manifest
let hwnd = create_main_window()?; // your window, with WS_CLIPCHILDREN
let bundle = std::env::current_exe().unwrap().with_file_name("bundle");
let mut options = ViewOptions::new(bundle.join("app.hbc"));
options.frame = bake_host_windows::first_screen_frame(&options.hbc);
let mut rc = RECT::default();
unsafe { GetClientRect(hwnd, &mut rc)? };
let view = BakeNativeView::create(hwnd, rc, options)?;
VIEW.with(|v| *v.borrow_mut() = Some(view));
unsafe {
let _ = ShowWindow(hwnd, SW_SHOW);
let mut msg = MSG::default();
while GetMessageW(&mut msg, None, 0, 0).as_bool() {
let _ = TranslateMessage(&msg);
DispatchMessageW(&msg);
}
}
bake_host_windows::shutdown(Duration::from_secs(10));
Ok(())
}
// In your window procedure:
unsafe extern "system" fn wnd_proc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT {
match msg {
WM_SIZE => {
let mut rc = RECT::default();
unsafe {
let _ = GetClientRect(hwnd, &mut rc);
}
VIEW.with(|v| {
if let Some(view) = v.borrow().as_ref() {
view.set_bounds(rc);
}
});
LRESULT(0)
}
WM_DESTROY => {
VIEW.with(|v| v.borrow_mut().take());
unsafe { PostQuitMessage(0) };
LRESULT(0)
}
_ => unsafe { DefWindowProcW(hwnd, msg, wparam, lparam) },
}
}- The first frame. With
options.frame,BakeNativeView::createdraws the snapshot before it returns, while Hermes loads the bundle. JavaScript then takes over the same elements. Without a snapshot, the first frame waits for JavaScript. - Lifetime. The view lives as long as its
HWND. It is destroyed with its parent window. To remove it earlier, callDestroyWindow(view.hwnd()). - The thread. Create the view on a thread with a normal message loop. The view posts its updates to that thread's queue. The thread must be a single-threaded COM apartment (STA), as Windows UI threads are. If the thread has not initialized COM, the view initializes an STA and releases it with the thread's last view. On an MTA thread
BakeNativeView::createfails withthe UI thread must be a single-threaded COM apartment…: callCoInitializeEx(None, COINIT_APARTMENTTHREADED)on the thread before you create views. - Shutdown.
bake_host_windows::shutdownwaits for every closed view's JavaScript thread. Call it after the last view's window is destroyed and before the process exits. Without it, the process can crash while Hermes shuts down. - Size, DPI and dark mode. The view lays the snapshot out at its real size. It handles per-monitor DPI changes, and follows the system's app mode (light or dark).
view.set_dark(true)forces a scheme. - Native modules.
bake buildgenerates thebakenative-app-modulescrate: the contract of every app module the bundle calls (a<Name>Spectrait) andbundle_modules::root(...), one parameter per module the host implements. Register the crate's own modules and the screen's:for m in bakenative_app_modules::modules().into_iter().chain(bakenative_app_modules::bundle_modules::root(GreetingModule { screen })) { options.modules.register_arc(m) }. See Native modules. - One section of a window.
options.fits_content = truemakes the page as tall as its content:options.on_content_heightreports the height in DIPs (on the UI thread), and you size the view's window to it. The root does not scroll; wheel input it does not use goes to the parent window. The page is laid out at a 1 DIP viewport height:vhunits,position: fixedand virtual lists do not apply.
examples/windows/src/host.rs is a complete host: a header with a native button, the view below it, dark mode, focus and DPI handling.
Ship the app
Put the Hermes DLLs next to your exe, and the bundle in a folder:
MyApp\
my-app.exe
bakenative_hermes.dll native/hermes/artifacts/windows/x64
hermesvm.dll native/hermes/artifacts/windows/x64
bundle\app.hbc
bundle\app.baked
bundle\app.css
bundle\manifest.jsonThe DLLs link the Visual C++ runtime dynamically. Machines with the Visual C++ 2015-2022 redistributable have it. Otherwise, copy vcruntime140.dll, vcruntime140_1.dll and msvcp140.dll next to the exe.
corepack pnpm windows -- --no-run --package builds this layout for the example and zips it to examples/windows/dist/BakeNative-windows-x64.zip. examples/windows/README.md covers code signing and MSIX.
Ship updates
A bundle is a plain folder, so you can replace it without a new app release. Ship the whole dist/windows folder every time. The files belong together: see Shipping updates.
Check before you launch
A bundle built by a newer bake may need a newer app. Check the downloaded bundle before you open it, and fall back to the one you shipped:
let downloaded = update_dir.join("app.hbc");
let hbc = match bake_host_windows::bundle_refusal(&downloaded) {
None => downloaded,
Some(reason) => {
eprintln!("skipping update: {reason}");
shipped_dir.join("app.hbc")
}
};bundle_refusal reads the manifest.json next to app.hbc. It returns why this app cannot run the bundle, or None:
| Reason | Meaning |
|---|---|
…manifest.json is missing | The folder has no manifest |
…manifest.json is not JSON | The manifest cannot be parsed. A UTF-8 byte order mark counts as invalid |
bundle host protocol N > supported M | Built by a CLI that speaks a newer protocol than this app's BakeNative |
app.hbc writes renderer batch stream …, this runtime reads … | Built by a bake from another BakeNative version. Rebuild the bundle with the bake that matches the app |
manifest.json has no batchDigest … | Built by an older bake. Rebuild the bundle |
BakeNativeView::create runs the same check before anything is drawn. It returns an error such as bake_root_open failed (Unsupported -6): incompatible bundle: bundle host protocol 99 > supported 3.
Replace the bundle
The view reads the bundle when it is created. To show a new bundle, write it to a new folder, check it, then destroy the old view and create a new one with the new paths. Do not overwrite the files of a bundle that a view is running.
The view never adopts a snapshot that was baked from a different app.hbc. If the files get mixed up, JavaScript renders the screen again from scratch. On Windows, the wrong snapshot is still the first frame until JavaScript replaces it, so always replace the whole folder.
Prepare a screen before it opens
If your app opens the BakeNative screen later, it can prepare the first screen earlier, for example when the pointer enters the button that opens it. BakeNativeView::prewarm reads and lays out the snapshot on worker threads. It does not start Hermes. BakeNativeView::create then uses the prepared result if the size, DPI and color scheme still match, and lays out again if they do not. examples/windows shows this with bakenative-app.exe --open-later.
Accessibility
The host exposes the core's semantic tree to UI Automation, so Narrator and other screen readers
read it as they read a browser: roles (buttons, links, headings, lists, tabs, menus, dialogs,
tables), names, states and values, with the UIA patterns that go with them (Invoke, Toggle,
SelectionItem, ExpandCollapse, Value, RangeValue), positions in a set, and aria-controls as
ControllerFor. Focus follows the core's focus, and aria-activedescendant moves it to the
descendant. The roles each platform maps to are in native/rust/schema/README.md
("Accessibility").
Native modules
Windows modules are written in Rust with bake_runtime::modules. Pass them in ViewOptions::modules. See Native modules.
Not yet
- A C, C++ or C# API, and a NuGet or MSIX package.
- Buttons and form controls are drawn by the host in the Windows 11 style, not real Win32 or WinUI controls. Text fields are real
EDITcontrols, so content drawn over a field does not cover it. - Touch-screen panning, scroll momentum and overscroll.
See also
- Embed in an Android app: the same steps for the Android host.
- Native UI and Universal UI: components that draw on Windows (CSS) as on the web.