Not a Win32 application error
I am having issues using this library in an Electron application.
I am using electron-vite on Windows 11 (not in WSL) and when I start the dev environment (pnpm exec electron-vite dev) the app crashes.
I haven't found a lot of resources about this issue but those I found are mostly stating the issue when the app is packaged, but I am having issues with the app even starting for development.
I have tried using @electron/rebuild but no success.
I reinstalled all node_modules used in the project using npkill to make sure I haven't brought something OS specific in the project from my other computer (mac).
The simplified code I am using to invoke win32-api is:
// service class listening for ipcMain events
import { FindWindowEx } from 'win32-api/util';
export class IPCService {
constructor() {
ipcMain.on('app:notepad', async (event) => {
const hWnd = await FindWindowEx(0, 0, 'Notepad', null);
console.log(hWnd);
event.returnValue = hWnd;
});
}
}
// native preload script
import { contextBridge, ipcRenderer } from 'electron';
contextBridge.exposeInMainWorld('electron', {
app: {
notepad: (): string => {
return ipcRenderer.sendSync('app:notepad');
},
},
});
// React component
export const App = () => {
const getNotepad = () => {
console.log(window.electron!.app.notepad());
};
return (
<div>
<Button onClick={getNotepad}>Notepad</Button>
</div>
);
};
The moment I comment FindWindowEx and the import for it the app launches just fine.
The issue has to be electron specific because I tried running this code in a simple file and it provides the expected results:
import { spawn } from 'child_process';
import {
FindWindowEx,
GetDefaultPrinter,
} from 'win32-api/util'
const printerName = await GetDefaultPrinter()
const child = spawn('notepad.exe')
const hWnd = await FindWindowEx(0, 0, 'Notepad', null)
console.log(printerName);
// Microsoft Print to PDF
console.log(hWnd);
// 0
// Also launched Notepad
I don't the reason about Electron , maybe you can try npm instead of pnpm