mirror of
https://github.com/ElectronNET/Electron.NET.git
synced 2026-02-09 21:24:55 +00:00
Page:
HostHook
Pages
ASP.Net
About
Advanced Migration Topics
App
AutoUpdater
Clipboard
Configuration
Console App
Custom_main
Debugging
Dialog
Dock
GlobalShortcut
Home
HostHook
IpcMain
Menu
Migration Checks
Migration Guide
NativeTheme
Notification
Overview
Package Building
Package Description
PowerMonitor
Screen
Shell
Startup Methods
System Requirements
Tray
WebContents
What's New
WindowManager
Clone
1
HostHook
github-actions[bot] edited this page 2025-10-31 16:53:39 +00:00
Execute native JavaScript/TypeScript code from the host process.
Overview
The Electron.HostHook API allows you to execute native JavaScript/TypeScript code from the host process. This enables advanced integration scenarios where you need to run custom JavaScript code or access Node.js APIs directly.
Methods
🧊 void Call(string socketEventName, params dynamic[] arguments)
Execute native JavaScript/TypeScript code synchronously.
Parameters:
socketEventName- Socket name registered on the hostarguments- Optional parameters
🧊 Task<T> CallAsync<T>(string socketEventName, params dynamic[] arguments)
Execute native JavaScript/TypeScript code asynchronously with type-safe return values.
Parameters:
T- Expected return typesocketEventName- Socket name registered on the hostarguments- Optional parameters
Returns:
Task with the result from the executed host code.
Usage Examples
Basic Host Hook Execution
// Execute simple JavaScript function
Electron.HostHook.Call("myFunction", "parameter1", 42);
// Execute with callback-style result
var result = await Electron.HostHook.CallAsync<string>("getUserName", userId);
Console.WriteLine($"User name: {result}");
Advanced Integration
// Call custom Electron API
var fileContent = await Electron.HostHook.CallAsync<string>("readFile", "config.json");
Console.WriteLine($"Config: {fileContent}");
// Execute with multiple parameters
var processedData = await Electron.HostHook.CallAsync<object[]>("processData", rawData, options);
// Call with complex objects
var settings = new { theme = "dark", language = "en" };
var updatedSettings = await Electron.HostHook.CallAsync<object>("updateSettings", settings);
Error Handling
try
{
// Execute host function with error handling
var result = await Electron.HostHook.CallAsync<string>("riskyOperation", inputData);
Console.WriteLine($"Success: {result}");
}
catch (Exception ex)
{
// Handle execution errors
Console.WriteLine($"Host hook error: {ex.Message}");
Electron.Dialog.ShowErrorBox("Operation Failed", "Could not execute host function.");
}
Type-Safe Operations
// Strongly typed return values
var userInfo = await Electron.HostHook.CallAsync<UserInfo>("getUserInfo", userId);
Console.WriteLine($"User: {userInfo.Name}, Email: {userInfo.Email}");
// Array results
var fileList = await Electron.HostHook.CallAsync<string[]>("listFiles", directoryPath);
foreach (var file in fileList)
{
Console.WriteLine($"File: {file}");
}
// Complex object results
var systemStats = await Electron.HostHook.CallAsync<SystemStatistics>("getSystemStats");
Console.WriteLine($"CPU: {systemStats.CpuUsage}%, Memory: {systemStats.MemoryUsage}%");
Custom ElectronHostHook Setup
// In your ElectronHostHook/index.ts
import { app } from 'electron';
export function getAppVersion(): string {
return app.getVersion();
}
export async function readConfigFile(): Promise<string> {
const fs = require('fs').promises;
return await fs.readFile('config.json', 'utf8');
}
export function customNotification(message: string): void {
// Custom notification logic
console.log(`Custom notification: ${message}`);
}
Integration with .NET Code
// Use host hook in your application logic
public async Task<string> GetApplicationVersion()
{
return await Electron.HostHook.CallAsync<string>("getAppVersion");
}
public async Task LoadConfiguration()
{
try
{
var config = await Electron.HostHook.CallAsync<ConfigObject>("readConfigFile");
ApplyConfiguration(config);
}
catch (Exception ex)
{
Console.WriteLine($"Failed to load config: {ex.Message}");
UseDefaultConfiguration();
}
}
public void ShowCustomNotification(string message)
{
Electron.HostHook.Call("customNotification", message);
}
Related APIs
- Electron.IpcMain - Inter-process communication
- Electron.App - Application lifecycle events
- Electron.WebContents - Web content integration
Additional Resources
- Host Hook Documentation - Setting up custom host hooks
- API Overview

- Electron.App

- Electron.Dialog

- Electron.Menu

- Electron.WindowManager

- Electron.Shell

- Electron.Screen

- Electron.Notification

- Electron.Tray

- Electron.Clipboard

- Electron.Dock

- Electron.HostHook

- Electron.IpcMain

- Electron.GlobalShortcut

- Electron.AutoUpdater

- Electron.NativeTheme

- Electron.PowerMonitor

Want to contribute to this documentation? Please fork and create a PR! The Wiki is autogenerated from the /docs content in the repository.