Example Code

Get Windows Shutdown Status 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.

    Hardware

  • Data Acquisition (DAQ)

    Software

  • LabVIEW

    Driver

  • NI DAQmx

Code and Documents

Attachment

Overview
This VI demonstrates how to get Windows shutdown (reboot) state, so your application can finish properly.

 
Description

This VI can be useful when application cannot be run as windows service but must exit properly on shutdown or reboot.


Requirements

  • LabVIEW 2012 (or compatible)


Steps to Implement or Execute Code

  1. Use this VI as a subVI.

 

Additional Information or References
VI Snippet

002.png

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

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

Comments
r_exler
Member
Member
on

For me this VI just creates error (LabVIEW 2013, Win7 64 Bit OS)

Error 1097 occurred at Call Library Function Node in WindowsShutdownStarted.vi

Possible reason(s):

LabVIEW:  An exception occurred within the external code called by a Call Library Function Node. The exception might have corrupted the LabVIEW memory. Save any work to a new location and restart LabVIEW.

GriffinRU
Member
Member
on

Hi,

Try to change Calling convention from "C" to stdcall (WINAPI)...

-Artur

r_exler
Member
Member
on

Hi Artur, at least it worked on an old, slow Win32 system.

On my i7 based Win7/64Bit notebook the application gets killed by windows before it recognises the shutdown being started!

Some notes on my application, might be interresting for others as well: I'm doing durability tests on stepper motors and need the test to run over several months. To prevent the test interrupted I have an UPS system which is capable to power all the hardware including the controlling notebook for about 10 Minutes - this should be enough to overcome most power outages. In case the mains power fails for a longer period I will use this to shut down the test, saving the progress and state to be able to restart after power restores.

Unfortunately the UPS does not provide an software interface to get it's status (e.g. Mains/Battery power) or estimated time to shut down, so the software can just respond to the shutdown initiated by the power supply monitoring software.

I hope there is a similar system call to test for a user log off, which will kill the application as well. Most likely I'll include this in several applications, as I frequently have seen the users to log off or shut down without closing the application both killing the application), leaving external hardware e.g. positioning systems, ... in an unwanted state and/or power supplies turned on (could be dangerous).

GriffinRU
Member
Member
on

Hi,

How often you check Windows status?

As for UPS and Battery I will recommend to check WMI interface:

Example attached.

-Artur

P.S. User status can be obtain from WMI as well.

r_exler
Member
Member
on

I checked every 500ms.But my notebook is fast and closes the user interface within about 200ms after pressing the shutdown button - killing the labview application before it notices anything. Even if it would see the shutdown starting this would be too late.

I'll test your example on my notebook. On my Desktop PC it returns an empty array. Actually I will have to create a solution working with desktop and notebooks as well as supporting at least WinXP and Win7. And as you guess I'm not a programmer familar with windows specific things. Usually I'm programming LabView on Win and C code on embedded applications.

Thanks for your help!

GriffinRU
Member
Member
on

You should check faster, drop down to 10ms. Or write in C a DLL and let windows send you an event... MSDN my friend

Your app should be able to exit properly, unless windows thinks it is a stopped responding application. (You can extract more info by tapping into windows processes...)

Win32_Battery should work for Desktop if you have UPS attached

Win32_PortableBattery should work for laptop only

Win32_ComputerSystem should work for both

Win32_UninterruptiblePowerSupply should work for WinXP

-Artur