I’m trying to use child_process to open another program through electron, but when I click the button even calling the function to render it doesn’t open the requested application.
Electron version ^16.0.7:
main.js
const { app, shell, BrowserWindow } = require('electron')
const path = require('path')
let mainWindow = null;
function createWindow () {
mainWindow = new BrowserWindow({
autoHideMenuBar: true,
width: 1300,
height: 840,
resizable: false,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
webPreferences: {nodeIntegration: true}
}
})
mainWindow.loadFile('./app/home.html')
}
app.whenReady().then(() => {
createWindow()
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('web-contents-created', (e, contents) => {
contents.on('new-window', (e, url) => {
e.preventDefault();
require('open')(url);
});
contents.on('will-navigate', (e, url) => {
if (url !== contents.getURL()) e.preventDefault(), require('open')(url);
});
});
preload.js
window.openGaeme = function(executablePath) {
let child = require('child_process').execFile;
child(executablePath, function(err, data) {
if(err){
console.error(err);
return;
}
console.log(data.toString());
});
}
function in renderer.js
function onClick() {
var executablePath1 = "C:\Program Files (x86)\Game\system\game.exe";
window.openGaeme(executablePath1);
}
HTML BUTTON AND SCRIPT:
<button class="button-go" role="button" onclick="onClick()">JOIN</button>
<script src="./core/renderer.js"></script>