如何让长按电源键拒绝关机?
在大多数较新的计算机上,您可以通过按住电源按钮几秒钟来硬关闭。 但我想完全阻止这种情况发生。
您可以阻止一些软事件,例如 Windows 控制面板中的睡眠、挂起、休眠和“按电源按钮”事件; 您还可以使用一些 Win32 和 WDI 编程来捕获和处理/拒绝这些事件,但当您长时间按住按钮时,它似乎不会停止机械电源关闭。
我猜电源按钮是硬连线到电源的,当你按住它几秒钟时,永远不会等待操作系统? 或者有什么方法可以通过代码来防止这种情况?
我是一名 .NET 开发人员。
哇哦...! 你们中的很多人确实争论不这样做——“你们不这样做 拥有我的电脑或我的操作系统", - "你是 开发 SkyNet”等:) 这 应用程序是一个高度定制和 为某些客户提供封闭式解决方案。 这是一个定制的自助服务终端应用程序 运行在 Windows XP 上的硬件 嵌入并使用触摸屏。 它是 不公开。
On most newer computers you can shutdown the hard way by pressing the power button for a couple of seconds. But I want to prevent this completely.
You're able to prevent some soft events like the sleep, suspend, hibernate and "Press power button" events in Windows control panel; you can also use some Win32 and WDI programming to catch and handle/deny those events but it doesn't seem to stop the mechanical power off when you hold the button for a longer time.
I guess the power button is hard-wired to the power supply and never waits for the OS when you hold it down for some seconds? Or is there some way to prevent even this case by code?
I'm a .NET developer.
Whooaaa...! Lots of you really argued for not doing this - "You don't
own my computer or my OS", - "are you
developing SkyNet" etc :) This
application is a highly customized and
closed solution for some customers.
It's a kiosk application on customized
hardware running on Windows XP
Embedded and use a touch screen. It's
not public.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
我希望你不能这样做。 我曾多次遇到无法以任何其他方式重新启动计算机的情况。 在这种情况下,您真的想强迫笔记本电脑用户在运行时拔掉电源并取出电池吗?
发出警告后,您可能会为不同的情况编写更有意义的软件。 您能否详细说明这样做的动机?
如果用户能够按住电源按钮(表明他们真的非常想关闭机器),他们是否无法物理地断开电源?
编辑:回应 magsto 的评论:
如果每个参与的人都真的非常想要这个 - 向他们解释说,如果出现问题并且他们需要硬重置计算机,他们将不得不采用一种更直接的方式断电,我怀疑这比“按住电源”方法更有可能损坏硬件......与设备制造商联系。 听起来您可能正在使用特定设备,因此他们可能能够为您调整 BIOS 中的某些内容,即使它不是公开可用的选项。
鉴于这几乎总是一个非常糟糕的主意,我并不感到惊讶,因为它没有简单的支持,但如果你问制造商,可能有一些隐藏的方法可以做你想做的事。 不过,我仍然鼓励您让每个人都参与进来,思考所有潜在的后果。
I would hope that you can't do this. I've run into situations a number of times where I couldn't reboot the computer in any other way. In such cases, do you really want to force a laptop user to unplug the power supply and remove the battery while running?
Having given that warning, you may be writing software for rather different situations where it makes more sense. Could you give more details about the motivation for this?
If a user is able to hold the power button down (indicating that they really, really want to shut the machine down) are they unable to physically remove power?
EDIT: Responding to the comment from magsto:
If everyone involved really, really wants this - having explained to them that if things go wrong and they need to hard-reset the computer, they'll have to go to an even more direct way of powering down, which I suspect is more likely to damage hardware than the "hold down power" method... talk to the device manufacturer. It sounds like you're probably working with a specific device, so they may be able to tweak something in the BIOS for you, even if it's not a publicly available option.
Given that this is almost always a really bad idea, I'm not surprised there isn't simple support for it, but if you ask the manufacturer there may be some hidden way of doing what you want. I'd still encourage you to get everyone involved to think about all the potential consequences though.
请参阅与 4 秒规则相关的 ACPI 规范第 86 页 (https: //uefi.org/sites/default/files/resources/ACPI_6_3_final_Jan30.pdf)
正文如下:
总而言之,逻辑是在硬件中的。 ACPI 规范中没有记录更改此时间的方法。
See page 86 of the ACPI spec in relation to the 4-second rule (https://uefi.org/sites/default/files/resources/ACPI_6_3_final_Jan30.pdf)
The text is as follows:
Long and the short of it is that the logic is in hardware. There is no documented method for changing this timing in the ACPI specification.
这确实是一种机械断电,因此很难规避。
如果确实很重要,您可以断开电源按钮并使用网络唤醒功能打开/关闭计算机。 远程关机。
It is indeed a mechanical power off, so it will be kind of difficult to circumvent.
If it's really important, you could disconnect the power button and turn the computer on/off with wake-on-lan resp. remote shutdown.
使用脉冲发生器开关。 我不确定这是否是正确的术语,但我在目录中看到过它们。 这是一个快动开关,只有在您按下时才会“接通”一瞬间,而在您松开时则不会。 中途停下来是不行的。 一旦达到阈值,该机构就会开始移动以闭合触点,并且不会停止移动,直到触点再次打开。 这将允许您正常启动和正常停止,但无法保持电路闭合 4 秒。 (当然不用打开箱子)
Use a pulser switch. I'm not sure if that is the right term but I have seen them in catalog(s). It's a snap action switch which only 'makes' for a split second as you press and not as you release it. Stopping in the middle doesn't work. Once you reach the threshold the mechanism start moving to close the contact and doesn't stop moving until the contact opens again. This will allow you to start normally and stop normally but no way to keep the circuit closed for 4 seconds. (without opening the case, of course)
这是不可能的,因为关闭发生在操作系统级别以下,因此就操作系统而言就像拔掉插头一样!
This is not possible as this Power off occurs below the operating system level so it is like pulling the plug as far as the OS is concerned!
据我所知,您的说法是正确的,操作系统甚至不参与这种类型的“关闭”。 按住电源按钮一会儿只是向硬件发出信号,立即关闭计算机内所有设备的电源。 正如您所说,正常的关闭/睡眠/休眠事件当然可以使用 WinAPI 捕获和处理。
As far as I know, you are correct in saying that the OS is not even involved in this type of "shutdown". Holding the power button for a while is just a signal to the hardware to immediately turn off power to all devices within the computer. Normal shutdown/sleep/hibernate events can of course be caught and be handled using the WinAPI, as you state.
从主板上拔下电源线,并将其硬连线使其始终处于开启状态。 :)
Unplug the power cable from the motherboard, and hardwire it to be on all the time. :)
我相信您可以更改此设置的唯一方法是手动更改 BIOS 中的设置。 这可能无法被编程语言捕获。 如果确实需要,您可以从主板上拔下连接电源按钮的细线。
I believe the only way you can change this is by changing manually the settings in your BIOS. This probably can't be caught by a programming language. If you realy need to, you could just unplug the tiny wire from your motherboard which goes to the power button.
你们会更接近开发天网吗?
很抱歉这个问题,但正如很多人所建议的那样,我也相信这不应该成为可能,如果有可能设计的话。
Are you going nearer to develop the SkyNet ?
Sorry for this question, but as lot of people has suggested, I too believe this shouldn't be made possible, if at all it is possible to design.
好吧,禁用电源按钮也许是疯了。 但我们有一个软件应用程序,它使用数据库并使用一组昂贵的许可证。 这家大型德国供应商使用许可证管理器在驱动器上物理移动许可证。 碎片整理、某些防病毒应用程序以及切断电源都会损坏许可证。 我们有一个 UPS,可以在机器电源切断时优雅地关闭。 现在,似乎某些服务人员或操作员已使用电源按钮关闭了 PC,从而损坏了许可证。 这些设备位于世界各地的偏远地区,培训操作员可能并不总能防止这种情况发生。 解决方案选项似乎是 1) 与供应商合作禁用电源按钮,2) 尝试捕获它并警告用户可能的厄运,或 3) 使用不受此电源删除影响的操作系统和应用程序重新设计显示器。
Okay, so maybe it is insane to disable the power button. But we have a software application the uses a database and uses an expensive set of licenses. This large German vendor uses a license manager that moves the licenses physically around the drive. Defragging, certain anti-virus applications, and cutting the power corrupts the licenses. We have a UPS to allow elegant shutdowns if the machine power is cut. Now it seems some service guy or operator has powered down the PC with the power button corrupting the licenses. These devices are located in remote location all over the world and training the operator may not always prevent this. It seems like the solution options are 1) Work with the vendor to disable the power button, 2) attempt to trap it and warn the user of possible doom, or 3) redesign the display using an OS and application immune from this power removal.
你不能也不应该搞乱这个功能。
我可以设想两种可能出现问题的情况:
You can't and shouldn't be messing with this feature.
I can envisage two scenarios where this might be an issue:
当计算机用户是孩子并且电源按钮变成游戏而不是紧急操作时,这确实有意义。对于孩子来说,学习通过软件关闭机器的更好方法是禁用机械关闭。
It does make sense when the computer users are children and the power button becomes rather a game than an emergency action.The better way for a child to learn shutting down the machine via software is to disable mechanichal shutdown.
我建议以某种方式将电源按钮放在用户够不到的地方。 也许完全删除它。 并确保计算机在通电或其他情况时自动打开(不确定,但可以在BIOS中实现吗?)。
无论如何,对于始终在线的信息亭来说,这确实有意义。 至少对于许多其他硬件来说是这样。 关闭它的唯一方法是切断电源。
I would suggest to somehow just taking the power button out of reach from the user. Maybe removing it completely. And making sure the computer turns on automatically when it gets power or something (not sure but could be possible in bios?).
Anyways, for an always on kiosk thing, this does kind of make sense. At least it does for much other hardware. Where the only way to turning it off is to cut the power.
我也想实现同样的目标。 当阅读这里的答案时,我决定打开我的电脑。 我拥有一台惠普一体机,就我而言,我只能禁用电源按钮。 要部分禁用它只能通过硬件摆弄和调整来实现。
I also wanted to achieve the same thing. When reading the answers here I decided to open my PC. I own an HP All-In-One and in my case, I could only disable the power button. To partially disable it can only be possible with hardware fiddling and tweaking.