Punto informatico Network
Login Esegui login | Non sei registrato? Iscriviti ora (è gratuito!)
Username: Password:
  • Annuncio Pubblicitario

[VB.net e VB6] Effettuare un riavvio del sistema

Il forum per tutti i developer. Leggere attentamente il regolamento di sezione prima di postare.

[VB.net e VB6] Effettuare un riavvio del sistema

Messaggioda white_knight86 » ven apr 20, 2007 11:11 am

Ciao [:)] dovrei effettuare un riavvio del sistema da codice, ho pensato di utilizzare l'eseguibile shutdown da system32... [uhm] , avete qualche dritta da darmi? [^]
Scheda Madre: ASUS P5QL-E Processore: E7300 a 2.66 GHZ Memoria Ram: 4 Gb da 800 Mhz
Scheda video: Sapphire HD 5770 con 1Gb di Ram
Avatar utente
white_knight86
Senior Member
Senior Member
 
Messaggi: 268
Iscritto il: gio mar 11, 2004 6:24 pm

Messaggioda Silver Black » ven apr 20, 2007 12:07 pm

Con questo codice VB4-5-6 esegui il reboot o lo shut-down del sistema:

Codice: Seleziona tutto
Private Type LUID
   LowPart As Long
   HighPart As Long
End Type

Private Type TOKEN_PRIVILEGES
    PrivilegeCount As Long
    LuidUDT As LUID
   Attributes As Long
End Type

Const TOKEN_ADJUST_PRIVILEGES = &H20
Const TOKEN_QUERY = &H8
Const SE_PRIVILEGE_ENABLED = &H2

Const EWX_SHUTDOWN = 1
Const EWX_REBOOT = 2
Const EWX_FORCE = 4

Private Declare Function GetVersion Lib "kernel32" () As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle _
    As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare Function LookupPrivilegeValue Lib "advapi32" Alias _
    "LookupPrivilegeValueA" (ByVal lpSystemName As String, _
    ByVal lpName As String, lpLuid As LUID) As Long
Private Declare Function AdjustTokenPrivileges Lib "advapi32" (ByVal _
    TokenHandle As Long, ByVal DisableAllPrivileges As Long, _
    NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, _
    PreviousState As Any, ReturnLength As Any) As Long
Private Declare Function ExitWindowsEx Lib "user32" (ByVal dwOptions As Long, _
    ByVal dwReserved As Long) As Long

' Shut down windows, and optional reboot it
' if the 2nd argument is True, no WM_QUERYENDSESSION and WM_ENDSESSION
' messages are sent to active applications

Sub ShutDownWindows(ByVal Reboot As Boolean, Optional ByVal Force As Boolean)
    Dim hToken As Long
    Dim tp As TOKEN_PRIVILEGES
    Dim flags As Long
   
    ' Windows NT/2000 require a special treatment
    ' to ensure that the calling process has the
    ' privileges to shut down the system
   
    ' under NT the high-order bit (that is, the sign bit)
    ' of the value retured by GetVersion is cleared
    If GetVersion() >= 0 Then
        ' Open this process for adjusting its privileges
        OpenProcessToken GetCurrentProcess(), (TOKEN_ADJUST_PRIVILEGES Or _
            TOKEN_QUERY), hToken
       
        ' Get the LUID for shutdown privilege.
        ' retrieves the locally unique identifier (LUID) used
        ' to locally represent the specified privilege name
        ' (first argument = "" means the local system)
        LookupPrivilegeValue "", "SeShutdownPrivilege", tp.LuidUDT
       
        ' complete the TOKEN_PRIVILEGES structure with the # of
        ' privileges and the desired attribute
        tp.PrivilegeCount = 1
        tp.Attributes = SE_PRIVILEGE_ENABLED
       
        ' enables or disables privileges in the specified access token
        ' last 3 arguments are zero because we aren't interested
        ' in previous privilege attributes.
        AdjustTokenPrivileges hToken, False, tp, 0, ByVal 0&, ByVal 0&
    End If
   
    ' prepare shutdown flags
    flags = EWX_SHUTDOWN
    If Reboot Then flags = flags Or EWX_REBOOT
    If Force Then flags = flags Or EWX_FORCE
   
    ' finally, you can shut down Windows
    ExitWindowsEx flags, &HFFFF
   
End Sub



Per VB.NET guarda qui.
Sergio Pappalardo aka Silver Black
https://www.silvercybertech.com
Avatar utente
Silver Black
Silver Member
Silver Member
 
Messaggi: 1148
Iscritto il: gio gen 06, 2005 8:50 pm
Località: Italia

Messaggioda white_knight86 » lun apr 23, 2007 7:00 pm

Ok grazie [^] , me lo studio così lo imparo per bene [;)]
Scheda Madre: ASUS P5QL-E Processore: E7300 a 2.66 GHZ Memoria Ram: 4 Gb da 800 Mhz
Scheda video: Sapphire HD 5770 con 1Gb di Ram
Avatar utente
white_knight86
Senior Member
Senior Member
 
Messaggi: 268
Iscritto il: gio mar 11, 2004 6:24 pm

Messaggioda Zane » ven mag 25, 2007 1:37 pm

Sotto Win 2000 e successivi puoi anche usare qualcosa del tipo

Codice: Seleziona tutto
shell("shutdown -r")


(verifica solo un attimo la sintassi...)
Avatar utente
Zane
MLI Hero
MLI Hero
 
Messaggi: 7935
Iscritto il: lun ago 05, 2002 9:36 am
Località: Ferrara


Torna a Programmazione

Chi c’è in linea

Visitano il forum: Nessuno e 2 ospiti

cron
Powered by phpBB © 2002, 2005, 2007, 2008 phpBB Group
Traduzione Italiana phpBB.it

megalab.it: testata telematica quotidiana registrata al Tribunale di Cosenza n. 22/09 del 13.08.2009, editore Master New Media S.r.l.; © Copyright 2008 Master New Media S.r.l. a socio unico - P.I. 02947530784. GRUPPO EDIZIONI MASTER Spa Tutti i diritti sono riservati. Per la pubblicità: Master Advertising