OVH Cloud OVH Cloud

Shutdown sous Xp

1 réponse
Avatar
mg
Salut,

J'ai ecrire un petit programme en VB qui permet d'etteindre le PC via les
API windows.
Tout focntionne correctement sauf sous XP et W2k, la machine reste allumer
( Après fermeture de toutes les applications chargée).
Merci d'avance.

1 réponse

Avatar
Patrick Garceau
Manque de privilèges sous 2k ou xp pour fermer le système.

Je paste mon module de shutdown...

Patrick Garceau


Module ShutDown.bas

Option Explicit

Public Enum ShutdownAttr
EWX_LogOff = 0
EWX_SHUTDOWN = 1
EWX_REBOOT = 2
EWX_FORCE = 4
EWX_POWEROFF = 8
End Enum

'The ExitWindowsEx function either logs off, shuts down, or shuts
'down and restarts the system.
Public Declare Function ExitWindowsEx Lib "user32" (ByVal dwOptions As Long,
ByVal dwReserved As Long) As Long

'The GetLastError function returns the calling thread's last-error
'code value. The last-error code is maintained on a per-thread basis.
'Multiple threads do not overwrite each other's last-error code.
Public Declare Function GetLastError Lib "kernel32" () As Long

Public Const mlngWindows95 = 0
Public Const mlngWindowsNT = 1

Public glngWhichWindows32 As Long

'The GetVersion function returns the operating system in use.
Public Declare Function GetVersion Lib "kernel32" () As Long

Public Type LUID
UsedPart As Long
IgnoredForNowHigh32BitPart As Long
End Type

Public Type LUID_AND_ATTRIBUTES
TheLuid As LUID
Attributes As Long
End Type

Public Type TOKEN_PRIVILEGES
PrivilegeCount As Long
TheLuid As LUID
Attributes As Long
End Type

'The GetCurrentProcess function returns a pseudohandle for the
'current process.
Public Declare Function GetCurrentProcess Lib "kernel32" () As Long

'The OpenProcessToken function opens the access token associated with
'a process.
Public Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle
As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long

'The LookupPrivilegeValue function retrieves the locally unique
'identifier (LUID) used on a specified system to locally represent
'the specified privilege name.
Public Declare Function LookupPrivilegeValue Lib "advapi32" Alias
"LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As
String, lpLuid As LUID) As Long

'The AdjustTokenPrivileges function enables or disables privileges
'in the specified access token. Enabling or disabling privileges
'in an access token requires TOKEN_ADJUST_PRIVILEGES access.
Public 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

Public Declare Sub SetLastError Lib "kernel32" (ByVal dwErrCode As Long)

Public Sub AdjustToken()

'********************************************************************
'* This procedure sets the proper privileges to allow a log off or a
'* shut down to occur under Windows NT.
'********************************************************************

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

Dim hdlProcessHandle As Long
Dim hdlTokenHandle As Long
Dim tmpLuid As LUID
Dim tkp As TOKEN_PRIVILEGES
Dim tkpNewButIgnored As TOKEN_PRIVILEGES
Dim lBufferNeeded As Long

'Set the error code of the last thread to zero using the
'SetLast Error function. Do this so that the GetLastError
'function does not return a value other than zero for no
'apparent reason.
SetLastError 0

'Use the GetCurrentProcess function to set the hdlProcessHandle
'variable.
hdlProcessHandle = GetCurrentProcess()

If GetLastError <> 0 Then
MsgBox "GetCurrentProcess error==" & GetLastError
End If

OpenProcessToken hdlProcessHandle, _
(TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hdlTokenHandle

If GetLastError <> 0 Then
MsgBox "OpenProcessToken error==" & GetLastError
End If

'Get the LUID for shutdown privilege
LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid

If GetLastError <> 0 Then
MsgBox "LookupPrivilegeValue error==" & GetLastError
End If

tkp.PrivilegeCount = 1 ' One privilege to set
tkp.TheLuid = tmpLuid
tkp.Attributes = SE_PRIVILEGE_ENABLED

'Enable the shutdown privilege in the access token of this process
AdjustTokenPrivileges hdlTokenHandle, _
False, _
tkp, _
Len(tkpNewButIgnored), _
tkpNewButIgnored, _
lBufferNeeded

If GetLastError <> 0 Then
MsgBox "AdjustTokenPrivileges error==" & GetLastError
End If

End Sub

Sub ProcessShutdown(ByVal Shutdowntype As ShutdownAttr)
If glngWhichWindows32 = mlngWindowsNT Then
AdjustToken
End If
ExitWindowsEx (Shutdowntype), &HFFFF
End Sub

Sub Main()
'********************************************************************
'* When the project starts, check the operating system used by
'* calling the GetVersion function.
'********************************************************************
Dim lngVersion As Long

lngVersion = GetVersion()

If ((lngVersion And &H80000000) = 0) Then
glngWhichWindows32 = mlngWindowsNT
Else
glngWhichWindows32 = mlngWindows95
End If

'Restart
'ProcessShutdown EWX_SHUTDOWN + EWX_FORCE + EWX_POWEROFF
'Shutdown
ProcessShutdown EWX_SHUTDOWN + EWX_FORCE

End Sub






"mg" wrote in message
news:425bf232$0$11724$
Salut,

J'ai ecrire un petit programme en VB qui permet d'etteindre le PC via les
API windows.
Tout focntionne correctement sauf sous XP et W2k, la machine reste allumer
( Après fermeture de toutes les applications chargée).
Merci d'avance.