The HostHook API allows you to execute your own JavaScript/TypeScript code on the host process.
+
+
Create first an ElectronHostHook directory via the Electron.NET CLI, with the following command: electronize add hosthook.
+
In this directory you can install any NPM packages and embed your own JavaScript/TypeScript code. It is also possible to respond to events from the host process.
+
You find the sample source code in Controllers\HostHookController.cs and in the ElectronHostHook folder.
+
+
+
+
+
+
+
+
+
+
+
+
Use Electron.HostHook.CallAsync to execute asynchronously your own JavaScript/TypeScript code, that expect a result value.
+
+
This example execute the TypeScript code, that listening on "create-excel". The TypeScript code use a NPM Package names exceljs, to create a Excel file and reply a success message.
+
+
Main Process (C#)
+
+Electron.IpcMain.On("start-hoosthook", async (args) =>
+{
+ var mainWindow = Electron.WindowManager.BrowserWindows.First();
+ var options = new OpenDialogOptions
+ {
+ Properties = new OpenDialogProperty[]
+ {
+ OpenDialogProperty.openDirectory
+ }
+ };
+ var folderPath = await Electron.Dialog.ShowOpenDialogAsync(mainWindow, options);
+
+ var resultFromTypeScript = await Electron.HostHook.CallAsync("create-excel-file", folderPath);
+ Electron.IpcMain.Send(mainWindow, "excel-file-created", resultFromTypeScript);
+});
+
+
+
index.ts from ElectronHostHook-Folder (TypeScript)
+
+
+import * as Electron from "electron";
+import { Connector } from "./connector";
+import { ExcelCreator } from "./excelCreator";
+
+export class HookService extends Connector {
+ constructor(socket: SocketIO.Socket, public app: Electron.App) {
+ super(socket, app);
+ }
+
+ onHostReady(): void {
+ // execute your own JavaScript Host logic here
+ this.on("create-excel", async (path, done) => {
+ const excelCreator: ExcelCreator = new ExcelCreator();
+ const result: string = await excelCreator.create(path);
+
+ done(result);
+ });
+ }
+}
+
+