Commits

overlord  committed 6954f5c

Daybo Logic shutdown utility - a standard way to shut down the operating system on different machines.

  • Participants

Comments (0)

Files changed (21)

File Win32/shtdwn.c

+#include <Windows.H>
+#pragma hdrstop
+
+#include "shtdwn.h"
+
+BOOL Shutdown(void(*ErrCB)(const char* Message), const BYTE Mode)
+{
+  DWORD winShutdownMode;
+  /* For Windows NT, we need to do some mental requests, these
+  always fail on Chicago & Memphis */
+  DWORD winVersion = GetVersion();
+  if (winVersion < 0x80000000) /* Windows NT? */
+  {
+    HANDLE hToken;
+    TOKEN_PRIVILEGES tkp;
+
+    // Get a token for this process.
+
+    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
+    {
+      if (ErrCB) ErrCB("OpenProcessToken");
+      return FALSE;
+    }
+
+    // Get the LUID for the shutdown privilege.
+
+    LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
+
+    tkp.PrivilegeCount = 1;  // one privilege to set
+    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
+
+    // Get the shutdown privilege for this process.
+
+
+    AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
+
+    // Cannot test the return value of AdjustTokenPrivileges.
+
+    if (GetLastError() != ERROR_SUCCESS)
+    {
+      if (ErrCB) ErrCB("AdjustTokenPrivileges");
+      return FALSE;
+    }
+  } /*(Windows NT)*/
+
+  // Shut down the system and force all applications to close.
+  if ( Mode == MODE_SHUTDOWN ) winShutdownMode = EWX_SHUTDOWN;
+  else if ( Mode == MODE_POWERDOWN ) winShutdownMode = EWX_POWEROFF;
+  else if ( Mode == MODE_REBOOT ) winShutdownMode = EWX_SHUTDOWN;
+  else
+  {
+    if (ErrCB) ErrCB("Wrong flag - overlord@daybologic.co.uk");
+    return FALSE;
+  }
+  if (!ExitWindowsEx(winShutdownMode, 0))
+  {
+    if (ErrCB) ErrCB("ExitWindowsEx");
+    return FALSE;
+  }
+
+  return TRUE;
+}

File Win32/shtdwn.h

+#ifndef __INC_WIN32_SHUTDOWN_H
+#define __INC_WIN32_SHUTDOWN_H
+/*-------------------------------------------------------------------------*/
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+BOOL Shutdown(void(*ErrCB)(const char* Message), const BYTE Mode);
+
+#ifdef __cplusplus
+}
+#endif
+/*-------------------------------------------------------------------------*/
+#endif /*!__INC_WIN32_SHUTDOWN_H*/

File res/arrived.wav

Binary file added.

File res/goingdown.wav

Binary file added.

File res/immediately.wav

Binary file added.

File res/in.wav

Binary file added.

File res/min.wav

Binary file added.

File res/mins.wav

Binary file added.

File res/secs.wav

Binary file added.
+/* Portable shutdown module */
+
+#pragma warn -pch /* These are not supposed to be precompiled headers! */
+#pragma hdrstop
+
+#ifdef __WIN32__
+#  include "Win32/shtdwn.c"
+#else
+#  error ("There is no code written for non-Win32 platforms")
+#endif
+#ifndef __INC_SHUTDOWN_H
+#define __INC_SHUTDOWN_H
+/*-------------------------------------------------------------------------*/
+/* Portable shutdown header */
+
+#ifdef __WIN32__
+#  include "Win32/shtdwn.h"
+#else
+#  error ("Header not currently available for non-Win32")
+#endif
+
+/* The 'Mode' parameter takes ONE of these values */
+#define MODE_SHUTDOWN (0)
+#define MODE_POWERDOWN (1)
+#define MODE_REBOOT (2)
+/*-------------------------------------------------------------------------*/
+#endif /*__INC_SHUTDOWN_H*/

File shutdown.DSW

Binary file added.

File shutdown.OBR

Binary file added.
+#include <stdio.h>
+#include <stdlib.h>
+#pragma hdrstop
+
+#include "types.h"
+#include "shtdwn.h"
+
+typedef struct _S_SHUTDOWNINFO {
+  time_t ShutdownTime; /* The time at which system shutdown will occur */
+  BYTE Mode; /* Can be MODE_SHUTDOWN, MODE_POWERDOWN or MODE_REBOOT */
+} S_SHUTDOWNINFO, *PS_SHUTDOWNINFO;
+
+fstatic void ErrorPrinter(const char* Message);
+static void PrintUsage(void);
+static void ShutdownMessage(const PS_SHUTDOWNINFO PShutdownInfo);
+/*-------------------------------------------------------------------------*/
+int main()
+{
+  PrintUsage();
+  Shutdown(ErrorPrinter, 0xFF);
+  return EXIT_SUCCESS;
+}
+/*-------------------------------------------------------------------------*/
+static void ErrorPrinter(const char* Message)
+{
+  printf("Shutdown error: %s", Message);
+}
+/*-------------------------------------------------------------------------*/
+static void PrintUsage()
+{
+  printf("shutdown [-] [-n|-p|-r|-k] [-o[-n]] time [warning-message ...]\n");
+}
+/*-------------------------------------------------------------------------*/
+static void ShutdownMessage(const PS_SHUTDOWNINFO PShutdownInfo, const time_t CurrentTime)
+{
+  time_t timeLeft = PShutdownInfo->ShutdownTime - CurrentTime;
+
+  printf("*** ");
+  if (!timeLeft) /* No seconds left? */
+    printf("FINAL ");
+  printf("System shutdown message ***\n");
+  printf("System going down ");
+  if (!timeLeft) /* No seconds left? */
+  {
+    printf("IMMEDIATELY");
+  }
+  else
+  {
+    printf("

File shutdown.def

+NAME shutdown

File shutdown.exe

Binary file added.

File shutdown.ide

Binary file added.

File shutdown.mbt

Binary file added.

File shutdown.mrt

Binary file added.

File shutdown.res

Binary file added.
+#ifndef __INC_TYPES_H
+#define __INC_TYPES_H
+
+typedef unsigned char BYTE;
+typedef unsigned int BOOL;
+typedef unsigned long DWORD;
+typedef unsigned short WORD;
+
+#define TRUE (1U)
+#define FALSE (!TRUE)
+
+#endif /*!__INC_TYPES_H*/