A sample Electron application that demonstrates loading a .NET .dll file and interacting with it using the node-api-dotnet package. This example allows you to invoke methods from your .NET assembly directly within your Electron app.
ElectronJS
│ ├── main.js # Main process file
│ ├── renderer.js # Renderer process file
│ ├── index.html # Main HTML file
│ ├── MyDotNetLibrary.dll # .NET DLL file to interact with
│ └── package.json # Project configuration file
- Node.js - Install Node.js (version 14 or higher recommended).
- Electron - Installed via npm.
- .NET SDK - Required to build
.dllfiles. Install from here. - node-api-dotnet - Used to load .NET assemblies into Node.js.
-
Clone the Repository:
git clone <repository_url> cd ElectronJS
-
Add Your DLL File:
Make sure your
.NETlibrary,MyDotNetLibrary.dll, is in the project root directory (MyElectronApp/). -
Install Dependencies:
npm install
Handles the main process of Electron, loads the .dll file, and sets up IPC communication with the renderer process to handle .NET method calls.
Handles interactions from the HTML UI, sends data to the main process, and displays results.
A simple interface to take input, invoke the .NET function, and display the result.
-
Start the Application:
npm start
-
Using the App:
- Enter two numbers in the input fields.
- Click "Add Numbers".
- The result of the addition (calculated by the .NET method) will appear below the button.
In main.js, the .dll file is loaded using node-api-dotnet:
const dotnet = require('node-api-dotnet/net6.0');
dotnet.load(path.join(__dirname, 'MyDotNetLibrary.dll'));The renderer.js file communicates with the main process to call a .NET method:
const result = await ipcRenderer.invoke('dotnet-add', a, b);- Electron Version: Ensure compatibility between
node-api-dotnetand your Electron version. - Path Issues: Double-check the path to
MyDotNetLibrary.dll. - DLL Compatibility: Ensure that the
.dllwas built for the same .NET runtime as specified (e.g.,net6.0).