使用Node.js将窗口设置为节能模式 [英] Setting windows to energy save mode using Node.js

查看:98
本文介绍了使用Node.js将窗口设置为节能模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Electron和Node.js开发一个应用程序,应该在需要时将笔记本电脑(运行Windows 10的计算机)设置为节能模式,这是否有可能使用Node.js和electronic技术做到这一点?谢谢

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

推荐答案

您可以调用WinAPI函数 SetSuspendState 使用 node-ffi 库。

You can call the WinAPI function SetSuspendState using the node-ffi 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);
}

请注意,这会进行正常的待机,并会唤醒事件。如果要禁用唤醒事件,请使用 powrprof.SetSuspendState(0,0,1)(第三个参数1而不是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.

更新:请注意,如果您认为使用 rundll32 ,那么您将得到奇怪的行为,具体取决于计算机设置以及天气和星期几(如-未定义行为),因为 rundll32 不仅会按照您的想法运行任意的DLL函数。请参阅本文 rundll32 文档。调用 rundll32 powrprof.dll,SetSuspendState 0,0,0 可能会使您的计算机进入睡眠状态,但可能会在另一台计算机上执行其他操作,例如休眠而不是调用待机模式(或理论上甚至崩溃)。因此,不要这样做!

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!

这篇关于使用Node.js将窗口设置为节能模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆