Newsgroup: it.comp.lang.visual-basic
Titolo: How do you shutdown NT

I would like to be able to force a shutdown and reboot NT from VB.  Right now
I am using the shutdown.exe from the NT resource kit.  I would prefer to do
it within my own code and not have to use a shell program.  If there is a way
can someone send me a sample I can play with since I am new with VB. Thanks
in advance!
Please send an e-mail to
tcl...@mediaone.net
-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp
   Create Your Own Free Member Forum

in advance!
Hi,
with this code you can reboot ,shutdown &/| logoff your
NT4WS , if you want to use with 95 just add a function to test
the OS and in W95 don't call the AdjustToken
(Just create a form, and add 3 button called logoff / reboot / shutdown)
Bye
Roberto Corti
Amministratore di rete
rco...@NOSPAMMONItinet.ch
'=========================DECLARE.BAS===================================
Option Explicit
Public Const EWX_FORCE = 4
Public Const EWX_LOGOFF = 0
Public Const EWX_REBOOT = 2
Public Const EWX_SHUTDOWN = 1
Public Const TOKEN_ADJUST_PRIVILEGES = &H20
Public Const TOKEN_QUERY = &H8
Public Const SE_PRIVILEGE_ENABLED = &H2
Public dummy As Long
Public Type LUID
    UsedPart As Long
    IgnoredForNowHigh32BitPart As Long
End Type
Public Type TOKEN_PRIVILEGES
    PrivilegeCount As Long
    TheLuid As LUID
    Attributes As Long
End Type
Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal
dwReserved As Long) As Long
Declare Function LookupPrivilegeValue Lib "advapi32" Alias
"LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As
String, lpLuid As LUID) As Long
Declare Function GetCurrentProcess Lib "kernel32" () As Long
Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As
Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Declare Function AdjustTokenPrivileges Lib "advapi32" (ByVal TokenHandle As
Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES,
ByVal BufferLength As Long, PreviousState As TOKEN_PRIVILEGES, ReturnLength
As Long) As Long
'====================RIBUTTA================================================
==
Option Explicit
'
' Codice di esempio per il reboot, il logoff e il riavvio di un sistema
Windows NT
'
Private Sub Logoff_Click()
    AdjustToken
    dummy = ExitWindowsEx(EWX_LOGOFF Or EWX_FORCE, 0)
End Sub
Private Sub Reboot_Click()
    AdjustToken
    dummy = ExitWindowsEx(EWX_REBOOT Or EWX_FORCE, 0)
End Sub
Private Sub Shutdown_Click()
    AdjustToken
    dummy = ExitWindowsEx(EWX_SHUTDOWN Or EWX_FORCE, 0)
End Sub
'
'  MSDN
'
Private Sub AdjustToken()
    Dim hdlProcessHandle As Long, hdlTokenHandle As Long, lBufferNeeded As
Long
    Dim tmpLuid As LUID
    Dim tkp As TOKEN_PRIVILEGES, tkpNewButIgnored As TOKEN_PRIVILEGES
    hdlProcessHandle = GetCurrentProcess()
    OpenProcessToken hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or
TOKEN_QUERY), hdlTokenHandle
    LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
    tkp.PrivilegeCount = 1
    tkp.TheLuid = tmpLuid
    tkp.Attributes = SE_PRIVILEGE_ENABLED
    AdjustTokenPrivileges hdlTokenHandle, False, tkp, Len(tkpNewButIgnored),
tkpNewButIgnored, lBufferNeeded
End Sub