ªð¦^¦Cªí ¤W¤@¥DÃD µo©«

[µo°Ý] Ãö¾÷°ÝÃD

[µo°Ý] Ãö¾÷°ÝÃD

¤j®a¦n:

     ½Ð°Ý¤@¤U¡A§Ú·Q¥Îexcel vba°õ¦æ§¹µ{¦¡«á¡A¦Û°ÊÃö³¬¹q¸£¡C
     ¬d¤F«Ü¤[¡A¤£ª¾¹D«ç»ò¼Ë¥Îvbaµ{¦¡¨ÓÃö¾÷¡A©Ò¥H½Ð±Ð¤@¤U¤j®a¡A·PÁ¡C

¥»©«³Ì«á¥Ñ Joforn ©ó 2016-7-28 21:03 ½s¿è

ª`·NExitWindowsªº调¥Î参数¡A¥i¥H¤À别实现关Éó¡A­«·s±Ò°Ê¥H¤Îµn¥X·í«e¥Î¤á[code]Option Explicit
'这个¬O关É󱱨î¼Ò块
#If VBA7 Then
  Private Declare PtrSafe Function ExitWindowsEx Lib "user32" (ByVal dwOptions As Long, ByVal dwReserved As Long) As Long
  Private Declare PtrSafe Function GetLastError Lib "KERNEL32" () As Long
  Private Declare PtrSafe Function GetCurrentProcess Lib "KERNEL32" () As Long
  Private Declare PtrSafe Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
  Private Declare PtrSafe Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
  Private Declare PtrSafe 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
  Private Declare PtrSafe Sub SetLastError Lib "KERNEL32" (ByVal dwErrCode As Long)
  Private Declare PtrSafe Function GetVersion Lib "KERNEL32" () As Long
#Else
  Private Declare Function ExitWindowsEx Lib "user32" (ByVal dwOptions As Long, ByVal dwReserved As Long) As Long
  Private Declare Function GetLastError 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 TOKEN_PRIVILEGES, ReturnLength As Long) As Long
  Private Declare Sub SetLastError Lib "KERNEL32" (ByVal dwErrCode As Long)
  Private Declare Function GetVersion Lib "KERNEL32" () As Long
#End If
'Public glngWhichWindows32 As Long

Private Type LUID
  UsedPart As Long
  IgnoredForNowHigh32BitPart As Long
End Type

Private Type LUID_AND_ATTRIBUTES
  TheLuid As LUID
  Attributes As Long
End Type

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

Public Enum ExitMode
  Exit_LOGOFF = 0&
  Exit_SHUTDOWN = 1&
  Exit_REBOOT = 2&
  Exit_FORCE = 4&
  Exit_POWEROFF = 8&
  Exit_FORCEIFHUNG = &H10&
  Exit_RESTARTAPPS = &H40&
End Enum

Public Function ExitWindows(Optional ByVal ExitMode As ExitMode = Exit_SHUTDOWN Or Exit_FORCE) As Long

'********************************************************************
'* 这个过µ{¤¹许µ{§Ç¦bWindows¤U关Éó©ÎªÌ­«·s
¥@¬É¨º»ò¤j¡A¥i§Ú·Q¥h­þ¡H

TOP

shell  "shutdown .........."

win7 ¤£ª¾¹D¦³¨S¦³ shutdown.exe

http://idaiwan.pixnet.net/blog/post/27604073-%E4%BD%BF%E7%94%A8-shutdown.exe-%E5%AF%A6%E7%8F%BE-windows-%E4%B8%80%E9%8D%B5%E9%87%8D%E6%96%B0%E9%96%8B%E6%A9%9F%E3%80%81%E9%97%9C

TOP

¦^´_ 1# spermbank
ª`·NExitWindows³o­Óµ{¦¡ªº½Õ¥Î°Ñ¼Æ¡A¥i¥H¤À§O¥Î¥H实现Ãö³¬¹q·½¡B­«·s±Ò°ÊWindows©Î¬Oµn¥X·í«e¥Î¤á¡C
  1. #If VBA7 Then
  2.   Private Declare PtrSafe Function ExitWindowsEx Lib "user32" (ByVal dwOptions As Long, ByVal dwReserved As Long) As Long
  3.   Private Declare PtrSafe Function GetLastError Lib "KERNEL32" () As Long
  4.   Private Declare PtrSafe Function GetCurrentProcess Lib "KERNEL32" () As Long
  5.   Private Declare PtrSafe Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
  6.   Private Declare PtrSafe Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
  7.   Private Declare PtrSafe 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
  8.   Private Declare PtrSafe Sub SetLastError Lib "KERNEL32" (ByVal dwErrCode As Long)
  9.   Private Declare PtrSafe Function GetVersion Lib "KERNEL32" () As Long
  10. #Else
  11.   Private Declare Function ExitWindowsEx Lib "user32" (ByVal dwOptions As Long, ByVal dwReserved As Long) As Long
  12.   Private Declare Function GetLastError Lib "KERNEL32" () As Long
  13.   Private Declare Function GetCurrentProcess Lib "KERNEL32" () As Long
  14.   Private Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
  15.   Private Declare Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
  16.   Private 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
  17.   Private Declare Sub SetLastError Lib "KERNEL32" (ByVal dwErrCode As Long)
  18.   Private Declare Function GetVersion Lib "KERNEL32" () As Long
  19. #End If

  20. Private Type LUID
  21.   UsedPart As Long
  22.   IgnoredForNowHigh32BitPart As Long
  23. End Type

  24. Private Type LUID_AND_ATTRIBUTES
  25.   TheLuid As LUID
  26.   Attributes As Long
  27. End Type

  28. Private Type TOKEN_PRIVILEGES
  29.   PrivilegeCount As Long
  30.   TheLuid As LUID
  31.   Attributes As Long
  32. End Type

  33. Public Enum ExitMode
  34.   Exit_LOGOFF = 0&
  35.   Exit_SHUTDOWN = 1&
  36.   Exit_REBOOT = 2&
  37.   Exit_FORCE = 4&
  38.   Exit_POWEROFF = 8&
  39.   Exit_FORCEIFHUNG = &H10&
  40.   Exit_RESTARTAPPS = &H40&
  41. End Enum

  42. Public Function ExitWindows(Optional ByVal ExitMode As ExitMode = Exit_SHUTDOWN Or Exit_FORCE) As Long

  43. '********************************************************************
  44. '* ³o­Óµ{¦¡¥Î¨ÓÃö³¬¡B­«±Ò­pºâ¾÷©Î¬Oµn¥X·í«e¥Î¤á
  45. '********************************************************************

  46.   Const TOKEN_ADJUST_PRIVILEGES = &H20
  47.   Const TOKEN_QUERY = &H8
  48.   Const SE_PRIVILEGE_ENABLED = &H2
  49.   
  50.   Dim hdlProcessHandle  As Long
  51.   Dim hdlTokenHandle    As Long
  52.   Dim tmpLuid           As LUID
  53.   Dim tkp               As TOKEN_PRIVILEGES
  54.   Dim tkpNewButIgnored  As TOKEN_PRIVILEGES
  55.   Dim lBufferNeeded     As Long
  56.   Dim lngVersion        As Long
  57.   
  58.   On Error GoTo ErrorExit
  59.   Err.Clear
  60.   lngVersion = GetVersion()
  61.   If ((lngVersion And &H80000000) = 0) Then
  62.     SetLastError 0
  63.     hdlProcessHandle = GetCurrentProcess()
  64.     ExitWindows = GetLastError:    If ExitWindows Then Exit Function
  65.     OpenProcessToken hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hdlTokenHandle
  66.     ExitWindows = GetLastError:    If ExitWindows Then Exit Function
  67.     LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
  68.     ExitWindows = GetLastError:    If ExitWindows Then Exit Function
  69.     tkp.PrivilegeCount = 1
  70.     tkp.TheLuid = tmpLuid
  71.     tkp.Attributes = SE_PRIVILEGE_ENABLED
  72.     AdjustTokenPrivileges hdlTokenHandle, False, tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded
  73.     ExitWindows = GetLastError:    If ExitWindows Then Exit Function
  74.   End If
  75.   If ExitWindowsEx(ExitMode, &HFFFF) = 0 Then ExitWindows = GetLastError
  76.   Exit Function
  77. ErrorExit:
  78.   ExitWindows = Err.Number
  79. End Function

  80. Sub PowerOff()
  81.   ExitWindows Exit_POWEROFF Or Exit_FORCE
  82. End Sub
½Æ»s¥N½X
¤W¤@¼Ó¦^´_¤£ª¾¬°¦ó³Q½×¾Â¦Y±¼¤F¤@¥bªº¥N½X¡K¡K
¥@¬É¨º»ò¤j¡A¥i§Ú·Q¥h­þ¡H

TOP

´£¿ô¡GExit_FORCEªí¥Ü¦Û°ÊÃö³¬«á¥x¹B¦æ¤¤ªºµ{¦¡¡A³o¼Ë´N¤£·|¦]¬°¨ä¥¦µ{¦¡ªº­ì¦]¾É­PÃö³¬Windows¥¢±Ñ¡A¦ý¬O¦pªG¨Ï¥Î¤F³o­Ó°Ñ¼Æ¡A¤@©w­n°O±o§â¦Û¤vªº©Ò¦³ªºÀɮתº¦s¼Ó®@¡A§_«h¡A©Ò¦³ªº¨S¦³¦sÀɪº¤å¥ó±N¥á¥¢³Ì·sªº­×§ï¡I¡I¦pªG¤£¨Ï¥Î³o­Ó°Ñ¼Æ¡A¨º»ò¤@¯ë±¡ªp¤U¥i¯à³£¤£·|Ãö³¬­pºâ¾÷¦¨¥\¡A¦]¬°«á¥x¤@¯ë·|¦³«Ü¦hµ{¦¡·|¦Û¤vªý¤îÃö¾÷«ü¥O¡C
¥@¬É¨º»ò¤j¡A¥i§Ú·Q¥h­þ¡H

TOP

        ÀR«ä¦Û¦b : ·O´d¨S¦³¼Ä¤H¡A´¼¼z¤£°_·Ð´o¡C
ªð¦^¦Cªí ¤W¤@¥DÃD