delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/1998/01/14/17:19:43

From: jt AT boxhill DOT com (Jason Tishler)
Subject: Re: Windows Shutdown
14 Jan 1998 17:19:43 -0800 :
Message-ID: <34BBB1AA.48178C05.cygnus.gnu-win32@boxhill.com>
References: <34BAAE09 DOT 29B7372E AT bensonlaw DOT com>
Mime-Version: 1.0
To: Bryon Roche <broche AT bensonlaw DOT com>
Cc: "gnu-win32 AT cygnus DOT com" <gnu-win32 AT cygnus DOT com>

Bryon,

The key is setting the appropriate privilege and then calling either
ExitWindowsEx or InitiateSystemShutdown.

Attached is a snippet from my code (sorry for the MFC-isms).  Hope it
helps.

Jason

-- 
Jason Tishler                        Phone: +1 (212) 989-4455 ext. 2120
Box Hill Systems Corporation         Fax:   +1 (212) 989-6817
161 Avenue of the Americas           Email: jt AT boxhill DOT com
New York, NY 10013 USA               WWW:   http://www.boxhill.com

--- Cut Here ---
BOOL
CFmgView::Reboot()
{
	// Get name of host to reboot.
	CString aHost = GetDocument()->GetHost();

	// Determine whether or not the reboot is for the local machine
	char aLocalComputerName[MAX_COMPUTERNAME_LENGTH + 1];
	DWORD aLocalComputerNameLength = MAX_COMPUTERNAME_LENGTH + 1;
	GetComputerName(aLocalComputerName, &aLocalComputerNameLength);
	CString aPrivilegeName = (aHost.CompareNoCase(aLocalComputerName) == 0) ?
		SE_SHUTDOWN_NAME : SE_REMOTE_SHUTDOWN_NAME;

	// Get a token for this process. 
	HANDLE aToken;  
	BOOL aStatus = OpenProcessToken(
		GetCurrentProcess(), 
		TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
		&aToken);
	if (!aStatus)
	{
		CString aText;
		aText.Format("OpenProcessToken failed with last error %ld", GetLastError());
		MessageBox(aText, "OpenProcessToken Error", MB_OK | MB_ICONEXCLAMATION);
		return FALSE;
	}
 
	// Get the LUID for the appropriate shutdown privilege. 
	TOKEN_PRIVILEGES aTokenPrivileges; 
	aStatus = LookupPrivilegeValue(
		aHost,
		aPrivilegeName,
		&aTokenPrivileges.Privileges[0].Luid); 
	if (!aStatus)
	{
		CString aText;
		aText.Format("LookupPrivilegeValue failed with last error %ld", GetLastError());
		MessageBox(aText, "LookupPrivilegeValue Error", MB_OK | MB_ICONEXCLAMATION);
		CloseHandle(aToken);
		return FALSE;
	}

	// Set the appropriate shutdown privileges for this process. 
	aTokenPrivileges.PrivilegeCount = 1;
	aTokenPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; 
	aStatus = AdjustTokenPrivileges(
		aToken,
		FALSE,
		&aTokenPrivileges,
		0, 
		(PTOKEN_PRIVILEGES) 0,
		0); 
	if (GetLastError() != ERROR_SUCCESS) // Testing the return value is NOT sufficient
	{
		CString aText;
		aText.Format("AdjustTokenPrivileges failed with last error %ld", GetLastError());
		MessageBox(aText, "AdjustTokenPrivileges Error", MB_OK | MB_ICONEXCLAMATION);
		CloseHandle(aToken);
		return FALSE;
	}

	// Reboot the host.
	aStatus = ::InitiateSystemShutdown(
		(LPTSTR) (LPCTSTR) aHost,
		0,
		0,
		TRUE,
		TRUE);
	if (!aStatus)
	{
		CString aText;
		aText.Format("InitiateSystemShutdown failed with last error %ld", GetLastError());
		MessageBox(aText, "InitiateSystemShutdown Error", MB_OK | MB_ICONEXCLAMATION);
	}

	CloseHandle(aToken);
	return aStatus;
}
-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request AT cygnus DOT com" with one line of text: "help".

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019