SetSystemTimeAdjustmentPrecise
関数シグネチャ
// api-ms-win-core-sysinfo-l1-2-4.dll
#include <windows.h>
BOOL SetSystemTimeAdjustmentPrecise(
ULONGLONG dwTimeAdjustment,
BOOL bTimeAdjustmentDisabled
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| dwTimeAdjustment | ULONGLONG | in | 調整後のクロック更新頻度を指定します。 |
| bTimeAdjustmentDisabled | BOOL | in | システムが使用する時刻調整モードを指定するフラグを指定します。 値が TRUE の場合、システムは独自の内部メカニズムを用いて時刻を同期します。この場合、dwTimeAdjustment の値は無視されます。 値が FALSE の場合、アプリケーションが制御を行い、指定された dwTimeAdjustment の値が各クロック更新割り込みのたびに時刻クロックへ加算されます。 |
戻り値の型: BOOL
公式ドキュメント
システムの時刻クロックに対する定期的な時刻調整を有効または無効にします。有効にした場合、こうした時刻調整を利用して、別の時刻情報源と時刻を同期させることができます。(SetSystemTimeAdjustmentPrecise)
戻り値
関数が成功した場合、戻り値は 0 以外になります。
関数が失敗した場合、戻り値は 0 になります。拡張エラー情報を取得するには、 GetLastError を呼び出します。関数が失敗する原因の 1 つとして、呼び出し元が SE_SYSTEMTIME_NAME 特権を保持していない場合が挙げられます。
解説(Remarks)
この関数を使用するには、呼び出し元がシステム時刻特権 (SE_SYSTEMTIME_NAME) を持っている必要があります。この特権は既定では無効になっています。この関数を呼び出す前に AdjustTokenPrivileges 関数を使用して特権を有効にし、関数呼び出し後に特権を無効にしてください。詳細については、以下のコード例を参照してください。
Examples
このサンプルでは、システム時刻特権を有効にする方法、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");
}
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// api-ms-win-core-sysinfo-l1-2-4.dll
#include <windows.h>
BOOL SetSystemTimeAdjustmentPrecise(
ULONGLONG dwTimeAdjustment,
BOOL bTimeAdjustmentDisabled
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("api-ms-win-core-sysinfo-l1-2-4.dll", SetLastError = true, ExactSpelling = true)]
static extern bool SetSystemTimeAdjustmentPrecise(
ulong dwTimeAdjustment, // ULONGLONG
bool bTimeAdjustmentDisabled // BOOL
);<DllImport("api-ms-win-core-sysinfo-l1-2-4.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SetSystemTimeAdjustmentPrecise(
dwTimeAdjustment As ULong, ' ULONGLONG
bTimeAdjustmentDisabled As Boolean ' BOOL
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' dwTimeAdjustment : ULONGLONG
' bTimeAdjustmentDisabled : BOOL
Declare PtrSafe Function SetSystemTimeAdjustmentPrecise Lib "api-ms-win-core-sysinfo-l1-2-4" ( _
ByVal dwTimeAdjustment As LongLong, _
ByVal bTimeAdjustmentDisabled As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
SetSystemTimeAdjustmentPrecise = ctypes.windll.LoadLibrary("api-ms-win-core-sysinfo-l1-2-4.dll").SetSystemTimeAdjustmentPrecise
SetSystemTimeAdjustmentPrecise.restype = wintypes.BOOL
SetSystemTimeAdjustmentPrecise.argtypes = [
ctypes.c_ulonglong, # dwTimeAdjustment : ULONGLONG
wintypes.BOOL, # bTimeAdjustmentDisabled : BOOL
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('api-ms-win-core-sysinfo-l1-2-4.dll')
SetSystemTimeAdjustmentPrecise = Fiddle::Function.new(
lib['SetSystemTimeAdjustmentPrecise'],
[
-Fiddle::TYPE_LONG_LONG, # dwTimeAdjustment : ULONGLONG
Fiddle::TYPE_INT, # bTimeAdjustmentDisabled : BOOL
],
Fiddle::TYPE_INT)#[link(name = "api-ms-win-core-sysinfo-l1-2-4")]
extern "system" {
fn SetSystemTimeAdjustmentPrecise(
dwTimeAdjustment: u64, // ULONGLONG
bTimeAdjustmentDisabled: i32 // BOOL
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("api-ms-win-core-sysinfo-l1-2-4.dll", SetLastError = true)]
public static extern bool SetSystemTimeAdjustmentPrecise(ulong dwTimeAdjustment, bool bTimeAdjustmentDisabled);
"@
$api = Add-Type -MemberDefinition $sig -Name 'api-ms-win-core-sysinfo-l1-2-4_SetSystemTimeAdjustmentPrecise' -Namespace Win32 -PassThru
# $api::SetSystemTimeAdjustmentPrecise(dwTimeAdjustment, bTimeAdjustmentDisabled)#uselib "api-ms-win-core-sysinfo-l1-2-4.dll"
#func global SetSystemTimeAdjustmentPrecise "SetSystemTimeAdjustmentPrecise" sptr, sptr
; SetSystemTimeAdjustmentPrecise dwTimeAdjustment, bTimeAdjustmentDisabled ; 戻り値は stat
; dwTimeAdjustment : ULONGLONG -> "sptr"
; bTimeAdjustmentDisabled : BOOL -> "sptr"
; ※HSP3.7は int64 引数(64bit値渡し)に非対応です。
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "api-ms-win-core-sysinfo-l1-2-4.dll"
#cfunc global SetSystemTimeAdjustmentPrecise "SetSystemTimeAdjustmentPrecise" int64, int
; res = SetSystemTimeAdjustmentPrecise(dwTimeAdjustment, bTimeAdjustmentDisabled)
; dwTimeAdjustment : ULONGLONG -> "int64"
; bTimeAdjustmentDisabled : BOOL -> "int"
; ※int64 引数の DLL 値渡しは x64 ランタイム(hsp3_64)のみ対応(x86 は未対応)。; BOOL SetSystemTimeAdjustmentPrecise(ULONGLONG dwTimeAdjustment, BOOL bTimeAdjustmentDisabled)
#uselib "api-ms-win-core-sysinfo-l1-2-4.dll"
#cfunc global SetSystemTimeAdjustmentPrecise "SetSystemTimeAdjustmentPrecise" int64, int
; res = SetSystemTimeAdjustmentPrecise(dwTimeAdjustment, bTimeAdjustmentDisabled)
; dwTimeAdjustment : ULONGLONG -> "int64"
; bTimeAdjustmentDisabled : BOOL -> "int"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
api_ms_win_core_sysinfo_l1_2_4 = windows.NewLazySystemDLL("api-ms-win-core-sysinfo-l1-2-4.dll")
procSetSystemTimeAdjustmentPrecise = api_ms_win_core_sysinfo_l1_2_4.NewProc("SetSystemTimeAdjustmentPrecise")
)
// dwTimeAdjustment (ULONGLONG), bTimeAdjustmentDisabled (BOOL)
r1, _, err := procSetSystemTimeAdjustmentPrecise.Call(
uintptr(dwTimeAdjustment),
uintptr(bTimeAdjustmentDisabled),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction SetSystemTimeAdjustmentPrecise(
dwTimeAdjustment: UInt64; // ULONGLONG
bTimeAdjustmentDisabled: BOOL // BOOL
): BOOL; stdcall;
external 'api-ms-win-core-sysinfo-l1-2-4.dll' name 'SetSystemTimeAdjustmentPrecise';result := DllCall("api-ms-win-core-sysinfo-l1-2-4\SetSystemTimeAdjustmentPrecise"
, "Int64", dwTimeAdjustment ; ULONGLONG
, "Int", bTimeAdjustmentDisabled ; BOOL
, "Int") ; return: BOOL●SetSystemTimeAdjustmentPrecise(dwTimeAdjustment, bTimeAdjustmentDisabled) = DLL("api-ms-win-core-sysinfo-l1-2-4.dll", "bool SetSystemTimeAdjustmentPrecise(qword, bool)")
# 呼び出し: SetSystemTimeAdjustmentPrecise(dwTimeAdjustment, bTimeAdjustmentDisabled)
# dwTimeAdjustment : ULONGLONG -> "qword"
# bTimeAdjustmentDisabled : BOOL -> "bool"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "api-ms-win-core-sysinfo-l1-2-4" fn SetSystemTimeAdjustmentPrecise(
dwTimeAdjustment: u64, // ULONGLONG
bTimeAdjustmentDisabled: i32 // BOOL
) callconv(std.os.windows.WINAPI) i32;proc SetSystemTimeAdjustmentPrecise(
dwTimeAdjustment: uint64, # ULONGLONG
bTimeAdjustmentDisabled: int32 # BOOL
): int32 {.importc: "SetSystemTimeAdjustmentPrecise", stdcall, dynlib: "api-ms-win-core-sysinfo-l1-2-4.dll".}pragma(lib, "api-ms-win-core-sysinfo-l1-2-4");
extern(Windows)
int SetSystemTimeAdjustmentPrecise(
ulong dwTimeAdjustment, // ULONGLONG
int bTimeAdjustmentDisabled // BOOL
);ccall((:SetSystemTimeAdjustmentPrecise, "api-ms-win-core-sysinfo-l1-2-4.dll"), stdcall, Int32,
(UInt64, Int32),
dwTimeAdjustment, bTimeAdjustmentDisabled)
# dwTimeAdjustment : ULONGLONG -> UInt64
# bTimeAdjustmentDisabled : BOOL -> Int32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t SetSystemTimeAdjustmentPrecise(
uint64_t dwTimeAdjustment,
int32_t bTimeAdjustmentDisabled);
]]
local api-ms-win-core-sysinfo-l1-2-4 = ffi.load("api-ms-win-core-sysinfo-l1-2-4")
-- api-ms-win-core-sysinfo-l1-2-4.SetSystemTimeAdjustmentPrecise(dwTimeAdjustment, bTimeAdjustmentDisabled)
-- dwTimeAdjustment : ULONGLONG
-- bTimeAdjustmentDisabled : BOOL
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('api-ms-win-core-sysinfo-l1-2-4.dll');
const SetSystemTimeAdjustmentPrecise = lib.func('__stdcall', 'SetSystemTimeAdjustmentPrecise', 'int32_t', ['uint64_t', 'int32_t']);
// SetSystemTimeAdjustmentPrecise(dwTimeAdjustment, bTimeAdjustmentDisabled)
// dwTimeAdjustment : ULONGLONG -> 'uint64_t'
// bTimeAdjustmentDisabled : BOOL -> 'int32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("api-ms-win-core-sysinfo-l1-2-4.dll", {
SetSystemTimeAdjustmentPrecise: { parameters: ["u64", "i32"], result: "i32" },
});
// lib.symbols.SetSystemTimeAdjustmentPrecise(dwTimeAdjustment, bTimeAdjustmentDisabled)
// dwTimeAdjustment : ULONGLONG -> "u64"
// bTimeAdjustmentDisabled : BOOL -> "i32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t SetSystemTimeAdjustmentPrecise(
uint64_t dwTimeAdjustment,
int32_t bTimeAdjustmentDisabled);
C, "api-ms-win-core-sysinfo-l1-2-4.dll");
// $ffi->SetSystemTimeAdjustmentPrecise(dwTimeAdjustment, bTimeAdjustmentDisabled);
// dwTimeAdjustment : ULONGLONG
// bTimeAdjustmentDisabled : BOOL
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;
public interface Api-ms-win-core-sysinfo-l1-2-4 extends StdCallLibrary {
Api-ms-win-core-sysinfo-l1-2-4 INSTANCE = Native.load("api-ms-win-core-sysinfo-l1-2-4", Api-ms-win-core-sysinfo-l1-2-4.class);
boolean SetSystemTimeAdjustmentPrecise(
long dwTimeAdjustment, // ULONGLONG
boolean bTimeAdjustmentDisabled // BOOL
);
}@[Link("api-ms-win-core-sysinfo-l1-2-4")]
lib Libapi-ms-win-core-sysinfo-l1-2-4
fun SetSystemTimeAdjustmentPrecise = SetSystemTimeAdjustmentPrecise(
dwTimeAdjustment : UInt64, # ULONGLONG
bTimeAdjustmentDisabled : Int32 # BOOL
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef SetSystemTimeAdjustmentPreciseNative = Int32 Function(Uint64, Int32);
typedef SetSystemTimeAdjustmentPreciseDart = int Function(int, int);
final SetSystemTimeAdjustmentPrecise = DynamicLibrary.open('api-ms-win-core-sysinfo-l1-2-4.dll')
.lookupFunction<SetSystemTimeAdjustmentPreciseNative, SetSystemTimeAdjustmentPreciseDart>('SetSystemTimeAdjustmentPrecise');
// dwTimeAdjustment : ULONGLONG -> Uint64
// bTimeAdjustmentDisabled : BOOL -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function SetSystemTimeAdjustmentPrecise(
dwTimeAdjustment: UInt64; // ULONGLONG
bTimeAdjustmentDisabled: BOOL // BOOL
): BOOL; stdcall;
external 'api-ms-win-core-sysinfo-l1-2-4.dll' name 'SetSystemTimeAdjustmentPrecise';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "SetSystemTimeAdjustmentPrecise"
c_SetSystemTimeAdjustmentPrecise :: Word64 -> CInt -> IO CInt
-- dwTimeAdjustment : ULONGLONG -> Word64
-- bTimeAdjustmentDisabled : BOOL -> CInt
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let setsystemtimeadjustmentprecise =
foreign "SetSystemTimeAdjustmentPrecise"
(uint64_t @-> int32_t @-> returning int32_t)
(* dwTimeAdjustment : ULONGLONG -> uint64_t *)
(* bTimeAdjustmentDisabled : BOOL -> int32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library api-ms-win-core-sysinfo-l1-2-4 (t "api-ms-win-core-sysinfo-l1-2-4.dll"))
(cffi:use-foreign-library api-ms-win-core-sysinfo-l1-2-4)
(cffi:defcfun ("SetSystemTimeAdjustmentPrecise" set-system-time-adjustment-precise :convention :stdcall) :int32
(dw-time-adjustment :uint64) ; ULONGLONG
(b-time-adjustment-disabled :int32)) ; BOOL
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $SetSystemTimeAdjustmentPrecise = Win32::API::More->new('api-ms-win-core-sysinfo-l1-2-4',
'BOOL SetSystemTimeAdjustmentPrecise(UINT64 dwTimeAdjustment, BOOL bTimeAdjustmentDisabled)');
# my $ret = $SetSystemTimeAdjustmentPrecise->Call($dwTimeAdjustment, $bTimeAdjustmentDisabled);
# dwTimeAdjustment : ULONGLONG -> UINT64
# bTimeAdjustmentDisabled : BOOL -> BOOL
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f GetSystemTimeAdjustmentPrecise — システム時刻の高精度な周期的調整値と分解能を取得する。