Suspend Windows from the Command Line

 

I used to leave my computer up all night, but I've come to realize that this is ecologically unsound. Now I suspend it before going to sleep, but this missed running a daily job that used to run at 03:00 am. The job marks my students' exercises and send me email with the next day's appointments. I thus decided to schedule the task to wakeup my computer at 3:00 am, run the job, and then suspend it again. The Windows scheduler allows you to specify a wakeup option, but not a subsequent suspend. Furthermore, it seems that Windows lacks a way to suspend from the command line (while maintaining the ability to hibernate), and the only free tools on the web are distributed in executable form, so I ended writing a small tool myself.

Here it is.

/*
 * Command to force suspend a Windows computer.
 * Compile with: cl suspend.c advapi32.lib
 *
 * Copyright (c) 2008, Diomidis Spinellis
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include <stdio.h>

#include <windows.h>

/* Return as a string the error description for err */
static char *
wstrerror(LONG err)
{
	static LPVOID lpMsgBuf;

	if (lpMsgBuf)
		LocalFree(lpMsgBuf);
	FormatMessage(
	    FORMAT_MESSAGE_ALLOCATE_BUFFER |
	    FORMAT_MESSAGE_FROM_SYSTEM |
	    FORMAT_MESSAGE_IGNORE_INSERTS,
	    NULL, err,
	    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
	    (LPTSTR) &lpMsgBuf, 0, NULL);
	return lpMsgBuf;
}

main()
{
	HANDLE tok;
	TOKEN_PRIVILEGES priv;

	if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES |
	    TOKEN_QUERY, &tok) == 0) {
		fprintf(stderr, "OpenProcessToken: %s\n",
		    wstrerror(GetLastError()));
		return (1);
	}

	// Get the LUID for shutdown privilege.
	if (LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
	    &priv.Privileges[0].Luid) == 0) {
		fprintf(stderr, "LookupPrivilegeValue: %s\n",
		    wstrerror(GetLastError()));
		return (1);
	}

	// Enable AdjustTokenPrivileges.
	priv.PrivilegeCount = 1;
	priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

	if (AdjustTokenPrivileges(tok, FALSE, &priv, 0, (PTOKEN_PRIVILEGES)NULL,
	    0) == 0 || GetLastError() != ERROR_SUCCESS) {
		fprintf(stderr, "AdjustTokenPrivileges: %s\n",
		    wstrerror(GetLastError()));
		return (1);
	}

	// Force suspend
	if (SetSystemPowerState(TRUE, TRUE) == 0) {
		fprintf(stderr, "SetSystemPowerState: %s\n",
		    wstrerror(GetLastError()));
		return (1);
	}
	return (0);
}
My colleague Dimitris Staikos recently observed that in systems software a large part of the code concerns error handling. This is certainly true in this case.

Comments   Toot! Share


Last modified: Monday, October 6, 2008 7:25 pm

Creative Commons Licence BY NC

Unless otherwise expressly stated, all original material on this page created by Diomidis Spinellis is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.