Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

i want to shutdown remote computer in a local network using MFC applications. i found that we can shutdown using InitiateSystemShutdown(). But shows access is denied error. Can anybody help me?

i tried the below code
C++
BOOL MySystemShutdown( LPTSTR lpMsg )
 {
 	HANDLE hToken;              
 	TOKEN_PRIVILEGES tkp;       
 	BOOL fResult;               // system shutdown flag 
 	
 	// Get the current process token handle so we can get shutdown 
 	// privilege. 
 	
 	if (!OpenProcessToken(GetCurrentProcess(), 
         TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) 
 		return FALSE; 
 	
 	// Get the LUID for shutdown privilege. 
 	
 	LookupPrivilegeValue(NULL,SE_REMOTE_SHUTDOWN_NAME , 
         &tkp.Privileges[0].Luid); 
 	
 	tkp.PrivilegeCount = 1;  // one privilege to set    
 	tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; 
 	
 	// Get 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) 
 		return FALSE; 
 	
 	// Display the shutdown dialog box and start the countdown. 
 	
 	fResult = InitiateSystemShutdown( 
 		("remote computer name "),    // shut down remote computer 
  		lpMsg,   // message for user
  		10,      // time-out period, in seconds 
  		FALSE,   // ask user to close apps 
  		TRUE);   // reboot after shutdown 
 	
        int s = GetLastError();
        CString ss;
        ss.Format("%d",s);
        AfxMessageBox(ss);

 	if (!fResult) 
 		return FALSE; 
 	
 	// Disable shutdown privilege. 
 	
 	tkp.Privileges[0].Attributes = 0; 
 	AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, 
         (PTOKEN_PRIVILEGES) NULL, 0); 
 	
 	return TRUE; 
 }

Thanks in advance.
Posted
Updated 9-Jan-12 21:41pm
v2
Comments
LaxmikantYadav 7-Oct-11 1:00am    
Simply Try This, InitiateSystemShutdown(\\127.0.01, NULL, 0, true, true);

It will restart your local pc :)

1 solution

Hi ,

I Got the solution for the above problem. The code works fine if we have the access privilege on the computer which we are trying to shutdown. After that run the program.
Thanks :-)
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900