大家好,又见面了,我是你们的朋友全栈君。下面是本人愿来写的关机程序可以适用于98/xp/2000,在程序中调用即可。现在操作系统多为2000或xp,所以需要特别注意的是应该先得到关机的特权:(要想弄懂下面的程序,先要具备vb调用api函数的知识……)
其中:前面一些Public Declare都是api函数的声明. Public Sub AdjustToken()子程序用来取得关机特权. Public Sub Shutdown() ‘是关机子程序 Public Sub Reboot() ‘是重启子程序 *********************代码开始了:***************** Public Structure LUID Dim UsedPart As Integer Dim IgnoredForNowHigh32BitPart As Integer End Structure Public Structure LUID_AND_ATTRIBUTES Dim TheLuid As LUID Dim Attributes As Integer End Structure Public Structure TOKEN_PRIVILEGES Dim PrivilegeCount As Integer Dim TheLuid As LUID Dim Attributes As Integer End Structure ‘强制关机函数 Public Declare Function ExitWindowsEx Lib “user32” (ByVal uFlags As Integer, ByVal dwReserved As Integer) As Integer ‘GetLastError函数返回本线程的最后一次错误代码。错误代码是按照线程 ‘储存的,多线程也不会覆盖其他线程的错误代码。 Public Declare Function GetLastError Lib “kernel32” () As Integer ‘GetCurrentProcess函数返回当前进程的一个句柄。 Public Declare Function GetCurrentProcess Lib “kernel32” () As Integer ‘OpenProcessToken函数打开一个进程的访问代号。 Public Declare Function OpenProcessToken Lib “advapi32” (ByVal ProcessHandle As Integer, ByVal DesiredAccess As Integer, ByRef TokenHandle As Integer) As Integer ‘LookupPrivilegeValue函数获得本地唯一的标示符(LUID),用于在特定的系统中 ‘表示特定的优先权。 ‘UPGRADE_WARNING: 结构 LUID 可能要求封送处理属性作为此声明语句中的参数传递。 单击以获得更多信息:“ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword=”vbup1050″” Public Declare Function LookupPrivilegeValue Lib “advapi32” Alias “LookupPrivilegeValueA”(ByVal lpSystemName As String, ByVal lpName As String, ByRef lpLuid As LUID) As Integer ‘AdjustTokenPrivileges函数使能或者禁用指定访问记号的优先权。 ‘使能或者禁用优先权需要TOKEN_ADJUST_PRIVILEGES访问权限。 ‘UPGRADE_WARNING: 结构 TOKEN_PRIVILEGES 可能要求封送处理属性作为此声明语句中的参数传递。 单击以获得更多信息:“ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword=”vbup1050″” ‘UPGRADE_WARNING: 结构 TOKEN_PRIVILEGES 可能要求封送处理属性作为此声明语句中的参数传递。 单击以获得更多信息:“ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword=”vbup1050″” Public Declare Function AdjustTokenPrivileges Lib “advapi32” (ByVal TokenHandle As Integer, ByVal DisableAllPrivileges As Integer, ByRef NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Integer, ByRef PreviousState As TOKEN_PRIVILEGES, ByRef ReturnLength As Integer) As Integer Public Declare Sub SetLastError Lib “kernel32” (ByVal dwErrCode As Integer) ‘******************************************************************** ‘* 这个过程设置正确的优先权,以允许在Windows NT下关机或者重新启动。 ‘******************************************************************** Public Sub AdjustToken() Const TOKEN_ADJUST_PRIVILEGES As Short = &H20s Const TOKEN_QUERY As Short = &H8s Const SE_PRIVILEGE_ENABLED As Short = &H2s Dim hdlProcessHandle As Integer Dim hdlTokenHandle As Integer Dim tmpLuid As LUID Dim tkp As TOKEN_PRIVILEGES Dim tkpNewButIgnored As TOKEN_PRIVILEGES Dim lBufferNeeded As Integer ‘使用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 ‘ 设置一个优先权 ‘UPGRADE_WARNING: 未能解析对象 tkp.TheLuid 的默认属性。 单击以获得更多信息:“ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword=”vbup1037″” 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 Public Sub Shutdown() ‘关机子程序 ‘******************根据windows版本来关机************************ If glngWhichWindows32 = mlngWindowsNT Then AdjustToken() ‘调用取得优先权子程序 End If ExitWindowsEx(EWX_SHUTDOWN Or EWX_FORCE, &HFFFFs) ‘***************************************************************** End Sub Public Sub Reboot() ‘重启子程序 ‘******************根据windows版本来关机************************ If glngWhichWindows32 = mlngWindowsNT Then AdjustToken() ‘调用取得优先权子程序 End If ExitWindowsEx(EWX_REBOOT Or EWX_FORCE, &HFFFFs) ‘***************************************************************** End Sub
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/162980.html原文链接:https://javaforall.cn