Cant Get Machine to Shutdown.


M

Mr Newbie

Whatever I do I cant programatically make my machine shutdown. I am using
the following class and have tried both thte ExitWindowsEx and
InititateSystemShutdownEx shared functions Neither seem to work. I have also
made sure that the assembly had full trust.

Can anyone suggest what may be wrong ?????

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
 
Ad

Advertisements

M

Mr Newbie

Yours works like a dream. However at the end of the email you say

And yet, you do use this function to exit and I am using Windows XP ??

No matter, it works well anyways, so thanks very much.

Regards Mr N . . .
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top