C++
[MFC] Win11 컴퓨터 시간 변경 SetLocalTime() (visual studio 2022)
파제르
2023. 7. 26. 14:49
반응형
PC의 시간을 변경할 일이 간혹 생긴다.
어딘가와 시간동기화를 하기 위해서...
위 이미지는 프로그램 실행 시 시간이 변경되는 부분을 나타낸 부분이고...
2022년 3월 21일 8시로 시간을 변경하였다...
thㅗthㅡ 주세요
CString strTime = _T("20220321080000");
SYSTEMTIME sysTime;
sysTime.wYear = atoi(strTime.Left(4));
sysTime.wMonth = atoi(strTime.Mid(4, 2));
sysTime.wDay = atoi(strTime.Mid(6, 2));
sysTime.wHour = atoi(strTime.Mid(8, 2));
sysTime.wMinute = atoi(strTime.Mid(10, 2));
sysTime.wSecond = atoi(strTime.Mid(12, 2));
sysTime.wMilliseconds = 0;
HANDLE hToken;
TOKEN_PRIVILEGES tp;
LUID luid;
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
{
strTemp.Format("OpenProcessToken - OK\n");
OutputDebugString(strTemp);
if (LookupPrivilegeValue(NULL, SE_SYSTEMTIME_NAME, &luid))
{
strTemp.Format("LookupPrivilegeValue - OK\n");
OutputDebugString(strTemp);
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL) == ERROR_SUCCESS)
{
strTemp.Format("AdjustTokenPrivileges - 지정된 모든 권한을 조정했습니다.");
OutputDebugString(strTemp);
}
else
{
strTemp.Format("AdjustTokenPrivileges Error - %d\n", GetLastError());
OutputDebugString(strTemp);
}
if (SetLocalTime(&sysTime) == FALSE)
{
strTemp.Format("SetLocalTime(&sysTime) Error - %d\n", GetLastError());
OutputDebugString(strTemp);
}
}
}
CloseHandle(hToken);
visual studio에서도 설정해줘야하는 부분이 있다.
UAC.
프로젝트 속성에서 링커 - 매니페스트 파일에서 UAC 실행 수준을 변경 해준다.
기본값은 asInvoker(/level='asInvoker') 로 되어있지만
requireAdministrator(/level='requireAdministrator') 로 변경해준다.
아래는 샘플소스파일
안되면 댓글..
728x90
반응형