Subject: Re: Windows Shutdown
From: [email-masked] (Jason Tishler)

14 Jan 1998 17:19:43 -0800
Message-ID: <[email-masked]>
References: <[email-masked]>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
To: Bryon Roche <[email-masked]>
Cc: "[email-masked]" <[email-masked]>

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: [email-masked]
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
"[email-masked]" with one line of text: "help".