Results 1 to 6 of 6

Thread: Why it doe'nt reboot?

  1. #1

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606
    I want my program to reboot my computer and my code:

    Code:
    Private Sub Command1_Click()
    Dim retval As Long  ' return value
    retval = ExitWindowsEx(EWX_FORCE Or EWX_REBOOT, 0)
    End Sub
    my module:

    Code:
    Public Declare Function ExitWindowsEx Lib "user32.dll" 
    (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
    i try it with a look up
    and my return value(retval) = 1, so it's suppose to reboot but it's not, WHY????????

  2. #2
    Lively Member
    Join Date
    Aug 2000
    Location
    quebec
    Posts
    81

    Cool

    What OS are you running?

    If NT or 2000 you may have to use the
    Code:
    Public Declare Function AdjustTokenPrivileges Lib "advapi32.dll" Alias "AdjustTokenPrivileges" _
    (ByVal TokenHandle As Long, _
    ByVal DisableAllPrivileges As Long, _
    NewState As TOKEN_PRIVILEGES, _
    ByVal BufferLength As Long, _
    PreviousState As TOKEN_PRIVILEGES, _
    ReturnLength As Long) As Long
    function to enable the necessary privileges so that your app can do a shutdown.

    An example in C from MSDN:
    Code:
    HANDLE hToken; 
    TOKEN_PRIVILEGES tkp; 
     
    // Get a token for this process. 
     
    if (!OpenProcessToken(GetCurrentProcess(), 
            TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) 
        error("OpenProcessToken"); 
     
    // Get the LUID for the shutdown privilege. 
     
    LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, 
            &tkp.Privileges[0].Luid); 
     
    tkp.PrivilegeCount = 1;  // one privilege to set    
    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; 
     
    // Get the 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) 
        error("AdjustTokenPrivileges"); 
     
    // Shut down the system and force all applications to close. 
     
    if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0)) 
        error("ExitWindowsEx");
    Check here also.
    Your process must have the SE_SHUTDOWN_NAME privilege for this on NT/2000.

    hope it helps 'cause otherwise I can't tell you why the function wouldn't execute properly while returning 1.


    C/C++,Delphi,VB6,Java,PB (blech!),ASP,JSP,SQL...bla bla bla and bla
    I love deadlines. I like the whooshing sound they make as they fly by.
    —Douglas Adams

  3. #3

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606

    Arrow yeah

    I'm with NT:

    Do you have an example in VB

  4. #4
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    This should work on NT 4 and 5.
    Code:
    Option Explicit
    
    Private Type LUID
        UsedPart As Long
        IgnoredForNowHigh32BitPart As Long
    End Type
    Private Type TOKEN_PRIVILEGES
        PrivilegeCount As Long
        TheLuid As LUID
        Attributes As Long
    End Type
    
    Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
    Private Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
    Private Declare Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
    Private Declare Function AdjustTokenPrivileges Lib "advapi32" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As Long, ReturnLength As Long) As Long
    Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
    
    Private Const TOKEN_ADJUST_PRIVILEGES = &H20
    Private Const TOKEN_QUERY = &H8
    Private Const SE_PRIVILEGE_ENABLED = &H2
    Private Const EWX_FORCE = 4
    Private Const EWX_REBOOT = 2
    Private Const SE_SHUTDOWN_NAME = "SeShutdownPrivilege"
    
    Private Sub ShutDown()
    Dim retVal As Long
    Dim hToken As Long
    Dim tmpLuid As LUID
    Dim tkp As TOKEN_PRIVILEGES
        
        OpenProcessToken GetCurrentProcess, (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hToken
        ' Get the LUID for shutdown privilege
        retVal = LookupPrivilegeValue(vbNullString, SE_SHUTDOWN_NAME, tmpLuid)
        tkp.PrivilegeCount = 1    ' One privilege to set
        tkp.TheLuid = tmpLuid
        tkp.Attributes = SE_PRIVILEGE_ENABLED
        ' Enable the shutdown privilege in the access token of this process
        retVal = AdjustTokenPrivileges(hToken, False, tkp, 0, ByVal 0, 0)
        
        retVal = ExitWindowsEx(EWX_FORCE Or EWX_REBOOT, 0)
    End Sub
    
    Private Sub Command1_Click()
        Call ShutDown
    End Sub
    P.S. Of course it is only possible to enable the shutdown privilege if the user has that privilege. You can't use this function to reboot a pc, if the user is not allowed to reboot his pc.

    [Edited by Frans C on 10-11-2000 at 02:59 PM]

  5. #5

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606

    nope

    Still does'nt work, i dunno why?

    And i have the write to reboot/shutdown my computer.

    It work if i only force it,but i want to reboot it!!!!

  6. #6
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    I also had bad experiences with the ExitWindowsEx function. It didn't work properly on all PC's.

    I prefer the RemoteSystemShutdown function. This also allows to reboot another NT workstation on the network (if you have admin rights).

    I'll include the full code I used in an app I previously wrote. You need two commandbuttons (cmdShutdown and cmdAbort), three textboxes (txtCompName, txtTimeOut and txtMessage) and two checkboxes (chkReboot and chkForce).

    Code:
    Option Explicit
    Private Type LUID
        UsedPart As Long
        IgnoredForNowHigh32BitPart As Long
    End Type
    Private Type TOKEN_PRIVILEGES
        PrivilegeCount As Long
        TheLuid As LUID
        Attributes As Long
    End Type
    Private Declare Function InitiateSystemShutdown Lib "advapi32.dll" Alias "InitiateSystemShutdownA" (ByVal lpMachineName As String, ByVal lpMessage As String, ByVal dwTimeout As Long, ByVal bForceAppsClosed As Boolean, ByVal bRebootAfterShutdown As Boolean) As Boolean
    'Private Declare Function SetCurrentPrivilege Lib "advapi32.dll" Alias "SetCurrentPrivilegeA" (ByVal TargetComputer As String, ByVal Privilege As String, ByVal bEnablePrivilege As Boolean) As Boolean
    Private Declare Function AbortSystemShutdown Lib "advapi32.dll" Alias "AbortSystemShutdownA" (ByVal lpMachineName As String) As Boolean
    
    Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
    Private Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
    Private Declare Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
    Private Declare Function AdjustTokenPrivileges Lib "advapi32" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long
    Private Const TOKEN_ADJUST_PRIVILEGES = &H20
    Private Const TOKEN_QUERY = &H8
    Private Const SE_PRIVILEGE_ENABLED = &H2
    
    'To shut down the local computer, the calling process must have the SE_SHUTDOWN_NAME privilege. To shut down a remote computer, the calling process must have the SE_REMOTE_SHUTDOWN_NAME privilege on the remote computer. By default, users can enable the SE_SHUTDOWN_NAME privilege on the computer they are logged onto, and administrators can enable the SE_REMOTE_SHUTDOWN_NAME privilege on remote computers.
    
    Private Sub ShutDown(ByVal Abort As Boolean)
    Dim lRet As Boolean
    Dim CompName As String
    Dim Message As String
    Dim TimeOut As Long
    Dim Force As Boolean
    Dim Reboot As Boolean
    Dim Privilege As String
    Dim hdlProcessHandle As Long
    Dim hdlTokenHandle As Long
    Dim tmpLuid As LUID
    Dim tkp As TOKEN_PRIVILEGES
    Dim tkpNewButIgnored As TOKEN_PRIVILEGES
    Dim lBufferNeeded As Long
             
    
        Force = chkForce.Value = vbChecked
        Reboot = chkReboot.Value = vbChecked
        Message = txtMessage.Text
        CompName = txtCompName.Text
        If Len(Trim(CompName)) = 0 Then
            ' no computername specified
            ' so shutdown the local PC
            CompName = vbNullString
            Privilege = "SeShutdownPrivilege"
        Else
            Privilege = "SeRemoteShutdownPrivilege"
        End If
        If IsNumeric(txtTimeOut.Text) Then
            TimeOut = Val(txtTimeOut.Text)
        Else
            TimeOut = 10
        End If
        
        hdlProcessHandle = GetCurrentProcess()
        OpenProcessToken hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hdlTokenHandle
        ' Get the LUID for shutdown privilege
        LookupPrivilegeValue "", Privilege, tmpLuid
        tkp.PrivilegeCount = 1    ' One privilege to set
        tkp.TheLuid = tmpLuid
        tkp.Attributes = SE_PRIVILEGE_ENABLED
        ' Enable the shutdown privilege in the access token of this process
        AdjustTokenPrivileges hdlTokenHandle, False, tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded
        'lRet = SetCurrentPrivilege(CompName, "SeShutdownPrivilege", True)
        If Abort Then
            lRet = AbortSystemShutdown(CompName)
        Else
            lRet = InitiateSystemShutdown(CompName, Message, TimeOut, Force, Reboot)
        End If
    
    End Sub
    
    Private Sub cmdAbort_Click()
        Call ShutDown(True)
    End Sub
    
    Private Sub cmdShutdown_Click()
        Call ShutDown(False)
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width