如何通过 Windows API 关闭电脑?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1503839/
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
How to turn off pc via windows API?
提问by Night Walker
I neverprogrammed a winapi so i have a little problem here .
我从来没有编写过 winapi,所以我在这里遇到了一个小问题。
I need turn off my pc from my application .
我需要从我的应用程序中关闭我的电脑。
I found this example link textthen i found this example how to change privileges link text
我找到了这个示例链接文本然后我找到了这个示例如何更改权限链接文本
But i have problem how to get that parameter HANDLE hToken // access token handle
但我有问题如何获取该参数 HANDLE hToken // 访问令牌句柄
I think i need to make it in the next order to get the parameter OpenProcessToken LookupPrivilegeValue AdjustTokenPrivileges but there are a lot parameters that i have no idea what to do with them .
我想我需要按照下一个顺序来获取参数 OpenProcessToken LookupPrivilegeValue AdjustTokenPrivileges 但是有很多参数我不知道如何处理它们。
maybe you have jere some example how i get that HANDLE hToken parameter to make that work .
也许你有一些例子,我如何获得 HANDLE hToken 参数来使其工作。
By the way I already saw the following post link text
顺便说一句,我已经看到以下帖子链接文本
Thanks a lot all you .
非常感谢你们。
回答by Lior Kogan
// ==========================================================================
// system shutdown
// nSDType: 0 - Shutdown the system
// 1 - Shutdown the system and turn off the power (if supported)
// 2 - Shutdown the system and then restart the system
void SystemShutdown(UINT nSDType)
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp ;
::OpenProcessToken(::GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &hToken);
::LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1 ; // set 1 privilege
tkp.Privileges[0].Attributes= SE_PRIVILEGE_ENABLED;
// get the shutdown privilege for this process
::AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
switch (nSDType)
{
case 0: ::ExitWindowsEx(EWX_SHUTDOWN|EWX_FORCE, 0); break;
case 1: ::ExitWindowsEx(EWX_POWEROFF|EWX_FORCE, 0); break;
case 2: ::ExitWindowsEx(EWX_REBOOT |EWX_FORCE, 0); break;
}
}
回答by Jacob
You could use ShellExecute()to call shutdown.exe
您可以使用ShellExecute()调用shutdown.exe
回答by Daniel Mo?mondor
http://msdn.microsoft.com/en-us/library/aa376868(VS.85).aspx
http://msdn.microsoft.com/en-us/library/aa376868(VS.85).aspx
Try
尝试
ExitWindowsEx(EWX_POWEROFF, 0);
回答by T.E.D.
This is a bit much for the comments on Daniel's answer, so I'll put it here.
对丹尼尔的回答的评论有点多,所以我把它放在这里。
It looks like your main issue at this point is that your process isn't running with the priveleges required to perform a system shutdown.
看起来您此时的主要问题是您的进程没有以执行系统关闭所需的特权运行。
The docs for ExitWindowsExcontain this line:
ExitWindowsEx的文档包含这一行:
To shut down or restart the system, the calling process must use the
AdjustTokenPrivileges
function to enable theSE_SHUTDOWN_NAME
privilege. For more information, see Running with Special Privileges.
要关闭或重新启动系统,调用进程必须使用该
AdjustTokenPrivileges
功能启用该SE_SHUTDOWN_NAME
权限。有关更多信息,请参阅以特殊权限运行。
They also have some example code. In a pinch, you can just copy that.
他们也有一些示例代码。在紧要关头,你可以复制它。
回答by Esmail Jamshidiasl
#include<iostream>
using namespace std;
int main(){
system("shutdown -s -f -t 0");
}
回答by chowey
Some working code for InitiateSystemShutdownEx
:
一些工作代码InitiateSystemShutdownEx
:
// Get the process token
HANDLE hToken;
OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
&hToken);
// Build a token privilege request object for shutdown
TOKEN_PRIVILEGES tk;
tk.PrivilegeCount = 1;
tk.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
LookupPrivilegeValue(NULL, TEXT("SeShutdownPrivilege"), &tk.Privileges[0].Luid);
// Adjust privileges
AdjustTokenPrivileges(hToken, FALSE, &tk, 0, NULL, 0);
// Go ahead and shut down
InitiateSystemShutdownEx(NULL, NULL, 0, FALSE, FALSE, 0);
So far as I can tell, the advantage to this over the ExitWindowsEx
solution is that the calling process does not need to belong to the active user.
据我所知,与ExitWindowsEx
解决方案相比,此方法的优势在于调用进程不需要属于活动用户。