/*****************************************************************
函数名:EnableSetTimePriviledge
功 能:开放设置时间权限
返回值:布尔类型,权限开放成功返回TRUE,否则返回FALSE
*****************************************************************/
BOOL EnableSetTimePriviledge()
{
HANDLE m_hToken;
TOKEN_PRIVILEGES m_TokenPriv;
BOOL m_bTakenPriviledge; BOOL bOpenToken = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES |
TOKEN_QUERY, &m_hToken); m_bTakenPriviledge = FALSE;
if (!bOpenToken)
{
if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
{
//Must be running on 95 or 98 not NT. In that case just ignore the error
SetLastError(ERROR_SUCCESS);
if(!m_hToken)
CloseHandle(m_hToken);
return TRUE;
}
TRACE(_T("Failed to get Adjust privilege token\n"));
if(!m_hToken)
CloseHandle(m_hToken);
return FALSE;
}
ZeroMemory(&m_TokenPriv, sizeof(TOKEN_PRIVILEGES));
if (!LookupPrivilegeValue(NULL, SE_SYSTEMTIME_NAME, &m_TokenPriv.Privileges[0].Luid))
{
TRACE(_T("Failed in callup to lookup privilege\n"));
if(!m_hToken)
CloseHandle(m_hToken);
return FALSE;
}
m_TokenPriv.PrivilegeCount = 1;
m_TokenPriv.Privileges[0].Attributes |= SE_PRIVILEGE_ENABLED;
m_bTakenPriviledge = TRUE; BOOL bSuccess = AdjustTokenPrivileges(m_hToken, FALSE, &m_TokenPriv, 0, NULL, 0);
if (!bSuccess)
TRACE(_T("Failed to adjust SetTime privilege\n")); if(!m_hToken)
CloseHandle(m_hToken); return bSuccess;
}
启用进程访问令牌权限,再进行设置。
SYSTEMTIME sysTime;
GetLocalTime(&sysTime);
if(SetLocalTime(&sysTime))
{
//设置成功
}