Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

SetSystemTime (coredll)
 

coredll is for smart devices, not desktop Windows. Therefore, this information only applies to code using the .NET Compact Framework. To see if information for SetSystemTime in other DLLs exists, click on Find References to the right.

.
Summary
Sets the date and time on the system.

C# Signature:

[DllImport("coredll.dll", SetLastError=true)]
   static extern bool SetSystemTime(ref SYSTEMTIME time);

VB Signature:

<DllImport("coredll.dll")> _
   Private Shared Function SetSystemTime(ByRef time As SYSTEMTIME) As Boolean
   End Function

User-Defined Types C#:

public struct SYSTEMTIME
{
     public short year;
     public short month;
     public short dayOfWeek;
     public short day;
     public short hour;
     public short minute;
     public short second;
     public short milliseconds;
}

User-Defined Types VB:

Private Structure SYSTEMTIME
     Public Year As Short
     Public Month As Short
     Public DayOfWeek As Short
     Public Day As Short
     Public Hour As Short
     Public Minute As Short
     Public Second As Short
     Public Milliseconds As Short
End Structure

Notes:

This function sets the system time ignoring locale settings and summer time.

To set the date and time with consideration to the locale settings use SetLocalTime.

Tips & Tricks:

Please add some!

Sample Code:

Imports System.Runtime.InteropServices

Public Class Form1
    Inherits System.Windows.Forms.Form
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu

#Region " Windows Form Designer generated code "

    Public Sub New()
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()

    'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    MyBase.Dispose(disposing)
    End Sub

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Private Sub InitializeComponent()
    Me.MainMenu1 = New System.Windows.Forms.MainMenu
    Me.Button1 = New System.Windows.Forms.Button
    '
    'Button1
    '
    Me.Button1.Location = New System.Drawing.Point(56, 160)
    Me.Button1.Size = New System.Drawing.Size(128, 64)
    Me.Button1.Text = "Button1"
    '
    'Form1
    '
    Me.Controls.Add(Me.Button1)
    Me.Menu = Me.MainMenu1
    Me.Text = "Form1"

    End Sub

#End Region

    Private Structure SYSTEMTIME
    Public Year As Short
    Public Month As Short
    Public DayOfWeek As Short
    Public Day As Short
    Public Hour As Short
    Public Minute As Short
    Public Second As Short
    Public Milliseconds As Short
    End Structure

    <DllImport("coredll.dll")> _
    Private Shared Function SetSystemTime(ByRef time As SYSTEMTIME) As Boolean
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim idag As DateTime = Now().AddHours(1).AddMinutes(2).ToUniversalTime
    Dim s As New SYSTEMTIME
    s.Year = idag.Year
    s.Month = idag.Month
    s.DayOfWeek = idag.DayOfWeek
    s.Day = idag.Day
    s.Hour = idag.Hour
    s.Minute = idag.Minute
    s.Second = idag.Second
    s.Milliseconds = idag.Millisecond

    SetSystemTime(s)
    End Sub
End Class

C# Sample Code

using System.Runtime.InteropServices;

public class Form1 : System.Windows.Forms.Form
{
     internal System.Windows.Forms.Button Button1;
     internal System.Windows.Forms.MainMenu MainMenu1;

     #region " Windows Form Designer generated code "

     public Form1() : base()
     {

     //This call is required by the Windows Form Designer.
     InitializeComponent();

     //Add any initialization after the InitializeComponent() call

     }

     //Form overrides dispose to clean up the component list.
     protected override void Dispose(bool disposing)
    {
     base.Dispose(disposing);
     }

     //NOTE: The following procedure is required by the Windows Form Designer
     //It can be modified using the Windows Form Designer.
     //Do not modify it using the code editor.
     private void InitializeComponent()
     {
     this.MainMenu1 = new System.Windows.Forms.MainMenu();
     this.Button1 = new System.Windows.Forms.Button();
     //
     //Button1
     //
     this.Button1.Location = new System.Drawing.Point(56, 160);
     this.Button1.Size = new System.Drawing.Size(128, 64);
    this.Button1.Text = "Button1";
     //
     //Form1
     //
     this.Controls.Add(this.Button1);
     this.Menu = this.MainMenu1;
     this.Text = "Form1";

     }

     #endregion

     private struct SYSTEMTIME
     {
     public short Year;
     public short Month;
     public short DayOfWeek;
     public short Day;
     public short Hour;
     public short Minute;
     public short Second;
     public short Milliseconds;
     }

     [DllImport("coredll.dll")]
     private static extern bool SetSystemTime(ref SYSTEMTIME time);

     private void Button1_Click(object sender, System.EventArgs e)
     {
    DateTime idag = Now().AddHours(1).AddMinutes(2).ToUniversalTime();
    SYSTEMTIME s = new SYSTEMTIME();
     s.Year = (short)idag.Year;
     s.Month = (short)idag.Month;
     s.DayOfWeek = (short)idag.DayOfWeek;
     s.Day = (short)idag.Day;
     s.Hour = (short)idag.Hour;
     s.Minute = (short)idag.Minute;
     s.Second = (short)idag.Second;
     s.Milliseconds = (short)idag.Millisecond;
     SetSystemTime(ref s);
     }
}

Alternative Managed API:

Do you know one? Please contribute it!

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions