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

来源:互联网 发布:人工智能语言是什么 编辑:程序博客网 时间:2024/06/06 14:11
 
/** * @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}

原创粉丝点击