ExitWindowsEx_() doesn't reboot

Windows specific forum
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

ExitWindowsEx_() doesn't reboot

Post by Fluid Byte »

I'm currently working on a tool wich makes a couple of changes to the system and should ask the user to reboot in order for changes to take effect. So far, so good. I'm trying to achieve this by using the ExitWindowsEx_() API function. Doesn't work, no matter what kind of flag combinations you set. So I browsed the web and found the following article:

http://support.microsoft.com/default.aspx?scid=kb;en-us;176695

Seems exactly the solution to my problem so I converted the code to PB:

Code: Select all

Procedure AdjustToken()
	Protected tmpLuid.LUID
	Protected tkp.TOKEN_PRIVILEGES
	Protected tkpNewButIgnored.TOKEN_PRIVILEGES
	
	hdlProcessHandle = GetCurrentProcess_()
	OpenProcessToken_(hdlProcessHandle,#TOKEN_ADJUST_PRIVILEGES | #TOKEN_QUERY,@hdlTokenHandle)

	; Get the LUID for shutdown privilege.
	LookupPrivilegeValue_("","SeShutdownPrivilege",tmpLuid)

	tkp\PrivilegeCount = 1    ;' One privilege to set
	tkp\Privileges\LUID\LowPart = tmpLuid
	tkp\Privileges\LUID\HighPart = tmpLuid
	tkp\Privileges\Attributes = #SE_PRIVILEGE_ENABLED
	
	lBufferNeeded = AllocateMemory(200)
	
	; Enable the shutdown privilege in the access token of this process.
	AdjustTokenPrivileges_(hdlTokenHandle,#False,tkp,SizeOf(tkpNewButIgnored),tkpNewButIgnored,lBufferNeeded)

EndProcedure

AdjustToken()

ExitWindowsEx_(#EWX_SHUTDOWN | #EWX_FORCE | #EWX_REBOOT,$FFFF)
Well, if you run that code just nothing will happen. No clue what's wrong here.
Last edited by Fluid Byte on Tue Aug 15, 2006 6:33 pm, edited 3 times in total.
Hi-Toro
Enthusiast
Enthusiast
Posts: 270
Joined: Sat Apr 26, 2003 3:23 pm

Post by Hi-Toro »

I haven't looked into the differences, but this code used to work... might need minor changes for PB 4.0 though:

http://www.purebasic.fr/english/viewtopic.php?t=3808
James Boyd
http://www.hi-toro.com/
Death to the Pixies!
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

Actually, this exactly the same code as the example from the Microsoft site. I just tried the code from the link and it works without a flow. Just had to change 1 line. They both share these 3 funtions:

Code: Select all

OpenProcessToken_() 
LookupPrivilegeValue_() 
AdjustTokenPrivileges_()
Now after some messing with the code I noticed the small difference. The MS example uses

Code: Select all

LookupPrivilegeValue_("","SeShutdownPrivilege",tmpLuid) 
and the code from the link

Code: Select all

LookupPrivilegeValue_(0,"SeShutdownPrivilege",tkp\Privileges[0]\Luid)
The same varibale as used in 'AdjustTokenPrivileges_()'. So the MS example uses two different variables, wich caused the problem. Also you have to comment/erase the lines

Code: Select all

tkp\Privileges\LUID\LowPart = tmpLuid 
tkp\Privileges\LUID\HighPart = tmpLuid
they just cause unnessesary problems. Still there are some useless lines and parameteres. So if you clean it up it looks like this:

Code: Select all

Procedure ExitWindows(Flags.l) 
   Protected tkp.TOKEN_PRIVILEGES 
   Protected tkpNewButIgnored.TOKEN_PRIVILEGES
       
   OpenProcessToken_(GetCurrentProcess_(),#TOKEN_ADJUST_PRIVILEGES | #TOKEN_QUERY,@hdlTokenHandle) 

   ; Get the LUID for shutdown privilege. 
   LookupPrivilegeValue_(0,"SeShutdownPrivilege",tkp\Privileges\Luid) 

   tkp\PrivilegeCount = 1    ; One privilege to set 
   tkp\Privileges\Attributes = #SE_PRIVILEGE_ENABLED   
    
   ; Enable the shutdown privilege in the access token of this process. 
   AdjustTokenPrivileges_(hdlTokenHandle,0,tkp,0,0,0) 
   
   ExitWindowsEx_(Flags,0)
EndProcedure 

ExitWindows(#EWX_REBOOT)
However, doesn't matter wich code you use, they both work well.

Thanks for help Hi-Toro!
Post Reply