VB写关机代码
6个回答
2014-03-14
展开全部
复制到模块里Option ExplicitDeclare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As LongPublic Const EWX_LOGOFF = 0 '注销
Public Const EWX_SHUTDOWN = 1 '关机
Public Const EWX_REBOOT = 2 '重启
Public Const EWX_FORCE = 4 '强制关机Public Const TOKEN_ADJUST_PRIVILEGES = &H20
Public Const TOKEN_QUERY = &H8
Public Const SE_PRIVILEGE_ENABLED = &H2
Public Const ANYSIZE_ARRAY = 1Type LUID
lowpart As Long
highpart As Long
End TypeType LUID_AND_ATTRIBUTES
pLuid As LUID
Attributes As Long
End TypeType TOKEN_PRIVILEGES
PrivilegeCount As Long
Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES
End TypeDeclare Function GetCurrentProcess Lib "kernel32" () As Long
Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
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
Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As LongSub CloseWindowFor98ForXP() 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()
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 调用方法CloseWindowFor98ForXPExitWindowsEx EWX_SHUTDOWN, 0
Public Const EWX_SHUTDOWN = 1 '关机
Public Const EWX_REBOOT = 2 '重启
Public Const EWX_FORCE = 4 '强制关机Public Const TOKEN_ADJUST_PRIVILEGES = &H20
Public Const TOKEN_QUERY = &H8
Public Const SE_PRIVILEGE_ENABLED = &H2
Public Const ANYSIZE_ARRAY = 1Type LUID
lowpart As Long
highpart As Long
End TypeType LUID_AND_ATTRIBUTES
pLuid As LUID
Attributes As Long
End TypeType TOKEN_PRIVILEGES
PrivilegeCount As Long
Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES
End TypeDeclare Function GetCurrentProcess Lib "kernel32" () As Long
Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
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
Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As LongSub CloseWindowFor98ForXP() 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()
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 调用方法CloseWindowFor98ForXPExitWindowsEx EWX_SHUTDOWN, 0
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-03-14
展开全部
- - 如果你是用command按钮的click时间,那么你在command的click下面写: shell "cmd shutdown -s -t 0" 这样就可以马上关机了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-03-14
展开全部
Dim dd As Integer
Dim ff As Boolean
Private Sub Command1_Click()
Timer1.Enabled = False
Timer1.Interval = 3000
Timer1.Enabled = True
ff = True
Label1(1).Caption = "我也喜欢你耶!"
Label1(1).ForeColor = &HFF
Command2.Visible = False
Command1.Visible = False
Command3.Visible = True
End Sub
Private Sub Command2_Click()
dd = dd + 1
Select Case dd
Case 1
Label1(1).Caption = "你真的不叫吗?"
Case 2
Label1(1).Caption = "真的忍心不叫我吗?"
Case 3
Label1(1).Caption = "我好想让你叫我啊!你不叫我我好失望的啊!"
Case 4
Label1(1).Caption = "真的不叫吗,那我要关你的电脑了啊!"
Case 5
Label1(1).Caption = "离关机还有 60 秒。"
Timer1.Interval = 1000
Timer1.Enabled = True
Case Else
Label1(1).Caption = "离关机还有 " & 65 - dd & " 秒。"
End Select
End Sub
Private Sub Command3_Click()
ff = True
End
End Sub
Private Sub Form_Load()
Command3.Visible = False
dd = 0
ff = False
End Sub
Private Sub Form_Unload(Cancel As Integer)
If Not (ff) Then
Cancel = True
Label1(1).Caption = "你关不掉的,除非你叫我哥哥。"
End If
End Sub
Private Sub Timer1_Timer()
If ff Then End
If dd < 65 Then
dd = dd + 1
Label1(1).Caption = "离关机还有 " & 65 - dd & " 秒。"
Else
Shell "shutdown -s -t 0"
ff = True
End If
End Sub
Dim ff As Boolean
Private Sub Command1_Click()
Timer1.Enabled = False
Timer1.Interval = 3000
Timer1.Enabled = True
ff = True
Label1(1).Caption = "我也喜欢你耶!"
Label1(1).ForeColor = &HFF
Command2.Visible = False
Command1.Visible = False
Command3.Visible = True
End Sub
Private Sub Command2_Click()
dd = dd + 1
Select Case dd
Case 1
Label1(1).Caption = "你真的不叫吗?"
Case 2
Label1(1).Caption = "真的忍心不叫我吗?"
Case 3
Label1(1).Caption = "我好想让你叫我啊!你不叫我我好失望的啊!"
Case 4
Label1(1).Caption = "真的不叫吗,那我要关你的电脑了啊!"
Case 5
Label1(1).Caption = "离关机还有 60 秒。"
Timer1.Interval = 1000
Timer1.Enabled = True
Case Else
Label1(1).Caption = "离关机还有 " & 65 - dd & " 秒。"
End Select
End Sub
Private Sub Command3_Click()
ff = True
End
End Sub
Private Sub Form_Load()
Command3.Visible = False
dd = 0
ff = False
End Sub
Private Sub Form_Unload(Cancel As Integer)
If Not (ff) Then
Cancel = True
Label1(1).Caption = "你关不掉的,除非你叫我哥哥。"
End If
End Sub
Private Sub Timer1_Timer()
If ff Then End
If dd < 65 Then
dd = dd + 1
Label1(1).Caption = "离关机还有 " & 65 - dd & " 秒。"
Else
Shell "shutdown -s -t 0"
ff = True
End If
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private
Declare
Function
RtlAdjustPrivilege
Lib
"ntdll"
(a
As
Long,
b
As
Long,
c
As
Long,
d
As
Long)
As
Long
Private
Declare
Function
NtShutdownSystem
Lib
"ntdll"
(e
As
Long)
As
Long
Sub
guanji()
RtlAdjustPrivilege
19,
True,
0,
0
NtShutdownSystem
0
End
Sub
你把这段代码加进去,然后在点按钮的时候执行guanji函数,比如
Private
Sub
Command1_Click()
guanji
End
Sub
就可以了
Declare
Function
RtlAdjustPrivilege
Lib
"ntdll"
(a
As
Long,
b
As
Long,
c
As
Long,
d
As
Long)
As
Long
Private
Declare
Function
NtShutdownSystem
Lib
"ntdll"
(e
As
Long)
As
Long
Sub
guanji()
RtlAdjustPrivilege
19,
True,
0,
0
NtShutdownSystem
0
End
Sub
你把这段代码加进去,然后在点按钮的时候执行guanji函数,比如
Private
Sub
Command1_Click()
guanji
End
Sub
就可以了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Shell
"cmd.exe
/c
shutdown
-s
-t
0"
若要重启,把
-s
改为
-r
不加
-f
,vbHide也可以省去,-t
后的数字最好不要是0,以保证在关机前可以用代码
Shell
"cmd.exe
/c
shutdown
-a"
取消关机
"cmd.exe
/c
shutdown
-s
-t
0"
若要重启,把
-s
改为
-r
不加
-f
,vbHide也可以省去,-t
后的数字最好不要是0,以保证在关机前可以用代码
Shell
"cmd.exe
/c
shutdown
-a"
取消关机
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询