0

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

4

1 回答 1

1

你做。这是一个例子

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; 

} // 关闭窗口

于 2015-12-16T00:20:33.027 回答