Example Code

Shutdown Windows in LabVIEW (2 Different Methods)

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

Overview

This example illustrates 2 methods for shutting down Windows from within LabVIEW.

 

Description

Method 1:  Using the System Exec VI

Shutdown is achieved by passing the system-level command: cmd.exe /C shutdown -r -m \\computername -t 5 -f

This command translates to shutting down the computer (named computername) with a timeout of 5 seconds, forcing all running applications to close without warning.

 

7.PNG
Method 2:  Using the Call Library Function Node

This method accesses the user32.dll and utilizes the ExitWindowsEx function to perform a Windows shutdown. Drop a Call Library Node (CLN) and configure it as shown in the following picture. uFlags set to zero forces a shutdown of all processes and then logs off the user; setting dwReason to zero indicates the reason for shutdown is related to "some other issue." If the function return is not equal to zero, then the function executed successfully.

 

6.PNG

 

Requirements

  • LabVIEW 2012 (or compatible)

 

Steps to Implement or Execute Code

Method 1

  1. Open System Exec to Shutdown Windows 2012 NIVerified.vi
  2. Input your computer name into the "computername" control
  3. Run the VI AFTER checking that your computer and work in progress is ready for a forced shutdown.

Method 2

  1. Open Call Library Function Node to Shutdown Windows 2012 NIVerified.vi
  2. Set the value of uFlags, dwReason to default value 0
  3. Run the VI

 

Additional Information or References

You should always consider protecting code like this with dialogs that allow users to save and close things a they see fit prior to a shutdown.

 

VI Snippet of System Exec to Shutdown Windows 2012 NIVerified

1.png

 

VI Snippet of Call Library Function Node to Shutdown Windows 2012 NIVerified

5.png

 

 

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

Regards,

Sara Lewandroski
Applications Engineer | National Instruments

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