I'm developing an app using Electron and Node.js and is supposed to set the laptop (running windows 10) to energy saving mode when needed, is this possible to do this using technologies of Node.js and electron? Thanks
2 Answers
You can call the WinAPI function SetSuspendState
using the node-ffi
library.
First, install the library:
npm install ffi --save
Then, you can use this code:
var ffi = require('ffi');
var powrprof = ffi.Library('powrprof.dll', {
SetSuspendState: ['int', ['int', 'int', 'int']]
});
function invokeStandby() {
powrprof.SetSuspendState(0, 0, 0);
}
Note that this does a normal standby and leaves wake events on. If you want to disable wake events, use powrprof.SetSuspendState(0, 0, 1)
(third parameter 1 instead of 0). See the docs for details.
UPDATE: Note that if you think a nice shortcut would be using rundll32
, then you will get weird behavior depending on the computer settings and probably the weather and the day of week (as in - undefined behavior), because rundll32
doesn't just run arbitrary DLL functions the way you think. See this article and the rundll32
docs. Calling rundll32 powrprof.dll,SetSuspendState 0,0,0
might put your computer to sleep, but it might do something else on another computer such as hibernating instead of invoking standby mode (or theoretically even crash). So, do not do this!

- 25,571
- 5
- 49
- 74
I came up with an idea which doesn't require additional libraries.
var exec = require('child_process').exec;
exec('rundll32.exe powrprof.dll,SetSuspendState 0,0,0');
This function simply spawns a shell calling rundll32.exe so we can directly execute the function SetSuspendState
with the required parameters.
Hope this helps

- 926
- 9
- 15
-
1Note that this is not correct! `rundll32` cannot call arbitrary DLL functions! In fact, the `0,0,0` does *not* what you expect, instead it passes unexpected garbage to the function, because the calling convention for `rundll32` is very specific and sends four parameters: `(HWND hwnd, HINSTANCE hinst, LPSTR pszCmdLine, int nCmdShow)`, where `pszCmdLine` is a string pointer to the command line. So effectively you are calling something like `SetSuspendState(0xABCDE, 0x10000000, "0, 0, 0", 0);` (in C terms), which is definitely not right and will most likely hibernate the PC (if hibernation is on) – CherryDT May 10 '16 at 08:59
-
For further details, see https://blogs.msdn.microsoft.com/oldnewthing/20040115-00/?p=41043 (scroll down to "Rundll32.exe entry points") or the answer Raymond added to [this](https://blogs.msdn.microsoft.com/oldnewthing/20070607-00/?p=26523#comment-512403) comment. – CherryDT May 10 '16 at 09:00
-
See also the documentation for `rundll32` from Microsoft: https://support.microsoft.com/en-us/kb/164787 – CherryDT May 10 '16 at 09:02
-
Thx for pointing this out. I knew about the existence of `rundll32` but never looked up it's documentation. I came across a few post which were doing the same thing so I had the wrong idea about this exe – coffee_addict May 10 '16 at 10:36
-
Updated link to the comment I linked above: http://web.archive.org/web/20181011035708/https://blogs.msdn.microsoft.com/oldnewthing/20070607-00/?p=26523#comment-512403 – CherryDT May 03 '19 at 17:52