Dim sysevt As Microsoft.Win32.SystemEvents
Private Sub SessionEndingHandler(ByVal sender As Object, ByVal e As SessionEndingEventArgs)
'cancel the shutdown event
e.Cancel = True
if e.Reason = Logoff then
Logofffunction
'invoke the shutdown event
Shutdown
elseif e.reason = shutdown then
shutdownfunction
'invoke the shutdown event
Shutdown
endif
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler sysevt.SessionEnding, AddressOf SessionEndingHandler
End Sub
Public Sub Shutdown(Optional ByVal pReboot As Boolean = False)
Const SHUTDOWN_FLAG As String = "1"
Const REBOOT_FLAG As String = "2"
Dim localpcname As String = System.Net.Dns.GetHostName
Dim ms As System.Management.ManagementScope = New System.Management.ManagementScope("\\" & localpcname & "\root\cimv2")
ms.Options.EnablePrivileges = True
'Query remote computer across the connection
Dim oq As System.Management.ObjectQuery = New System.Management.ObjectQuery("SELECT * FROM Win32_OperatingSystem")
Dim query1 As System.Management.ManagementObjectSearcher = New System.Management.ManagementObjectSearcher(ms, oq)
Dim queryCollection1 As System.Management.ManagementObjectCollection = query1.Get()
Dim mo As System.Management.ManagementObject
gFrmClient.ShutdownByServer = True
For Each mo In queryCollection1
Dim inputparam(1) As String
If pReboot Then
inputparam(0) = REBOOT_FLAG
Else
inputparam(0) = SHUTDOWN_FLAG
End If
inputparam(1) = "0"
mo.InvokeMethod("Win32Shutdown", inputparam)
Next
End Sub