void ptime() { time_t nowtime; struct tm *timeinfo; time( &nowtime ); timeinfo = localtime( &nowtime ); int hour,min,sec; hour=timeinfo->tm_hour; min=timeinfo->tm_min; sec=timeinfo->tm_sec; printf("当前时间:%d:%d:%d\n",hour,min,sec); printf("能否修改当前时间?(Y\N)\n"); char c; scanf("%c",&c); if(c=="Y"||c=="y") { SYSTEMTIME t; printf("请输入小时:"); scanf("%d",&t.wHour); printf("请输入分钟:"); scanf("%d",&t.wMinute); printf("请输入秒:"); scanf("%d",&t.wSecond); EnablePrivilege(SE_SYSTEMTIME_NAME,0); bool re=SetSystemTime(&t); if(re) { printf("修改成功\n"); } else { printf("修改失败\n"); } } else { return; } }
解决方案
10
请题主牢记,永远不要手动修改系统时间。
10
总而言之,你记住不要修改系统时间就好了。
免得你将来原因是擅自修改系统时间被雇主炒鱿鱼。
免得你将来原因是擅自修改系统时间被雇主炒鱿鱼。
20
本人这个测试成功,并且要求程序必须以管理员权限执行,否则依然失败:
#include <stdio.h> #include <windows.h> BOOL SetProcessPrivilege( HANDLE hProcess, // access token handle LPCTSTR lpszPrivilege, // name of privilege to enable/disable BOOL bEnablePrivilege // to enable or disable privilege ) { HANDLE hToken = NULL; BOOL bResult = OpenProcessToken(hProcess, TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES, &hToken); if (bResult) { LUID luid = { 0, 0 }; bResult = LookupPrivilegeValue(NULL, lpszPrivilege, &luid); if (bResult) { TOKEN_PRIVILEGES tp; tp.PrivilegeCount = 1; tp.Privileges[0].Luid.LowPart = luid.LowPart; tp.Privileges[0].Luid.HighPart = luid.HighPart; tp.Privileges[0].Attributes = bEnablePrivilege ? SE_PRIVILEGE_ENABLED : 0; bResult = AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL); } CloseHandle(hToken); } return bResult; } int main(int argc, char *argv[]) { BOOL bResult; HANDLE hProcess = GetCurrentProcess(); bResult = SetProcessPrivilege(hProcess, SE_SYSTEMTIME_NAME, TRUE); printf("%d (%u)\n", bResult, GetLastError()); SYSTEMTIME st; GetLocalTime(&st); st.wDay++; st.wDayOfWeek = 0; st.wMilliseconds = 0; bResult = SetLocalTime(&st); printf("%d (%u)\n", bResult, GetLastError()); return 0; }