Skip to content

Win32 Power Utilities

Three tiny C utilities — shutdown, restart, and logoff — each a confirmation dialog followed by one call to ExitWindowsEx.

Published Jan 4, 1998

The ExitWindowsEx family in its purest form: three console programs, each showing a Yes/No warning ("You will lose any unsaved information!") and calling the API with the appropriate flag on Yes. Shutdown uses EWX_FORCE | EWX_SHUTDOWN, restart uses EWX_FORCE | EWX_REBOOT, and logoff uses plain EWX_LOGOFF - the logoff variant personalizes the dialog title with the machine and user names via GetComputerName/GetUserName. Together they are the minimal reference for power-management shortcuts; the same flags work from any process that has the right privileges (on modern systems, shutting down or rebooting requires SE_SHUTDOWN_NAME).

// Shutdown.C
#include <windows.h>

int main()
{
	if((MessageBox(NULL,"WARNING - You will lose any unsaved information!","Shut Down The Computer?",4+256)) == 6)
	{
		return ExitWindowsEx(EWX_FORCE | EWX_SHUTDOWN,0);
	}
	return 0;
}
// Restart.C
#include <windows.h>

int main()
{
	if((MessageBox(NULL,"WARNING - You will lose any unsaved information!","Restart Windows?",4+256)) == 6)
	{
		return ExitWindowsEx(EWX_FORCE | EWX_REBOOT,0);
	}
	return 0;
}
// Logoff.C
#include <windows.h>
#include <stdio.h>

extern char *Get_UserName(void);
extern char *Get_MachineName(void);

int main()
{
	static char UserInfo[255];
	sprintf(UserInfo,"Log Off %s / %s",Get_MachineName(),Get_UserName());
	if((MessageBox(NULL,"WARNING - You will lose any unsaved information!",UserInfo,4+256)) == 6)
	{
		return ExitWindowsEx(EWX_LOGOFF,0);
	}
	return 0;
}

extern char *Get_MachineName(void)
{
	static	char StrFunction [64][257];
	static int StrCnt;
	static char A[257];
	static	DWORD  b;
	if (++StrCnt==64) StrCnt = 0;
	b=256;
	GetComputerName(A,&b);
	sprintf(StrFunction[StrCnt],"%s",A);
	return StrFunction[StrCnt];
}

extern char *Get_UserName(void)
{
	static	char StrFunction [64][257];
	static int StrCnt;
	static char A[257];
	static	DWORD  b;
	if (++StrCnt==64) StrCnt = 0;
	b=256;
	GetUserName(A,&b);
	sprintf(StrFunction[StrCnt],"%s",A);
	return StrFunction[StrCnt];
}

The message-box style is MB_ICONQUESTION + MB_YESNO (4+256), and IDYES is 6 - hence the == 6 check. Note EWX_FORCE kills any app that refuses to close, which is exactly what you want in a kiosk-style utility but not in everyday use; drop it if you prefer the normal "apps are still running" prompt chain.

An unhandled error has occurred. Reload 🗙