ok ich poste mal meine shutdown klasse hier für alle des wurde schon öfter ohne richtige lösung gefragt..
Imports System.Runtime.InteropServices
Public Class Shutdown
#Region " Privilegs "
' Constants
Private Const SE_PRIVILEGE_ENABLED As Integer = &H2
Private Const TOKEN_QUERY As Integer = &H8
Private Const TOKEN_ADJUST_PRIVILEGES As Integer = &H20
Private Const SE_SHUTDOWN_NAME As String = "SeShutdownPrivilege"
<StructLayout(LayoutKind.Sequential, Pack:=1)> _
Private Structure Luid ' locally unique identifier
Public Count As Integer
Public Luid As Long
Public Attr As Integer
End Structure
<DllImport("kernel32.dll", ExactSpelling:=True)> _
Private Shared Function GetCurrentProcess() As IntPtr
End Function ' get process handle ...
<DllImport("advapi32.dll", SetLastError:=True)> _
Private Shared Function OpenProcessToken(ByVal h As IntPtr, ByVal acc As _
Integer, ByRef phtok As IntPtr) As Boolean
End Function ' open process token
<DllImport("advapi32.dll", SetLastError:=True)> _
Private Shared Function LookupPrivilegeValue(ByVal host As String, ByVal _
name As String, ByRef pluid As Long) As Boolean
End Function ' luid privileg verpassen
<DllImport("advapi32.dll", ExactSpelling:=True, SetLastError:=True)> _
Private Shared Function AdjustTokenPrivileges(ByVal htok As IntPtr, ByVal _
disall As Boolean, ByRef newst As Luid, ByVal len As Integer, ByVal prev As _
IntPtr, ByVal relen As IntPtr) As Boolean
End Function ' token anpassen
' assigns shutdownprivileges to the running process
Public Shared Sub getShutdownPrivileges()
Dim tp As Luid
Dim hproc As IntPtr = GetCurrentProcess()
Dim htok As IntPtr = IntPtr.Zero
'Get a token for this process.
OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, htok)
tp.Count = 1
tp.Luid = 0
tp.Attr = SE_PRIVILEGE_ENABLED
' get a valid luid for shutdown
LookupPrivilegeValue(Nothing, SE_SHUTDOWN_NAME, tp.Luid)
'adjust the shutdown luid to the token
AdjustTokenPrivileges(htok, False, tp, 0, IntPtr.Zero, IntPtr.Zero)
End Sub
#End Region
#Region " ExitWindowsEx "
' ExitWindows Constants
Public Const EWX_LOGOFF As Integer = &H0 ' logoff
Public Const EWX_SHUTDOWN As Integer = &H1 ' shutdown
Public Const EWX_REBOOT As Integer = &H2 ' reboot
Public Const EWX_FORCE As Integer = &H4 ' force apps to be closed
Public Const EWX_POWEROFF As Integer = &H8 ' turn off
Public Const EWX_FORCEIFHUNG As Integer = &H10 ' force if hung
' Exit Windows
<DllImport("user32.dll", ExactSpelling:=True, SetLastError:=True)> _
Public Shared Function ExitWindowsEx(ByVal flg As Integer, ByVal rea As _
Integer) As Boolean
End Function
#End Region
#Region " ShutdownFunctions "
<DllImport("advapi32.dll", SetLastError:=True)> _
Public Shared Function AbortSystemShutdown(ByVal lpMachineName As String) _
As Boolean
End Function
<DllImport("advapi32.dll", SetLastError:=True)> _
Public Shared Function InitiateSystemShutdown(ByVal lpMachineName As _
String, _
ByVal lpMessage As String, _
ByVal dwTimeout As Integer, _
ByVal bForceAppsClosed As Boolean, _
ByVal bRebootAfterShutdown As Boolean) As Boolean
End Function
<DllImport("advapi32.dll", SetLastError:=True)> _
Public Shared Function InitiateSystemShutdownEx(ByVal lpMachineName As _
String, _
ByVal lpMessage As String, _
ByVal dwTimeout As Integer, _
ByVal bForceAppsClosed As Boolean, _
ByVal bRebootAfterShutdown As Boolean, _
ByVal dwReason As Integer) As Boolean ' u can create your own reasons !
' see msdn
End Function
#End Region
End Class nun hast du die auswahl aus mehreren shutdown methoden
als erstes musst du die Shutdown.getShutdownPrivileges() methode aufrufen.
danach hat dein process shutdown rechte
dann kannst du folgendes machen :
Const NULL As String = ""
Shutdown.getShutdownPrivileges()
Shutdown.InitiateSystemShutdown(NULL, "Shutdown", 0, True, True) wenn du den parameter bForceAppsClosed auf true setzt kommen keine nachfragen
das etwas gespeichert werden soll ...
bRebootAfterShutdown sagt aus ob gerebooted werden soll ...
njoy |