Emergency Shutdown - Via ntdll.dll

Status
Niet open voor verdere reacties.

Maximvdw

Gebruiker
Lid geworden
7 feb 2009
Berichten
921
Hallo,

Ik ben onlangst op het idee gekomen om een fast shutdown te maken zoals "Shutdown Booster v1.2"
Nu ik vind hier bijna geen voorbeelden van en ik zit vast bij de privilege van SE_SHUTDOWN_PRIVILEGE . Ik zit hier al 2 maanden te proberen maar het lukt echt niet..
Hier mijn code::(

PHP:
   Public Declare Sub NtShutdownSystem Lib "ntdll" (ByVal ShutdownAction&)
    Public Declare Function RtlAdjustPrivilege& Lib "ntdll" (ByVal Privilege&, ByVal NewValue As Boolean, ByVal NewThread As Boolean, ByVal OldValue As Boolean)
    ' // Native API to Shutdown the System
    ' // The Shutdown Privilege
    Public Const SE_SHUTDOWN_PRIVILEGE& = 19
    ' // The Shutdown Actions
    Public Const SHUTDOWN& = 0
    Public Const RESTART& = 1
    Public Const POWEROFF& = 2



    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TurboShutdown()
    End Sub
    Sub TurboShutdown()
        RtlAdjustPrivilege(SE_SHUTDOWN_PRIVILEGE, 1, 0, 0) 'HIER KRIJG IK ERROR
        NtShutdownSystem(SHUTDOWN)

    End Sub
 
Ms dos

zou het iets uitmaken als ik deze code in een Module gebruik inp.v. in een Windows form?
 
Oh,.. 2 maanden zoeken terwijl het antwoord onder mijn neus ligt
PS: Ik stopte er nu een fout in. Zodat nieman Copy paste zou doen
Als je echt werkelijk de code wil mail me dan

VOLLEDIG: ©Maxim Van de Wynckel 2011
PHP:
Imports System.Runtime.InteropServices
Public Class Form1
    Public Declare Function RtlAdjustPrivilege& Lib "ntdll" (ByVal Privilege&, ByVal NewValue&, ByVal NewThread&, ByVal OldValue&)
    Public Declare Function NtShutdownSystem& Lib "ntdll" (ByVal ShutdownAction&)
    Public Const SE_SHUTDOWN_PRIVILEGE& = 19
    ' // The Shutdown Actions
    Public Const SHUTDOWN& = 0
    Public Const RESTART& = 1
    Public Const POWEROFF& = 2
    Public Class Privileges
        'This routine enables the Shutdown privilege for the current process, 
        'which is necessary if you want to call ExitWindowsEx.
        Private Const ANYSIZE_ARRAY As Integer = 1
        Private Const TOKEN_QUERY As Integer = &H8
        Private Const TOKEN_ADJUST_PRIVILEGES As Integer = &H20
        Private Const SE_PRIVILEGE_ENABLED = &H2
        Public Const SE_SHUTDOWN_NAME = "SeShutdownPrivilege"
        Public Const SE_RESTORE_NAME = "SeRestorePrivilege"
        <StructLayout(LayoutKind.Sequential)> _
        Private Structure LUID
            Public LowPart As UInt32
            Public HighPart As UInt32
        End Structure
        <StructLayout(LayoutKind.Sequential)> _
        Private Structure LUID_AND_ATTRIBUTES
            Public Luid As LUID
            Public Attributes As UInt32
        End Structure
        <StructLayout(LayoutKind.Sequential)> _
        Private Structure TOKEN_PRIVILEGES
            Public PrivilegeCount As UInt32
            <MarshalAs(UnmanagedType.ByValArray, SizeConst:=ANYSIZE_ARRAY)> _
            Public Privileges() As LUID_AND_ATTRIBUTES
        End Structure
        <DllImport("advapi32.dll", SetLastError:=True)> _
        Private Shared Function LookupPrivilegeValue( _
         ByVal lpSystemName As String, _
         ByVal lpName As String, _
         ByRef lpLuid As LUID _
          ) As Boolean
        End Function
        <DllImport("advapi32.dll", SetLastError:=True)> _
        Private Shared Function OpenProcessToken( _
         ByVal ProcessHandle As IntPtr, _
         ByVal DesiredAccess As Integer, _
         ByRef TokenHandle As IntPtr _
          ) As Boolean
        End Function
        <DllImport("kernel32.dll", SetLastError:=True)> _
        Private Shared Function CloseHandle(ByVal hHandle As IntPtr) As Boolean
        End Function
        <DllImport("advapi32.dll", SetLastError:=True)> _
        Private Shared Function AdjustTokenPrivileges( _
           ByVal TokenHandle As IntPtr, _
           ByVal DisableAllPrivileges As Boolean, _
           ByRef NewState As TOKEN_PRIVILEGES, _
           ByVal BufferLength As Integer, _
           ByRef PreviousState As TOKEN_PRIVILEGES, _
           ByRef ReturnLength As IntPtr _
         ) As Boolean
        End Function
        Public Shared Sub AcquirePrivilege(ByVal privilege As String)
            Dim lastWin32Error As Integer = 0
            'Get the LUID that corresponds to the Shutdown privilege, if it exists.
            Dim luid As LUID
            If Not LookupPrivilegeValue(Nothing, privilege, luid) Then
                lastWin32Error = Marshal.GetLastWin32Error()
                Throw New System.ComponentModel.Win32Exception(lastWin32Error, _
                 "LookupPrivilegeValue failed with error " & lastWin32Error.ToString & ".")
            End If
            'Get the current process's token.
            Dim hProc As IntPtr = Process.GetCurrentProcess().Handle
            Dim hToken As IntPtr
            If Not OpenProcessToken(hProc, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hToken) Then
                lastWin32Error = Marshal.GetLastWin32Error()
                Throw New System.ComponentModel.Win32Exception(lastWin32Error, _
                 "OpenProcessToken failed with error " & lastWin32Error.ToString & ".")
            End If
            Try
                'Set up a LUID_AND_ATTRIBUTES structure containing the Shutdown privilege, marked as enabled.
                Dim luaAttr As New LUID_AND_ATTRIBUTES
                luaAttr.Luid = luid
                luaAttr.Attributes = SE_PRIVILEGE_ENABLED
                'Set up a TOKEN_PRIVILEGES structure containing only the shutdown privilege.
                Dim newState As New TOKEN_PRIVILEGES
                newState.PrivilegeCount = 1
                newState.Privileges = New LUID_AND_ATTRIBUTES() {luaAttr}
                'Set up a TOKEN_PRIVILEGES structure for the returned (modified) privileges.
                Dim prevState As TOKEN_PRIVILEGES = New TOKEN_PRIVILEGES
                ReDim prevState.Privileges(CInt(newState.PrivilegeCount))
                'Apply the TOKEN_PRIVILEGES structure to the current process's token.
                Dim returnLength As IntPtr
                If Not AdjustTokenPrivileges(hToken, False, newState, Marshal.SizeOf(prevState), prevState, returnLength) Then
                    lastWin32Error = Marshal.GetLastWin32Error()
                    Throw New System.ComponentModel.Win32Exception(lastWin32Error, _
                     "AdjustTokenPrivileges failed with error " & lastWin32Error.ToString & ".")
                End If
            Finally
                CloseHandle(hToken)
            End Try
       End Sub
    End Class
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
   End Sub
End Class

Hij wist nog niet van WAAR in ntdll hij privelges gaf...
 
Laatst bewerkt:
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan