SetSystemTimeAdjustment disable clock adjust rather than changing the RTC clock rate

Tom Stoltz 20 Reputation points
2025-01-20T03:59:44.49+00:00

I need to override w32tm to adjust the DateTime clock-rate in C#. When I call the kernel32 API:

SetSystemTimeAdjustment(ref dwTimeAdjustment, ref bTimeAdjustmentDisabled);

I get a return of true, indicating it worked, but when I read back the results with GetSystemTimeAdjustment, lpTimeIncrement = lpTimeAdjustment, and the lpTimeAdjustmentDisabled is true, when I passed false in, indicating the Set operation disabled the clock adjust, rather than changing the rate.

  • I have successfully set the SE_SYSTEMTIME_NAME privilege. When I call SetSystemTimeAdjustment without setting the privilege, it returns false as expected, and has no change on the system (keeping the w32tm setting). When I set the privilege, it returns true, and changes the rate (overriding w32tm) but it always results in the adjust interval being 156250, rather than the value commanded.
  • I have tried with w32tm stopped as well, to make sure it wasn't fighting. Same result.

The function call looks like this:

WinRTClk.SetClockRate(156123); // Test to see if the clock can be sped up.

Here is the class:

public static class WinRTClk

{

    public static UInt32 ClkInterval = 0;

    public static UInt32 ClkRate = 0;

    private static bool ClkAdjDisable = false;

    public static double RTC_Adj_Ratio()

    {

        GetClockRate();

        return ((double) ((Int64) ClkRate - ClkInterval)) / ClkInterval;

    }

    public static bool GetClockRate() { return GetSystemTimeAdjustment(ref ClkRate, ref ClkInterval, ref ClkAdjDisable); }

    public static bool SetClockRate(Int32 Offset)

    {

        GetClockRate(); // Update ClkInterval

        UInt32 NewRate = (UInt32) ((Int64) ClkInterval + Offset);

        bool Disable = false;

        return SetSystemTimeAdjustment(ref NewRate, ref Disable);

    }

    // https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtimeadjustment

    [DllImport("kernel32.dll")]

    private static extern bool GetSystemTimeAdjustment(ref UInt32 TimeAdj, ref UInt32 ClkInc, ref bool AdjDisable);

    //https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-setsystemtimeadjustment

    [DllImport("kernel32.dll")]

    private static extern bool SetSystemTimeAdjustment(ref UInt32 TimeAdj, ref bool AdjDisable);

}
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,712 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,217 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 119.2K Reputation points
    2025-01-20T06:52:55.88+00:00

    Try another definition:

    [DllImport("kernel32.dll")]
    private static extern bool SetSystemTimeAdjustment( UInt32 TimeAdj, bool AdjDisable);
    

    And adjust the code.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.