Article View: cn.bbs.comp.lang.visual-basic
Article #242Re: ExitWindowsEx APi����
From: ������һ
Date: Tue, 12 Dec 2006 10:19
Date: Tue, 12 Dec 2006 10:19
198 lines
7965 bytes
7965 bytes
2��������Ĵ�����봰���У�
Option Explicit
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
'ExitWindowsEx��������˳���¼���ػ������������ϵͳ
Private Declare Function ExitWindowsEx Lib "user32" _
(ByVal dwOptions As Long, _
ByVal dwReserved As Long) As Long
'GetLastError����ر��̵߳����һ�δ�����롣��������ǰ����߳�
'����ģ����߳�Ҳ���Ḳ�������̵߳Ĵ�����롣
Private Declare Function GetLastError Lib "kernel32" () As Long
Private Const mlngWindows95 = 0
Private Const mlngWindowsNT = 1
Public glngWhichWindows32 As Long
' GetVersion���ز���ϵͳ�İ汾��
Private Declare Function GetVersion Lib "kernel32" () 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
'GetCurrentProcess����ص�ǰ��̵�һ�����
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
'OpenProcessToken�����һ����̵ķ��ʴ�š�
Private Declare Function OpenProcessToken Lib "advapi32" _
(ByVal ProcessHandle As Long, _
ByVal DesiredAccess As Long, _
TokenHandle As Long) As Long
'LookupPrivilegeValue�����ñ���Ψһ�ı�ʾ��(LUID)���������ض���ϵͳ��
'��ʾ�ض�������Ȩ��
Private Declare Function LookupPrivilegeValue Lib "advapi32" _
Alias "LookupPrivilegeValueA" _
(ByVal lpSystemName As String, _
ByVal lpName As String, _
lpLuid As LUID) As Long
'AdjustTokenPrivileges����ʹ�ܻ��߽���ָ�����ʼǺŵ�����Ȩ��
'ʹ�ܻ��߽�������Ȩ��ҪTOKEN_ADJUST_PRIVILEGES����Ȩ�ޡ�
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 Sub AdjustToken()
'********************************************************************
'* ������������ȷ������Ȩ����������Windows NT�¹ػ��������������
'********************************************************************
Const TOKEN_ADJUST_PRIVILEGES = &H20
Const TOKEN_QUERY = &H8
Const SE_PRIVILEGE_ENABLED = &H2
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
'ʹ��SetLastError�������ô������Ϊ0��
'��������GetLastError�������û�д���᷵��0
SetLastError 0
' GetCurrentProcess�������� hdlProcessHandle����
hdlProcessHandle = GetCurrentProcess()
If GetLastError <> 0 Then
MsgBox "GetCurrentProcess error==" & GetLastError
End If
OpenProcessToken hdlProcessHandle, _
(TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hdlTokenHandle
If GetLastError <> 0 Then
MsgBox "OpenProcessToken error==" & GetLastError
End If
' ��ùػ�����Ȩ��LUID
LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
If GetLastError <> 0 Then
MsgBox "LookupPrivilegeValue error==" & GetLastError
End If
tkp.PrivilegeCount = 1 ' ����һ������Ȩ
tkp.TheLuid = tmpLuid
tkp.Attributes = SE_PRIVILEGE_ENABLED
' �Ե�ǰ���ʹ�ܹػ�����Ȩ
AdjustTokenPrivileges hdlTokenHandle, _
False, _
tkp, _
Len(tkpNewButIgnored), _
tkpNewButIgnored, _
lBufferNeeded
If GetLastError <> 0 Then
MsgBox "AdjustTokenPrivileges error==" & GetLastError
End If
End Sub
Private Sub cmdLogoff_Click()
ExitWindowsEx (EWX_LogOff), &HFFFF
MsgBox "ExitWindowsEx's GetLastError " & GetLastError
End Sub
Private Sub cmdForceLogoff_Click()
ExitWindowsEx (EWX_LogOff Or EWX_FORCE), &HFFFF
MsgBox "����ExitWindowsEx������GetLastError " & GetLastError
End Sub
Private Sub cmdShutdown_Click()
If glngWhichWindows32 = mlngWindowsNT Then
AdjustToken
MsgBox "����AdjustToken���GetLastError " & GetLastError
End If
ExitWindowsEx (EWX_SHUTDOWN), &HFFFF
MsgBox "����ExitWindowsEx������GetLastError " & GetLastError
End Sub
Private Sub cmdForceShutdown_Click()
If glngWhichWindows32 = mlngWindowsNT Then
AdjustToken
MsgBox "����AdjustToken���GetLastError " & GetLastError
End If
ExitWindowsEx (EWX_SHUTDOWN Or EWX_FORCE), &HFFFF
MsgBox "ExitWindowsEx's GetLastError " & GetLastError
End Sub
Private Sub Form_Load()
'********************************************************************
'* ����Ŀ����ʱ������GetVersion������ϵͳ��
'********************************************************************
Dim lngVersion As Long
lngVersion = GetVersion()
If ((lngVersion And &H80000000) = 0) Then
glngWhichWindows32 = mlngWindowsNT
MsgBox "��Windows NT��Windows 2000������"
Else
glngWhichWindows32 = mlngWindows95
MsgBox "��Windows 95/98/Me������"
End If
End Sub
�� �� HP2006@smth.edu.cn-SPAM.no (��������) �Ĵ������ᵽ: ��
: ��ExitWindowsEx �����APi��xpϵͳ�йرռ�������������Ч ��ע�����
: ����������
: �����ָ���Խ�
: ...................
--
���ͷ�����ͷ����
[m[1;34m�� ��Դ:��ˮľ���� newsmth.net��[FROM: 59.66.78.*][m
Technical Details
Message-ID:
<4RUP4I$390@newsmth.net>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news1.google.com!newsfeed.gamma.ru!Gamma.RU!newsfeed.simtel.ru!Simtel.RU!news.newsmth.net!NEWSMTHShow Full Headers
Complete Article Headers
X-Google-Thread: 109fdd,554e366d3a18ce69 X-Google-Attributes: gid109fdd,public X-Google-Language: ENGLISH,GB Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news1.google.com!newsfeed.gamma.ru!Gamma.RU!newsfeed.simtel.ru!Simtel.RU!news.newsmth.net!NEWSMTH From: why00100@newsmth.net-SPAM.no (������һ) Newsgroups: cn.bbs.comp.lang.visual-basic Subject: Re: ExitWindowsEx APi���� Date: 12 Dec 2006 10:19:41 GMT Organization: ˮľ���� Message-ID: <4RUP4I$390@newsmth.net> Mime-Version: 1.0 Content-Type: text/plain; charset="gb2312" Content-Transfer-Encoding: 8bit X-Filename: VisualBasic/e/M.1165918674.10