VB °æ (¾«»ªÇø)
·¢ÐÅÈË: student (earth), ÐÅÇø: VB
±ê Ìâ: ÀûÓÃAPIº¯ÊýʵÏÖ Win95/98/NT µÄ×¢Ïú¡¢ÖØÆô¡¢¹Ø±Õ¼ÆË
·¢ÐÅÕ¾: ¹þ¹¤´ó×϶¡Ïã (2001Äê08ÔÂ28ÈÕ12:10:52 ÐÇÆÚ¶þ), Õ¾ÄÚÐżþ
'ÀûÓÃAPIº¯ÊýÄ£Äâ Win95/98/NT µÄ×¢Ïú¡¢ÖØÆô¡¢¹Ø±Õ¼ÆËã»ú
Option Explicit
Private Const EWX_LOGOFF = 0
Private Const EWX_SHUTDOWN = 1
Private Const EWX_REBOOT = 2
Private Const EWX_FORCE = 4
Private Const TOKEN_ADJUST_PRIVILEGES = &H20
Private Const TOKEN_QUERY = &H8
Private Const SE_PRIVILEGE_ENABLED = &H2
Private Const ANYSIZE_ARRAY = 1
Private Const VER_PLATFORM_WIN32_NT = 2 '²Ù×÷ϵͳÀàÐÍ( 2-NT )
Private Type OSVERSIONINFO
dwOSVersionInfoSize As Long '½á¹¹µÄ´óС£¨148£©<ÔÚÕýʽµ÷Óú¯Êý֮ǰ£¬±ØÐë
ÏȽ«Õâ¸ö½á¹¹µÄdwOSVersionInfoSize×Ö¶ÎÉèΪ½á¹¹µÄ´óС£¨148£©>
dwMajorVersion As Long 'Ö÷°æ±¾ºÅ
dwMinorVersion As Long '´Î°æ±¾ºÅ
dwBuildNumber As Long 'Éú³ÉºÅ
dwPlatformId As Long '²Ù×÷ϵͳÀàÐÍ( 1-95/98 ; 2-NT )
szCSDVersion As String * 128 '°æ±¾ºÅ(ÐÎÈç'µÚ¼¸°æ')
End Type
Private Type LUID
LowPart As Long
HighPart As Long
End Type
Private Type LUID_AND_ATTRIBUTES
pLuid As LUID
Attributes As Long
End Type
Private Type TOKEN_PRIVILEGES
PrivilegeCount As Long
Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES
End Type
'»ñÈ¡µ±Ç°½ø³ÌµÄÒ»¸öα¾ä±ú
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandl
e As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPr
ivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid
As LUID) As Long
Private Declare Function AdjustTokenPrivileges Lib "advapi32" (ByVal TokenHa
ndle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEG
ES, ByVal BufferLength As Long, PreviousState As TOKEN_PRIVILEGES, ReturnLen
gth As Long) As Long
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, B
yVal dwReserved As Long) As Long
Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (
ByRef lpVersionInformation As OSVERSIONINFO) As Long
'²âÊÔϵͳÀàÐÍ£¬Èç¹ûÔËÐÐÔÚNTϲ»Óô˹ý³Ì
Public Function IsWinNT() As Boolean
Dim myOS As OSVERSIONINFO
myOS.dwOSVersionInfoSize = Len(myOS)
GetVersionEx myOS
IsWinNT = (myOS.dwPlatformId = VER_PLATFORM_WIN32_NT)
End Function
'ÊÚȨµ±Ç°Ó¦ÓóÌÐòµÄ¹Ø±Õ²Ù×÷ϵͳȨÏÞ
Private Sub EnableShutDown()
Dim hProc As Long
Dim hToken As Long
Dim mLUID As LUID
Dim mPriv As TOKEN_PRIVILEGES
Dim mNewPriv As TOKEN_PRIVILEGES
hProc = GetCurrentProcess() '»ñÈ¡µ±Ç°½ø³ÌµÄÒ»¸öα¾ä±ú
OpenProcessToken hProc, TOKEN_ADJUST_PRIVILEGES + TOKEN_QUERY, hToken
LookupPrivilegeValue "", "SeShutdownPrivilege", mLUID
mPriv.PrivilegeCount = 1
mPriv.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
mPriv.Privileges(0).pLuid = mLUID
'ÔÊÐíµ±Ç°Ó¦ÓóÌÐòÓйرղÙ×÷ϵͳµÄȨÏÞ
AdjustTokenPrivileges hToken, False, mPriv, 4 + (12 * mPriv.PrivilegeCou
nt), mNewPriv, 4 + (12 * mNewPriv.PrivilegeCount)
End Sub
' ¹Ø±Õ¼ÆËã»ú
Public Sub ShutDownNT(Force As Boolean)
Dim ret As Long
Dim Flags As Long
Flags = EWX_SHUTDOWN
If Force Then Flags = Flags + EWX_FORCE
If IsWinNT Then EnableShutDown
ExitWindowsEx Flags, 0
End Sub
'ÖØÆô¼ÆËã»ú
Public Sub RebootNT(Force As Boolean)
Dim ret As Long
Dim Flags As Long
Flags = EWX_REBOOT
If Force Then Flags = Flags + EWX_FORCE
If IsWinNT Then EnableShutDown
ExitWindowsEx Flags, 0
End Sub
'×¢Ïúµ±Ç°Óû§
Public Sub LogOffNT(Force As Boolean)
Dim ret As Long
Dim Flags As Long
Flags = EWX_LOGOFF
If Force Then Flags = Flags + EWX_FORCE
ExitWindowsEx Flags, 0
End Sub
Private Sub Command1_Click()
LogOffNT True
End Sub
Private Sub Command2_Click()
RebootNT True
End Sub
Private Sub Command3_Click()
ShutDownNT True
End Sub
Private Sub Form_Load()
'Command1.Caption = "Log Off NT"
'Command2.Caption = "Reboot NT"
'Command3.Caption = "Shutdown NT"
End Sub
--
¡ù À´Ô´:¡¤¹þ¹¤´ó×϶¡Ïã bbs.hit.edu.cn¡¤[FROM: 202.118.170.186]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
Ò³ÃæÖ´ÐÐʱ¼ä£º2.409ºÁÃë