windows 长按电源键如何拒绝关机?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/642058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:08:47  来源:igfitidea点击:

How to deny shutdown when pressing the power button for a while?

.netwindowswinapihardwareacpi

提问by Magnus

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 offwhen you hold the button for a longer time.

您可以阻止一些软事件,如 Windows 控制面板中的睡眠、挂起、休眠和“按下电源按钮”事件;您还可以使用一些 Win32 和 WDI 编程来捕获和处理/拒绝这些事件,但是当您长时间按住按钮时,它似乎不会停止机械电源关闭

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.

我是 .NET 开发人员。

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.

呜呜呜……!你们中的很多人真的反对不这样做——“你不拥有我的电脑或我的操作系统”,——“你在开发 SkyNet”等等:) 对于某些客户来说,这个应用程序是一个高度定制和封闭的解决方案。它是在 Windows XP Embedded 上运行并使用触摸屏的定制硬件上的自助服务终端应用程序。这不是公开的。

采纳答案by Petesh

See page 62 of the ACPI spec in relation to the 4-second rule (http://www.acpi.info/DOWNLOADS/ACPIspec30b.pdf)

请参阅与 4 秒规则相关的 ACPI 规范第 62 页 ( http://www.acpi.info/DOWNLOADS/ACPIspec30b.pdf)

Long and the short of it is that the logic is in hardware.

总而言之,逻辑在硬件中。

回答by Jon Skeet

I would hopethat 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:

编辑:回应 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.

如果每个参与者都真的、真的想要这个 - 已经向他们解释说如果出现问题并且他们需要硬重置计算机,他们将不得不采取更直接的关机方式,我怀疑这更有可能比“按住电源”方法损坏硬件......与设备制造商交谈。听起来您可能正在使用特定设备,因此他们可能能够为您调整 BIOS 中的某些内容,即使它不是公开可用的选项。

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 everyoneinvolved to think about all the potential consequences though.

鉴于这几乎总是一个非常糟糕的主意,我并不惊讶它没有简单的支持,但是如果您询问制造商,可能会有一些隐藏的方式来做您想做的事。不过,我仍然鼓励您让每个人都参与进来,考虑所有潜在的后果。

回答by user1920524

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)

使用脉冲发生器开关。我不确定这是否是正确的术语,但我在目录中看到过它们。这是一个快速动作开关,它只会在您按下时“闭合”一瞬间,而不会在您松开时“闭合”。停在中间不起作用。一旦您达到阈值,该机制就会开始移动以关闭触点,并且在触点再次打开之前不会停止移动。这将允许您正常启动和正常停止,但无法保持电路关闭 4 秒。(当然,无需打开机箱)

回答by Lennaert

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.

如果这真的很重要,您可以断开电源按钮并通过唤醒网络来打开/关闭计算机。远程关机。

回答by OneSHOT

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!

这是不可能的,因为此关机发生在操作系统级别以下,因此就操作系统而言,这就像拔掉插头一样!

回答by Noldorin

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.

据我所知,您说操作系统甚至不参与这种类型的“关闭”是正确的。按住电源按钮一段时间只是向硬件发出信号,可以立即关闭计算机内所有设备的电源。正如您所说,正常的关机/睡眠/休眠事件当然可以使用 WinAPI 捕获和处理。

回答by leppie

Unplug the power cable from the motherboard, and hardwire it to be on all the time. :)

从主板上拔下电源线,并将其硬接线使其始终处于开启状态。:)

回答by Kunal S

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.

很抱歉这个问题,但正如很多人所建议的那样,我也相信这不应该成为可能,如果有可能的话。

回答by fbernier

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.

我相信您可以更改此设置的唯一方法是手动更改 BIOS 中的设置。这可能无法被编程语言捕获。如果你真的需要,你可以从主板上拔下连接电源按钮的细线。

回答by fbernier

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.

好的,所以也许禁用电源按钮是疯了。但是我们有一个软件应用程序,它使用数据库并使用一组昂贵的许可证。这家大型德国供应商使用许可证管理器在驱动器周围物理移动许可证。碎片整理、某些防病毒应用程序和切断电源会损坏许可证。如果机器电源被切断,我们有一个 UPS 允许优雅的关机。现在似乎某些服务人员或操作员通过电源按钮损坏许可证关闭了 PC。这些设备位于世界各地的偏远地区,对操作员进行培训可能并不总是能防止这种情况发生。似乎解决方案选项是 1) 与供应商合作禁用电源按钮,2) 尝试捕获它并警告用户可能的厄运,