Announcement

Collapse
No announcement yet.

InitiateSystemShutdown Access Denied

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • InitiateSystemShutdown Access Denied

    I've taken this code right out of my app and made the debug program just so you can test it too.

    I'm getting access denied, not to be totally unexpected on a navy computer but when I took it home I got the same thing.

    This code camedirectly out of an older app I had that DID work!

    Anything I"mdoing ? DOes this work for you?

    I put the abortsystemshutdown in after a sleep statement so you won't have to restart to test....the dialog box has a 30 second timeout.


    Thanks,

    This should drop right in and compile for you with 8.04

    Code:
    #Compile Exe
    #Dim All
    #Include "Win32api.inc"
    #Include "Reason.inc"
    
    Declare Function CCSGetComputerName()As String
    Declare Function SetShutdownPrivilege() As Long
    Declare Function GetLastErrorDescription( ByVal ErrorCode As Long ) As String
    
    '============================================<WINMAIN>==================================================================
    Function WinMain (ByVal hInstance     As Long, _
                      ByVal hPrevInstance As Long, _
                      ByVal lpCmdLine     As Asciiz Ptr, _
                      ByVal iCmdShow      As Long) As Long
    
    Local Param1            As String
    Local Param2            As String
    Local lResult           As Long
    Local ErrType           As Long
    Local lpMachineName     As Asciiz * 16
    
    lpMachineName = CCSGetComputerName()
    Param1 = 30
    Param2 = "DEBUGGING CODE - Test Test Test"
    
    lResult = SetShutdownPrivilege() 'Grab privilege just in case
    'Errorchecking goes here
    
    lResult = InitiateSystemShutdownEx(ByVal VarPtr(lpMachineName),_
                                       ByVal StrPtr(Param2),_
                                       Val(Param1), _
                                       ByVal %FALSE, _
                                       ByVal %FALSE, _
                                       %SHTDN_REASON_MAJOR_OTHER Or %SHTDN_REASON_MINOR_MAINTENANCE)
    
    'lResult = InitiateSystemShutdown(lpMachineName, ByVal StrPtr(Param2), ByVal Val(Param1), ByVal %TRUE, ByVal %TRUE)
    
    
    If IsFalse lResult Then
        ErrType = GetLastError()
        MsgBox "Error: " & Format$(ErrType) & " - " & GetLastErrorDescription(ErrType) & $CrLf & "lResult (Non-Zero for success): " & Format$(lResult)
    End If
    
    'Use this for debug
    Sleep 2500
    AbortSystemShutdown lpMachineName
    End Function
    
    '==========================================================================================
    Function CCSGetComputerName()As String
    Local lResult As Long
    Local buff    As Asciiz * 17
    lResult = GetComputerName(buff, SizeOf(buff))
    Function = buff
    End Function
    '==========================================================================================
    Function SetShutdownPrivilege() As Long
    Local hdlProcessHandle    As Long
    Local hToken              As Long
    Local tmpLuid             As LUID
    Local tp                  As TOKEN_PRIVILEGES
    Local tpNewButIgnored     As TOKEN_PRIVILEGES
    Local BufferLength        As Long
    Local lBufferNeeded       As Long
    
    %TOKEN_ADJUST_PRIVILEGES = &H20
    %TOKEN_QUERY = &H8
    %SE_PRIVILEGE_ENABLED = &H2
    
    hdlProcessHandle = GetCurrentProcess()
    OpenProcessToken hdlProcessHandle, (%TOKEN_ADJUST_PRIVILEGES Or %TOKEN_QUERY), hToken
    ' Get the LUID for setSystemTime privilege.
    LookupPrivilegeValue "", "SeShutdownPrivilege",tmpLuid
    tp.PrivilegeCount = 1
    ' One privilege to set
    tp.Privileges(1).pLuid = tmpLuid
    tp.Privileges(1).Attributes=%SE_PRIVILEGE_ENABLED
    ' Enable the SetSystemTime privilege in the access token of this process.
    BufferLength = SizeOf(tpNewButIgnored)
    Function= AdjustTokenPrivileges( ByVal hToken,ByVal 0,tp,ByVal BufferLength ,tpNewButIgnored,lBufferNeeded)
    End Function
    '==========================================================================================
    Function GetLastErrorDescription( ByVal ErrorCode As Long ) As String
    Dim sRtrnCode   As Asciiz * 256
    Dim lRet        As Long
    If ErrorCode = %ERROR_SUCCESS Then Exit Function
    lRet = FormatMessage( %FORMAT_MESSAGE_FROM_SYSTEM, _
                        ByVal 0&, _
                        ErrorCode, _
                        ByVal 0&, _
                        sRtrnCode, _
                        SizeOf( sRtrnCode ), _
                        ByVal 0& )
    
    If lRet > 0 Then Function = Left$( sRtrnCode, lRet )
    End Function
    '==========================================================================================
    Scott Turchin
    MCSE, MCP+I
    http://www.tngbbs.com
    ----------------------
    True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

  • #2
    Addendum: - That code works great on Windows 2003 server.
    Scott Turchin
    MCSE, MCP+I
    http://www.tngbbs.com
    ----------------------
    True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

    Comment


    • #3
      Anyone?
      Scott Turchin
      MCSE, MCP+I
      http://www.tngbbs.com
      ----------------------
      True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

      Comment


      • #4
        Hi Scott,

        It throws a compile error for me (string operand expected) in 8.03 on:
        Code:
        Param1 = 30
        Changing it to:
        Code:
        Param1 = "30"
        fixes it, as does changing Param1 to a DWORD instead (as per MSDN function definition) and removing VAL() from the call.

        This is under Windows XP Pro SP2, with an administrative account.

        Regards,

        Pete.

        Comment


        • #5
          I get the same errors as Peter. 8.04 XP Pro SP3

          ====================================
          "The charity that is a trifle to us
          can be precious to others."
          Homer"
          ====================================
          It's a pretty day. I hope you enjoy it.

          Gösta

          JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
          LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

          Comment


          • #6
            You could try ExitWindowsEx with the EWX_POWEROFF and/or EWX_SHUTDOWN options.

            There's some remarks in the doc which I am sure are important, not that I understand it all that well...

            The ExitWindowsEx function returns as soon as it has initiated the shutdown. The shutdown or logoff then proceeds asynchronously. The function is designed to stop all processes in the caller's logon session. Therefore, if you are not the interactive user, the function can succeed without actually shutting down the computer. If you are not the interactive user, use the InitiateSystemShutdown or InitiateSystemShutdownEx function.
            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              It's in the acquiring of the privilege on XP, if you do not it will fail as I've read on some other forums.

              That said here's an updated version - strictly for use with this API, not the original app.

              It should return a privilege error - and I'm not up to speed on that code, I merelyc hanged it from my SeSetsystemTime priv..

              Code:
              #Compile Exe
              #Dim All
              #Include "Win32api.inc"
              #Include "Reason.inc"
              
              Declare Function SetShutdownPrivilege() As Long
              Declare Function CCSGetComputerName()As String
              Declare Function GetLastErrorDescription( ByVal ErrorCode As Long ) As String
              
              '============================================<WINMAIN>==================================================================
              Function WinMain (ByVal hInstance     As Long, _
                                ByVal hPrevInstance As Long, _
                                ByVal lpCmdLine     As Asciiz Ptr, _
                                ByVal iCmdShow      As Long) As Long
              
              Local lResult               As Long
              Local ErrType               As Long
              Local lpMachineName         As Asciiz * 16
              Local lpMsg                 As Asciiz * 255
              Local dwTimeOut             As Dword
              Local bForceAppsClosed      As Long
              Local bRebootAfterShutdown  As Long
              
              dwTimeOut               = 30
              bForceAppsClosed        = %TRUE
              bRebootAfterShutdown    = %TRUE
              lpMachineName           = CCSGetComputerName()
              lpMsg                   = "DEBUG: Attempting to acquire privileges and shut down the system for debugging purposes"
              
              
              lResult = SetShutdownPrivilege() 'The return IS the GetLastError
              ErrType = GetLastError()
              MsgBox "SetShutdownPrivilege: " & Format$(ErrType) & $CrLf & "Description: " & GetLastErrorDescription(ErrType)
              lResult = InitiateSystemShutdownEx(lpMachineName,_
                                                 lpMsg,_
                                                 dwTimeout, _
                                                 %FALSE, _
                                                 %TRUE, _
                                                 %SHTDN_REASON_MAJOR_OTHER Or %SHTDN_REASON_MINOR_MAINTENANCE)
              
              MsgBox "InitiateSystemShutdownEx: " & Format$(ErrType) & $CrLf & "Description: " & GetLastErrorDescription(ErrType)
              
              End Function
              
              '==========================================================================================
              Function SetShutdownPrivilege() As Long
              Local hdlProcessHandle    As Long
              Local hToken              As Long
              Local tmpLuid             As LUID
              Local tp                  As TOKEN_PRIVILEGES
              Local tpNewButIgnored     As TOKEN_PRIVILEGES
              Local BufferLength        As Long
              Local lBufferNeeded       As Long
              Local ErrType             As Long
              Local lResult             As Long
              
              '%TOKEN_ADJUST_PRIVILEGES = &H20
              '%TOKEN_QUERY = &H8
              '%SE_PRIVILEGE_ENABLED = &H2
              
              hdlProcessHandle = GetCurrentProcess()
              OpenProcessToken hdlProcessHandle, (%TOKEN_ADJUST_PRIVILEGES Or %TOKEN_QUERY), hToken
              ' Get the LUID for SeShutdownPrivilege privilege.
              LookupPrivilegeValue "", "SeShutdownPrivilege",tmpLuid
              tp.PrivilegeCount = 1
              ' One privilege to set
              tp.Privileges(1).pLuid = tmpLuid
              tp.Privileges(1).Attributes=%SE_PRIVILEGE_ENABLED
              ' Enable the Shutdown privilege in the access token of this process.
              BufferLength = SizeOf(tpNewButIgnored)
              lResult = AdjustTokenPrivileges( ByVal hToken,ByVal 0,tp,ByVal BufferLength ,tpNewButIgnored,lBufferNeeded)
              'If the function succeeds, the return value is nonzero.
              'To determine whether the function adjusted all of the specified privileges, call GetLastError,
              'which returns one of the following values when the function succeeds:
              Function = lResult
              End Function
              '==========================================================================================
              Function CCSGetComputerName()As String
              Local lResult As Long
              Local buff    As Asciiz * 17
              lResult = GetComputerName(buff, SizeOf(buff))
              Function = buff
              End Function
              '==========================================================================================
              Function GetLastErrorDescription(ByVal ErrorCode As Long) As String
              Dim sRtrnCode   As Asciiz * 256
              Dim lRet        As Long
              If ErrorCode = %ERROR_SUCCESS Then Exit Function
              lRet = FormatMessage( %FORMAT_MESSAGE_FROM_SYSTEM, _
                                  ByVal 0&, _
                                  ErrorCode, _
                                  ByVal 0&, _
                                  sRtrnCode, _
                                  SizeOf( sRtrnCode ), _
                                  ByVal 0& )
              If lRet > 0 Then Function = Left$(sRtrnCode, lRet -2 ) '-2 Removes the Chr$(13,10)
              End Function
              '==========================================================================================
              Scott Turchin
              MCSE, MCP+I
              http://www.tngbbs.com
              ----------------------
              True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

              Comment

              Working...
              X