gpt4 book ai didi

c++ - 我是否需要 SE_SHUTDOWN_NAME 权限才能注销用户?

转载 作者:行者123 更新时间:2023-12-03 12:51:37 24 4
gpt4 key购买 nike

我需要从 C++ 程序中注销用户。我用ExitWindowsEx API,但我从文档中不确定是否需要任何特殊权限?

最佳答案

你知道。这是一个例子

bool ShutdownWindows(void)
{
HANDLE hToken = NULL;
TOKEN_PRIVILEGES tkp = {0};
bool bRet = false;

// Get a token for this process.
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
if (LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid)) {
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

// Get the shutdown privilege for this process.
if (AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, NULL, 0)) {
::CloseHandle(hToken);

if (ERROR_SUCCESS == GetLastError()) {

DWORD dwFlags = EWX_POWEROFF;
DWORD dwReason = SHTDN_REASON_MAJOR_SYSTEM;

if (ExitWindowsEx(dwFlags, dwReason)) {
bRet = true;
}
}
}
}
}

return bRet;

}//关闭Windows

关于c++ - 我是否需要 SE_SHUTDOWN_NAME 权限才能注销用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13168289/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com