You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
github-actions[bot] edited this page Oct 31, 2025
·
1 revision
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.
// Call custom Electron APIvarfileContent=awaitElectron.HostHook.CallAsync<string>("readFile","config.json");Console.WriteLine($"Config: {fileContent}");// Execute with multiple parametersvarprocessedData=awaitElectron.HostHook.CallAsync<object[]>("processData",rawData,options);// Call with complex objectsvarsettings=new{theme="dark",language="en"};varupdatedSettings=awaitElectron.HostHook.CallAsync<object>("updateSettings",settings);
Error Handling
try{// Execute host function with error handlingvarresult=awaitElectron.HostHook.CallAsync<string>("riskyOperation",inputData);Console.WriteLine($"Success: {result}");}catch(Exceptionex){// Handle execution errorsConsole.WriteLine($"Host hook error: {ex.Message}");Electron.Dialog.ShowErrorBox("Operation Failed","Could not execute host function.");}
// In your ElectronHostHook/index.tsimport{app}from'electron';exportfunction getAppVersion():string{returnapp.getVersion();}exportasync function readConfigFile(): Promise<string>{
const fs =require('fs').promises;returnawaitfs.readFile('config.json','utf8');}exportfunctioncustomNotification(message:string):void{// Custom notification logicconsole.log(`Customnotification: ${message}`);}
Integration with .NET Code
// Use host hook in your application logicpublicasyncTask<string>GetApplicationVersion(){returnawaitElectron.HostHook.CallAsync<string>("getAppVersion");}publicasyncTaskLoadConfiguration(){try{varconfig=awaitElectron.HostHook.CallAsync<ConfigObject>("readConfigFile");ApplyConfiguration(config);}catch(Exceptionex){Console.WriteLine($"Failed to load config: {ex.Message}");UseDefaultConfiguration();}}publicvoidShowCustomNotification(stringmessage){Electron.HostHook.Call("customNotification",message);}