From: "Didier Lefebvre" References: <3851237F.B7AED105@free.fr> Subject: Re: Relancer le systeme avec Excel Date: Mon, 20 Dec 1999 21:12:25 +0100 Newsgroups: microsoft.public.fr.excel copie le code suivant dans un module et appelle la fonction ShutdownWindows (fonctionne pour moi aussi bien sous NT4/SP5 que Win95/OSR2) -- a+ --- Dier ---------------------------------------------------------------------- ----------------- 'Déclarations des API, structures et constantes 'quitter Windows Private declare function ExitWindowsEx Lib "user32" _ (ByVal uFlags As long, _ ByVal dwReserved As long) As long 'valeurs de uFlags Private Const EWX_LOGOFF As long = 0 Private Const EWX_SHUTDOWN As long = 1 Private Const EWX_REBOOT As long = 2 Private Const EWX_FORCE As long = 4 Private Const EWX_POWEROFF As long = 8 Private Const EWX_FORCEIFHUNG = 10& ' W2000 seulement Private Const EWX_RESET = EWX_LOGOFF Or EWX_FORCE Or EWX_REBOOT 'la version de Windows Private type OSVERSIONINFO dwOSVersionInfoSize As long dwMajorVersion As long dwMinorVersion As long dwBuildNumber As long dwPlatformId As long szCSDVersion As String * 128 end type Private declare function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As long 'valeurs retournés Private Const VER_PLATFORM_WIN32_NT = 2 Private Const VER_PLATFORM_WIN32_WINDOWS = 1 Private Const VER_PLATFORM_WIN32s = 0 'le process courant Private declare function GetCurrentProcess Lib "kernel32" () As long 'obtient un tokenHandle pour l'accès désiré Private declare function OpenProcesstoken Lib "advapi32.dll" _ (ByVal ProcessHandle As long, _ ByVal DesiredAccess As long, _ tokenHandle As long) As long 'constantes pour DesiredAccess Private Const TOKEN_ADJUST_PRIVILEGES = &H20 Private Const TOKEN_QUERY = &H8 'LUID (local unique identifier) Private type LUID LowPart As long HighPart As long 'unused end type 'retourne le LUID (local unique identifier) 'identifiant le privilège spécifié dans lpname Private declare function LookupPrivilegevalue Lib "advapi32.dll" _ Alias "LookupPrivilegevalueA" _ (ByVal lpSystemname As String, _ ByVal lpname As String, _ lpLuid As LUID) As long 'constante pour lpname Private Const PrivilegeShutdown = "SeShutdownPrivilege" 'structure qui contient les infos sur un privilège Private type TOKEN_PRIVILEGES Privilegecount As long pLuid As LUID Attributes As long end type 'constantes pour le membre Attributes de la structure TOKEN_PRIVILEGES Private Const SE_PRIVILEGE_ENABLED = &H2 Private Const SE_PRIVILEGE_ENABLED_BY_DEFAULT = &H1 Private Const SE_PRIVILEGE_USED_FOR_ACCESS = &H80000000 'modifie la valeur d'un privilège donné Private declare function AdjusttokenPrivileges Lib "advapi32.dll" _ (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 'ferme le handle Private declare function CloseHandle Lib "kernel32" _ (ByVal hObject As long) As long Public function ShutdownWindows() 'fonction qui ferme Windows dim lngRet As long 'si NT, le process doit avoir le droit de fermer Windows if GetOperatingSystemVersion = VER_PLATFORM_WIN32_NT then if Not setPermissiontoken then msgbox "Impossible de donner le privilège Shutdown au process en cours", vbCritical, "Erreur" Exit function end if end if 'ferme Windows lngRet = ExitWindowsEx(EWX_REBOOT Or EWX_FORCE, 0) end function Private function GetOperatingSystemVersion() As long 'fonction qui retourne la version de Windows dim OSV As OSVERSIONINFO OSV.dwOSVersionInfoSize = Len(OSV) if GetVersionEx(OSV) <> 0 then select Case OSV.dwPlatformId Case VER_PLATFORM_WIN32_NT GetOperatingSystemVersion = VER_PLATFORM_WIN32_NT Case VER_PLATFORM_WIN32s GetOperatingSystemVersion = VER_PLATFORM_WIN32s Case VER_PLATFORM_WIN32_WINDOWS GetOperatingSystemVersion = VER_PLATFORM_WIN32_WINDOWS Case Else msgbox "Impossible de trouver la version de Windows", vbCritical, "Erreur" end select end if end function Private function setPermissiontoken() As Boolean 'fonction qui donne le droit de fermer Windows au process en cours dim lngRet As long dim hProcess As long dim htoken As long dim udtLUID_get As LUID dim udttokenP_old As TOKEN_PRIVILEGES dim udttokenP_new As TOKEN_PRIVILEGES dim lBufferNeeded As long hProcess = GetCurrentProcess() if hProcess <> 0 then lngRet = OpenProcesstoken(hProcess, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, htoken) if lngRet <> 0 then lngRet = LookupPrivilegevalue(vbNullString, PrivilegeShutdown, udtLUID_get) if lngRet <> 0 then with udttokenP_new .Privilegecount = 1 .pLuid = udtLUID_get .Attributes = SE_PRIVILEGE_ENABLED end with lngRet = AdjusttokenPrivileges(htoken, false, udttokenP_new, Len(udttokenP_old), udttokenP_old, lBufferNeeded) CloseHandle htoken if lngRet <> 0 then setPermissiontoken = true Exit function end if end if end if end if setPermissiontoken = false end function