| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1196 人关注过本帖
标题:用API函数关闭计算机
只看楼主 加入收藏
subMain
Rank: 5Rank: 5
等 级:职业侠客
帖 子:50
专家分:357
注 册:2009-8-17
收藏
 问题点数:0 回复次数:3 
用API函数关闭计算机
用API函数关闭计算机
Windows NT
程序代码:
'模块代码
Public Const ANYSIZE_ARRAY = 1
Public Const TOKEN_ADJUST_PRIVILEGES = &H20
Public Const TOKEN_QUERY = &H8
Public Const SE_PRIVILEGE_ENABLED = &H2
Public Const EWX_LOGOFF = 0    ' 注销
Public Const EWX_SHUTDOWN = 1  ' 关机
Public Const EWX_REBOOT = 2    ' 重启
Public Const EWX_FORCE = 4     ' 强制关机
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 GetCurrentProcess Lib "kernel32" () As Long
Public Declare Function OpenProcessToken Lib "advapi32.dll" _
    (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) 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 ExitWindowsEx Lib "user32" _
    (ByVal uFlags As Long, ByVal dwReserved As Long) As Long

程序代码:
'窗口代码,添加cmdShutdown按钮
Private Sub cmdShutdown_Click()
    Call AdjustToken
   
    '关闭计算机
    ExitWindowsEx EWX_SHUTDOWN, 0
End Sub
Private Sub AdjustToken()
  Dim hdlProcessHandle As Long
  Dim hdlTokenHandle As Long
  Dim lngBufferNeeded As Long
  Dim tmpLuid As LUID
  Dim tkp As TOKEN_PRIVILEGES
  Dim tkpNewButIgnored As TOKEN_PRIVILEGES
 
  hdlProcessHandle = GetCurrentProcess()
 
  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, lngBufferNeeded
End Sub

上面的代码用于关闭Windows2000、XP等操作系统,如果关闭98或95可以只用ExitWindowsEx函数。
'这些是其它常量
Public Const EWX_LOGOFF = 0    ' 注销
Public Const EWX_SHUTDOWN = 1  ' 关机
Public Const EWX_REBOOT = 2    ' 重启
Public Const EWX_FORCE = 4     ' 强制关机
搜索更多相关主题的帖子: 函数 API 计算机 
2009-08-27 21:09
dahual
Rank: 1
等 级:新手上路
帖 子:19
专家分:5
注 册:2008-10-14
收藏
得分:0 
好,主要就是取得权限。
2009-08-30 20:32
liuyilin
Rank: 2
等 级:论坛游民
帖 子:26
专家分:40
注 册:2007-3-8
收藏
得分:0 
这是VB6的吧?
2009-10-05 10:46
事业男儿
Rank: 2
等 级:论坛游民
帖 子:317
专家分:14
注 册:2007-4-25
收藏
得分:0 
怎么在这程序上添加,判断后执行一幅动画,就是屏保???
2009-11-24 15:15
快速回复:用API函数关闭计算机
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.033777 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved