How SharpEmu Integrates with the Windows Host Platform: Native API Implementation

SharpEmu leverages Windows-specific APIs including Registry queries, Thread-Local Storage, Vectored Exception Handling, and Virtual Memory management to enable high-performance Xbox 360 emulation on Windows hosts.

SharpEmu is a cross-platform Xbox 360 emulator written in C# that implements deep Windows host platform integration through P/Invoke calls to native system libraries. When running on Windows, the emulator activates platform-specific code paths in SharpEmu.Core/Runtime/SharpEmuRuntime.cs that handle hardware detection, JIT compilation support, and resource management while maintaining portability for Linux and macOS targets.

Hardware Detection and System Enumeration

SharpEmu queries the host Windows system for diagnostic logging and compatibility checks by accessing the Windows Registry and native APIs.

CPU and Memory Detection via Registry

In SharpEmu.Logging/HostSystemInfo.cs, the emulator retrieves CPU identification by reading the Windows Registry key HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor\0. For system memory statistics, it calls GlobalMemoryStatusEx from kernel32.dll to determine total RAM available on the host.

// Produces something like:
// "Host hardware: CPU: 12th Gen Intel(R) Core(TM) i7-12700K; GPU: NVIDIA GeForce RTX 3080; RAM: 32,768 MB (32.0 GB)."
string hardwareInfo = SharpEmu.Logging.HostSystemInfo.Summary;
Console.WriteLine(hardwareInfo);

Graphics Adapter Enumeration

The same HostSystemInfo.cs file uses EnumDisplayDevices from user32.dll to enumerate installed graphics adapters and retrieve GPU names. This allows SharpEmu to log the exact graphics hardware present when generating diagnostic reports.

JIT Backend Integration and Exception Handling

The direct-execution JIT compiler requires low-level Windows services to manage per-thread state and handle runtime faults generated by emulated guest code.

Thread-Local Storage for the Direct Execution Backend

In SharpEmu.Core/Cpu/Native/DirectExecutionBackend.cs, SharpEmu manages thread-local storage using the Windows TLS API. It imports TlsAlloc, TlsFree, TlsSetValue, and TlsGetValue from kernel32.dll to store per-thread JIT state without interfering with other threads.

Vectored Exception Handling for Guest Code

To catch access violations and other faults generated by JIT-compiled guest code, SharpEmu sets up a vectored exception handler by importing AddVectoredExceptionHandler, RemoveVectoredExceptionHandler, and SetUnhandledExceptionFilter from kernel32.dll. This mechanism, also implemented in DirectExecutionBackend.cs, allows the emulator to intercept and translate hardware exceptions into emulated signals.

Process Isolation and Resource Management

SharpEmu leverages Windows Job Objects to isolate the emulator process and limit resource consumption.

In SharpEmu.GUI/EmulatorProcess.cs, the code imports CreateJobObjectW, CreateProcessW, AssignProcessToJobObject, and SetInformationJobObject from kernel32.dll. These functions create a sandboxed environment that restricts CPU time and memory usage when launching Xbox 360 executable files (XEX).

var procInfo = SharpEmu.GUI.EmulatorProcess.Start(
    exePath: @"C:\Games\Xbox360\MyGame.xex",
    args:    "",
    workingDirectory: @"C:\Games\Xbox360"
);
// The process now runs inside a Windows Job that limits CPU time and memory.

Audio Output Implementation

For Windows hosts, SharpEmu implements audio playback through the legacy Windows Multimedia API.

In SharpEmu.GUI/SndPreviewPlayer.cs, the code calls waveOutOpen and waveOutWrite from winmm.dll to stream PCM audio data to the host sound system. This provides low-latency audio output specifically for the Windows platform.

Memory Management for Host Buffers

The emulator requires fine-grained control over memory allocation and protection for JIT-generated code and guest memory mappings.

In SharpEmu.HLE/HostMemory.cs, SharpEmu wraps VirtualAlloc, VirtualFree, VirtualProtect, VirtualQuery, and FlushInstructionCache from kernel32.dll. These functions allocate executable memory regions and manage protection flags required for the dynamic recompiler.

IntPtr execBuf = SharpEmu.HLE.HostMemory.VirtualAlloc(
    IntPtr.Zero,
    size: 0x2000,
    allocationType: AllocationType.Commit | AllocationType.Reserve,
    protect: MemoryProtection.ExecuteReadWrite);

Summary

  • SharpEmu integrates with the Windows host platform through P/Invoke calls to kernel32.dll, user32.dll, and winmm.dll for hardware detection, process management, and memory operations.
  • Hardware detection relies on Registry queries and EnumDisplayDevices to log CPU, GPU, and RAM information in HostSystemInfo.cs.
  • JIT compilation uses Thread-Local Storage and Vectored Exception Handling from DirectExecutionBackend.cs to manage execution state and catch guest faults.
  • Process isolation is implemented via Windows Job Objects in EmulatorProcess.cs to sandbox the emulator runtime.
  • Memory management uses VirtualAlloc and VirtualProtect in HostMemory.cs to allocate executable buffers for the JIT backend.
  • Platform detection occurs at runtime using OperatingSystem.IsWindows() to activate these Windows-specific code paths while keeping the core emulator cross-platform.

Frequently Asked Questions

How does SharpEmu detect the host hardware on Windows?

SharpEmu queries the Windows Registry at HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor\0 to read CPU identifiers and calls GlobalMemoryStatusEx from kernel32.dll to obtain total system RAM. For graphics hardware, it uses EnumDisplayDevices from user32.dll to enumerate GPUs, all implemented in SharpEmu.Logging/HostSystemInfo.cs.

What Windows APIs does SharpEmu use for JIT compilation support?

The emulator uses TlsAlloc, TlsSetValue, and TlsGetValue for thread-local storage management, and AddVectoredExceptionHandler to catch access violations generated by guest code. These functions are imported in SharpEmu.Core/Cpu/Native/DirectExecutionBackend.cs to support the direct-execution JIT backend.

Why does SharpEmu use Windows Job Objects for process management?

Job Objects provide resource isolation and limits. By calling CreateJobObjectW and AssignProcessToJobObject in SharpEmu.GUI/EmulatorProcess.cs, SharpEmu can constrain CPU time and memory usage for the emulated process, preventing the guest Xbox 360 code from consuming excessive host resources.

How does SharpEmu allocate executable memory for the JIT on Windows?

Through SharpEmu.HLE/HostMemory.cs, the emulator calls VirtualAlloc with executable protection flags and FlushInstructionCache to ensure processor coherence. This allows the JIT compiler to generate and execute native x86-64 code within the emulator's address space.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →