Example Code

Calling SetSystemTime Function from Windows Kernel32.dll Using LabVIEW

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

Code and Documents

Attachment

Download All

Overview

This example demonstrates how to programmatically set your PCs system time by calling the SetSystemTIme function from the Windows' kernel32.dll.

 

Description

In kernel32.dll, there is a function called SetSystemTime defined as follows: 

BOOL WINAPI SetSystemTime(

  _In_  const SYSTEMTIME *lpSystemTime

); 

There are two main things to notice about this function.

  1. It returns a boolean data type.
  2. The function expects a SYSTEMTIME data type. 

As such, how do we configure the Call Library Function Node VI such that the dll call behaves appropriately?

 

When configuring the return type you will notice that there is no boolean data type option. In this case we will set the return type as Signe 8-bit as shown in the image below. The boolean is returned in the signed bit of the number and can be indexed out.

ReturnType.JPG

 

What about the data type SYSTEMTIME? What is this?  Reading the documentation for the function on MSDN shows that SYSTEMTIME is a Typed Defined Structure defined as follows:

 

typedef struct _SYSTEMTIME {

  WORD wYear;

  WORD wMonth;

  WORD wDayOfWeek;

  WORD wDay;

  WORD wHour;

  WORD wMinute;

  WORD wSecond;

  WORD wMilliseconds;

} SYSTEMTIME, *PSYSTEMTIME;

 

We can implement a structure in LabVIEW with a cluster.  In the definition each part of the system time is stored as a WORD or Unsigned 16-bit integer.  Thus we can implement this paramter in LabVIEW by creating a cluster of eight U16 controls. Now that we know what to pass in from LabVIEW how do we configure the Call Library node?  In LabVIEW we pass clusters/structures as an Adapt To Type datatype.  Leaving the default as Handles By Value will meet our needs.

SystemTime.JPG 

Requirements

  • LabVIEW 2012 (or compatible)


Steps to Implement or Execute Code

  1. In Windows Vista and later, you must open LabVIEW as an Administrator.
  2. You must open the VI through File >> Open while running as an Administrator.
  3. Run the program.
  4. Configure the SYSTEMTIME Structure/Cluster.
  5. Click the 'Change Time' boolean to change System Time.
  6. Click 'Stop' boolean to stop running VI.

 

Additional Information or References
VI Block Diagram

1.png 

 

 

**This document has been updated to meet the current required format for the NI Code Exchange.**

 

 

 

 

Regards,
Isaac S.
Applications Engineer
National Instruments

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.