VB实现关机程序

VB实现关机程序下面是本人愿来写的关机程序可以适用于98/xp/2000,在程序中调用即可。现在操作系统多为2000或xp,所以需要特别注意的是应该先得到关机的特权:(要想弄懂下面的程序,先要具备vb调用api函数的知识……)其中:前面一些Public Declare都是api函数的声明.     Public Sub AdjustToken()子程序用来取得关机特权.     Public Sub Sh

大家好,又见面了,我是你们的朋友全栈君。下面是本人愿来写的关机程序可以适用于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.net/162980.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • decltype 使用

    decltype 使用功能decltype可以将给定的表达式或变量的类型推导出来,包含引用和指针。一般用于复杂表达式作为返回值的类型推导。可以用于补足c++11的auto缺陷.编译阶段的事情,不会任何执行,表达式也不会执行。类型规则规则一:声明类型,类型包含引用就有引用,没有引用也不会自行添加。规则二:返回值则根据返回值类型确定最终类型。规则三:表达式根据默认和重载得到的最终类型。不建议特别复杂的表达式。声明类型分析案例一intmain()..

    2022年9月9日
    2
  • openwrt外网web管理_OpenAPI

    openwrt外网web管理_OpenAPI转自:http://odoodevelop.lofter.com/1.web模块注意,OpenERP模块中web部分用到的所有文件必须被放置在模块内的 static 文件夹里。这是强制性的,出于安全考虑。事实上,我们创建的文件夹CSS,JS和XML,仅仅是一个习惯。static文件夹oepetstore/static/css/petst

    2025年7月4日
    2
  • Vue生成二维码_视频生成二维码软件

    Vue生成二维码_视频生成二维码软件vue有两种生成二维码的方式,qrcode、vue-qr(有icon);

    2022年10月4日
    1
  • linux sftp和ftp的区别在哪?

    linux sftp和ftp的区别在哪?sftp和ftp

    2025年8月12日
    2
  • vue中map用法_vue里面的meta用法

    vue中map用法_vue里面的meta用法后端给我返回格式是这样[‘2018-8-14’,‘2018-8-14’]但是我是想要{date:“2018/08/13”,title:“”}{date:“2018/08/14”,title:“”}这样的格式一段代码搞定letarr=res.data;letnewArr=arr.map(val=&gt;{…

    2022年9月8日
    4
  • 控制结构与一个完整的类

    控制结构与一个完整的类

    2021年9月28日
    41

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号