您的位置:首页 > 编程语言 > C语言/C++

C/C++实现跨平台重启系统

2011-08-22 14:11 375 查看
/** 
* @brief restartSystem 
* 
* Detailed description.
*/
void restartSystem()
{
#ifdef WIN32
    HANDLE hToken;
    TOKEN_PRIVILEGES tkp;
    LUID luid;

    // Get version info to determine operation
    OSVERSIONINFO osvi;
    osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
    if (GetVersionEx(&osvi) == 0)
    {
        return;
    }
    // Determine the platform
    if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
    {
        // Windows NT 3.51, Windows NT 4.0, Windows 2000,
        // Windows XP, or Windows .NET Server
        if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
        {

        }
        else
        {
            LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &luid);

            tkp.PrivilegeCount = 1;  // one privilege to set
            tkp.Privileges[0].Luid = luid;
            tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
            AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(TOKEN_PRIVILEGES), NULL, 0);
        }
        ExitWindowsEx(EWX_REBOOT, 0);
    }
#else
    system("reboot");
#endif
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: