标题:[求助]关于VB中调用API函数ExitWindowsEx的问题?
只看楼主
zrgong
Rank: 1
等 级:新手上路
帖 子:95
专家分:0
注 册:2007-6-26
 问题点数:0 回复次数:4 
[求助]关于VB中调用API函数ExitWindowsEx的问题?

今日看到VB程序设计中可以用下列API函数来调用关机,重启和注销操作,自己尝试了一下发现并不如自己预期的效果:

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

运行中发现系统能进行注销(Command3_Click())操作外,关机和重启均无反应。

所以想问一下原因?还有其他相关调用关机的操作命令吗?这个和操作系统应该没有关系的吧,我是XP SP2

[此贴子已经被作者于2007-6-26 22:28:58编辑过]

搜索更多相关主题的帖子: API函数 Sub Private REBOOT Const 
2007-06-26 21:55
心中有剑
Rank: 2
等 级:新手上路
威 望:5
帖 子:611
专家分:0
注 册:2007-5-18
得分:0 

98下可以


'前提要应用wmi方法后才可以用
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

你试试这个看看!最好保存程序,负责概不负责啊!哈哈


2007-06-27 08:39
zrgong
Rank: 1
等 级:新手上路
帖 子:95
专家分:0
注 册:2007-6-26
得分:0 

楼上的效果不错,先谢谢了。

你那个程序好像不是调用API函数,是调用对象了吧,不是很清楚,看来要好好学习啊,有空能发点相关资料给我吗?

还有API函数ExitWindowsEx在XP上不行是因为什么,被禁了吗?


2007-06-27 23:41
心中有剑
Rank: 2
等 级:新手上路
威 望:5
帖 子:611
专家分:0
注 册:2007-5-18
得分:0 

不是,权限关系,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
这样就可以了


2007-06-28 11:38
zrgong
Rank: 1
等 级:新手上路
帖 子:95
专家分:0
注 册:2007-6-26
得分:0 

研究一下,谢谢了楼上的


2007-06-28 20:09



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-150576-1-1.html




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

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