システムの時刻の時刻に対する定期的な時刻調整を有効または無効にします。 有効にすると、このような時間調整を使用して、時刻を他の時刻情報ソースと同期できます。
構文
BOOL SetSystemTimeAdjustmentPrecise(
[in] DWORD64 dwTimeAdjustment,
[in] BOOL bTimeAdjustmentDisabled
);
パラメーター
[in] dwTimeAdjustment
調整されたクロック更新頻度を提供します。
[in] bTimeAdjustmentDisabled
システムが使用する時間調整モードを指定するフラグを指定します。
TRUE の値は、システムが独自の内部メカニズムを使用して時刻を同期する必要があることを示します。 この場合、 dwTimeAdjustment の値は無視されます。
FALSE の値は、アプリケーションが制御されていること、および dwTimeAdjustment の指定された値がクロック更新割り込みごとに時刻の時刻に追加されることを示します。
戻り値
関数が成功した場合、戻り値は 0 以外です。
関数が失敗した場合は、0 を返します。 詳細なエラー情報を得るには、GetLastError を呼び出します。 関数が失敗する 1 つの方法は、呼び出し元がSE_SYSTEMTIME_NAME特権を持っていない場合です。
解説
この関数を使用するには、呼び出し元にシステム時特権 (SE_SYSTEMTIME_NAME) が必要です。 この特権は既定で無効になっています。 AdjustTokenPrivileges 関数を使用して、この関数を呼び出す前に特権を有効にしてから、関数呼び出し後に特権を無効にします。 詳細については、以下のコード例を参照してください。
例
このサンプルでは、システム時特権を有効にする方法、 GetSystemTimeAdjustmentPrecise と SetSystemTimeAdjustmentPrecise を使用してシステム クロックを調整する方法、および現在のシステム時刻調整をきちんと印刷する方法を示します。
/******************************************************************
*
* ObtainRequiredPrivileges
*
* Enables system time adjustment privilege.
*
******************************************************************/
HRESULT
ObtainRequiredPrivileges()
{
HRESULT hr;
HANDLE hProcToken = NULL;
TOKEN_PRIVILEGES tp = {0};
LUID luid;
if (!LookupPrivilegeValue(NULL, SE_SYSTEMTIME_NAME, &luid))
{
hr = HRESULT_FROM_WIN32(GetLastError());
printf("Failed to lookup privilege value. hr=0x%08x\n", hr);
return hr;
}
// get the token for our process
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
&hProcToken))
{
hr = HRESULT_FROM_WIN32(GetLastError());
printf("Failed to open process token. hr=0x%08x\n", hr);
return hr;
}
// Enable just the SYSTEMTIME privilege
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (!AdjustTokenPrivileges(hProcToken, FALSE, &tp, 0, NULL, NULL))
{
hr = HRESULT_FROM_WIN32(GetLastError());
printf("Failed to adjust process token privileges. hr=0x%08x\n", hr);
}
else
{
hr = S_OK;
printf("Added SYSTEMTIME privilege to the process token\n");
}
if (NULL != hProcToken)
{
CloseHandle(hProcToken);
}
return hr;
}
/******************************************************************
*
* PrintCurrentClockAdjustments
*
* Prints current values of the system time adjustments.
*
******************************************************************/
void
PrintCurrentClockAdjustments()
{
// More granular clock adjustments
DWORD64 ullCurrentAdjustment = 0;
DWORD64 ullTimeIncrement = 0;
BOOL bEnabledPrecise = 0;
HRESULT hrPrecise = S_OK;
// Legacy clock adjustments
DWORD dwCurrentAdjustment = 0;
DWORD dwTimeIncrement = 0;
BOOL bEnabled = 0;
HRESULT hr = S_OK;
if (!GetSystemTimeAdjustmentPrecise(&ullCurrentAdjustment, &ullTimeIncrement, &bEnabledPrecise))
{
hrPrecise = HRESULT_FROM_WIN32(GetLastError());
}
if (!GetSystemTimeAdjustment(&dwCurrentAdjustment, &dwTimeIncrement, &bEnabled))
{
hr = HRESULT_FROM_WIN32(GetLastError());
}
printf("Precise_ADJ:%I64u Precise_INCR:%I64u Precise_EN:%d Precise_hr:0x%08x ADJ:%u INCR:%u EN:%d hr:0x%08x\n",
ullCurrentAdjustment, ullTimeIncrement, bEnabledPrecise, hrPrecise,
dwCurrentAdjustment, dwTimeIncrement, bEnabled, hr);
}
/******************************************************************
*
* RunNewAdjustmentSequence
*
* Adjust the system time using high-resolution
* GetSystemTimeAdjustmentPrecise() and SetSystemTimeAdjustmentPrecise() API.
*
******************************************************************/
void
RunNewAdjustmentSequence(DWORD dwPPMAdjustment)
{
DWORD64 ullCurrentAdjustment = 0;
DWORD64 ullTimeIncrement = 0;
BOOL bEnabledPrecise = 0;
LARGE_INTEGER liPerfCounterFrequency = {0};
DWORD dwNewAdjustmentUnits;
const DWORD cMicroSecondsPerSecond = 1000000;
if (dwPPMAdjustment > 1000)
{
printf("Adjustment too large. Skipping new adjustment sequence.\n");
return;
}
printf("Starting adjustment sequence using new API...\n");
if (!GetSystemTimeAdjustmentPrecise(&ullCurrentAdjustment, &ullTimeIncrement, &bEnabledPrecise))
{
printf("Failed to read the system time adjustment. Adjustment sequence aborted. hr:0x%08x\n",
HRESULT_FROM_WIN32(GetLastError()));
return;
}
(void)QueryPerformanceFrequency(&liPerfCounterFrequency);
printf("System Performance Counter Frequency: %I64u\n",
liPerfCounterFrequency.QuadPart);
dwNewAdjustmentUnits = (DWORD)(((float) dwPPMAdjustment * liPerfCounterFrequency.QuadPart/ cMicroSecondsPerSecond));
printf("Adjusting the system clock by +%d PPM (+%d new units)\n",
dwPPMAdjustment, dwNewAdjustmentUnits);
if (!SetSystemTimeAdjustmentPrecise(ullCurrentAdjustment + dwNewAdjustmentUnits, FALSE))
{
printf("Failed to set the system time adjustment. hr:0x%08x\n",
HRESULT_FROM_WIN32(GetLastError()));
}
PrintCurrentClockAdjustments();
printf("Restoring system clock adjustment settings\n");
if (!SetSystemTimeAdjustmentPrecise(ullCurrentAdjustment, FALSE))
{
printf("Failed to set the system time adjustment. hr:0x%08x\n",
HRESULT_FROM_WIN32(GetLastError()));
}
PrintCurrentClockAdjustments();
printf("Adjusting the system clock by -%d PPM (-%d new units)\n",
dwPPMAdjustment, dwNewAdjustmentUnits);
if (!SetSystemTimeAdjustmentPrecise(ullCurrentAdjustment - dwNewAdjustmentUnits, FALSE))
{
printf("Failed to set the system time adjustment. hr:0x%08x\n",
HRESULT_FROM_WIN32(GetLastError()));
}
PrintCurrentClockAdjustments();
printf("Restoring system clock adjustment settings\n");
if (!SetSystemTimeAdjustmentPrecise(ullCurrentAdjustment, FALSE))
{
printf("Failed to set the system time adjustment. hr:0x%08x\n",
HRESULT_FROM_WIN32(GetLastError()));
}
PrintCurrentClockAdjustments();
printf("Adjustment sequence complete\n\n");
}
要件
| サポートされている最小のクライアント | Windows 10 [デスクトップ アプリのみ] |
| サポートされている最小のサーバー | Windows Server 2016 [デスクトップ アプリのみ] |
| 対象プラットフォーム | Windows |
| ヘッダー | sysinfoapi.h |
| Library | Mincore.lib |
| [DLL] | Api-ms-win-core-version-l1-2-3.dll |