Reputation: 353
I have a Windows Service polling periodically whether my app in running or not. When it is, the service checks for an update on a server. If there is an update, I need a way for the service to let the app know it is available so the user can be notified.
Is this possible?
Upvotes: 1
Views: 1156
Reputation: 879
You can use one of the IPC means, by creating a named pipe (socket)
//"PIPE_NAME" :"the_name_of_the_pipe",
//"PIPE_PATH" : "\\\\.\\pipe\\"
const net = require('net');
let socketClient = net.createConnection(config.get('PIPE_PATH') + config.get('PIPE_NAME'), () => {
console.log('connected to service!');
});
socketClient.on('data', (data) => {
console.log(data.toString());
});
socketClient.on('error', (error) => {
//console.log(`Couldn't connect to service : ${error.toString()}`);
setTimeout(() => {
socketClient.connect(config.get('PIPE_PATH') + config.get('PIPE_NAME'), () => {
console.log('connected to service!');
});
}, 4000);
});
socketClient.on('end', () => {
console.log('disconnected from service');
socketClient.connect(config.get('PIPE_PATH') + config.get('PIPE_NAME'), () => {
console.log('connected to service!');
});
});
I used Qt
for the service side and created QLocalServer
to exchange messages between the service and electron app but you can use and find many examples out there
you can create a namedpipe that listens
//https://stackoverflow.com/a/26561999/3339316
HANDLE hPipe;
char buffer[1024];
DWORD dwRead;
hPipe = CreateNamedPipe(TEXT("\\\\.\\pipe\\Pipe"),
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, // FILE_FLAG_FIRST_PIPE_INSTANCE is not needed but forces CreateNamedPipe(..) to fail if the pipe already exists...
1,
1024 * 16,
1024 * 16,
NMPWAIT_USE_DEFAULT_WAIT,
NULL);
while (hPipe != INVALID_HANDLE_VALUE)
{
if (ConnectNamedPipe(hPipe, NULL) != FALSE) // wait for someone to connect to the pipe
{
while (ReadFile(hPipe, buffer, sizeof(buffer) - 1, &dwRead, NULL) != FALSE)
{
/* add terminating zero */
buffer[dwRead] = '\0';
/* do something with data in buffer */
printf("%s", buffer);
}
}
DisconnectNamedPipe(hPipe);
}
Upvotes: 1