开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 1846|回复: 1
收起左侧

vb关机

[复制链接]
发表于 2011-2-27 14:54:03 | 显示全部楼层 |阅读模式   福建省福州市
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long

Const SHUTDOWN = 1
Const REBOOT = 2
Const LOGOFF = 0

Private Sub Command1_Click()
sh = ExitWindowsEx(SHUTDOWN, dwReserved)
End Sub

Private Sub Command2_Click()
sh = ExitWindowsEx(REBOOT, dwReserved)

End Sub

Private Sub Command3_Click()
sh = ExitWindowsEx(LOGOFF, dwReserved)
End Sub
  
--
不是,权限关系,xp下如果想要关机 要首先提高权限才可以的!
Option Explicit
'==============================
'    关机与提高进程权限     声明
'============================
  Public Enum HowExitConst
               EWX_FORCE = 4 ' 强制关机
               EWX_LOGOFF = 0 ' 注销
               EWX_REBOOT = 2 ' 重起
               EWX_SHUTDOWN = 1 ' 关机
  End Enum

  Const TOKEN_ADJUST_PRIVILEGES = &H20
  Const TOKEN_QUERY = &H8
  Const SE_PRIVILEGE_ENABLED = &H2
  Const ANYSIZE_ARRAY = 1
  Public Type LUID
               lowpart As Long
               highpart As Long
  End Type

Public Type LUID_AND_ATTRIBUTES
   pLuid As LUID
   Attributes As Long
End Type

Public Type TOKEN_PRIVILEGES
   PrivilegeCount As Long
   Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES
End Type

Public Declare Function ExitWindowsEx Lib "user32" ( _
   ByVal uFlags As Long, _
   ByVal dwReserved As Long) As Long   '关机声明

Public Declare Function GetCurrentProcess Lib "kernel32" () As Long

Public Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" ( _
          ByVal lpSystemName As String, _
          ByVal lpName As String, _
          lpLuid As LUID) _
          As Long

Public 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
Public Declare Function OpenProcessToken Lib "advapi32.dll" _
         (ByVal ProcessHandle As Long, _
          ByVal DesiredAccess As Long, _
          TokenHandle As Long) As Long
'==============================
'    当前进程权限的提高
'==============================

Public Sub AdjustToken()

  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
   
  hdlProcessHandle = GetCurrentProcess() '获取当前进程ID
  OpenProcessToken hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), _
        hdlTokenHandle

  
  LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
  tkp.PrivilegeCount = 1
  tkp.Privileges(0).pLuid = tmpLuid
  tkp.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
                                                    '权限的提高

  AdjustTokenPrivileges hdlTokenHandle, False, tkp, Len(tkpNewButIgnored), _
                       tkpNewButIgnored, lBufferNeeded
End Sub
'=============================
'           关机操作
'=============================
Public Sub shutWindows()
  
  Call AdjustToken
  Call ExitWindowsEx(EWX_SHUTDOWN Or EWX_FORCE, 0)
End Sub

'=============================
'           注销操作
'=============================
Public Sub LogOffWindows()
  
  Call AdjustToken
  Call ExitWindowsEx(EWX_LOGOFF, 0)
End Sub
'=============================
'           重起操作
'=============================
Public Sub ReBootWindows()
  
  Call AdjustToken
  Call ExitWindowsEx(EWX_REBOOT, 0)
End Sub





Dim objSWbemServices As SWbemServices
Dim objSWbemObjectSet As SWbemObjectSet
Dim objSWbemObject As SWbemObject

Private Const WMI_LOGOFF = 0& '退出(注销)
Private Const WMI_SHUTDOWN = 1& '关机
Private Const WMI_REBOOT = 2& '重启动
Private Const WMI_FORCE = 4& '强制退出,即不通知现在活动应用程序让其先自我关闭
Private Const WMI_POWEROFF = 8& '关闭电源

Private Sub Command1_Click()
    Set objSWbemServices = GetObject("winmgmts:{impersonationLevel=impersonate,(Shutdown)}!")
    Set objSWbemObjectSet = objSWbemServices.ExecQuery("Select * from Win32_OperatingSystem")
    For Each objSWbemObject In objSWbemObjectSet
        objSWbemObject.Win32Shutdown WMI_POWEROFF, 0&   '关闭电源
    Next
End Sub

Private Sub Command2_Click()
   Set objSWbemServices = GetObject("winmgmts:{impersonationLevel=impersonate,(Shutdown)}!")
    Set objSWbemObjectSet = objSWbemServices.ExecQuery("Select * from Win32_OperatingSystem")
    For Each objSWbemObject In objSWbemObjectSet
        objSWbemObject.Win32Shutdown WMI_REBOOT, 0&   '重启
    Next
End Sub

Private Sub Command3_Click()
   Set objSWbemServices = GetObject("winmgmts:{impersonationLevel=impersonate,(Shutdown)}!")
    Set objSWbemObjectSet = objSWbemServices.ExecQuery("Select * from Win32_OperatingSystem")
    For Each objSWbemObject In objSWbemObjectSet
        objSWbemObject.Win32Shutdown WMI_LOGOFF, 0&   '注消
    Next
End Sub

Private Sub Command4_Click()
   Set objSWbemServices = GetObject("winmgmts:{impersonationLevel=impersonate,(Shutdown)}!")
    Set objSWbemObjectSet = objSWbemServices.ExecQuery("Select * from Win32_OperatingSystem")
    For Each objSWbemObject In objSWbemObjectSet
        objSWbemObject.Win32Shutdown WMI_SHUTDOWN, 0&   '关机
    Next
End Sub

Private Sub Command5_Click()
   Set objSWbemServices = GetObject("winmgmts:{impersonationLevel=impersonate,(Shutdown)}!")
    Set objSWbemObjectSet = objSWbemServices.ExecQuery("Select * from Win32_OperatingSystem")
    For Each objSWbemObject In objSWbemObjectSet
        objSWbemObject.Win32Shutdown WMI_FORCE, 0&   '强制退出
    Next
End Sub
结帖率:0% (0/2)
发表于 2013-5-1 23:04:47 | 显示全部楼层   江苏省苏州市
我就是要这个,
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报电话0663-3422125,QQ: 793400750,邮箱:wp@125.la
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表