Foxtable(狐表)用户栏目专家坐堂 → [求助]求关机代码,不是Shutdown那种!


  共有2995人关注过本帖树形打印复制链接

主题:[求助]求关机代码,不是Shutdown那种!

帅哥哟,离线,有人找我吗?
有点甜
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2018/6/5 8:42:00 [显示全部帖子]


 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2018/6/5 9:26:00 [显示全部帖子]

以下是引用zyqzyy在2018/6/5 9:17:00的发言:
语句不能出现在方法体内?

 

1、Imports 代码不能写,删除掉;直接在每一个地方写全名,如 System.Windows.Forms.TextBox

 

2、代码要写到【全局代码】那里,class、方法等,要加上public

 

 


 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2018/6/5 10:23:00 [显示全部帖子]

Public Class Shutdown
    <StructLayout(LayoutKind.Sequential, Pack:=1)> _
    Friend Structure TokPriv1Luid
        Public Count As Integer
        Public Luid As Long
        Public Attr As Integer
    End Structure

    <DllImport("kernel32.dll", ExactSpelling:=True)> _
    Friend Shared Function GetCurrentProcess() As IntPtr
    End Function
    <DllImport("advapi32.dll", ExactSpelling:=True, SetLastError:=True)> _
    Friend Shared Function OpenProcessToken(ByVal h As IntPtr, ByVal acc As Integer, ByRef phtok As IntPtr) As Boolean
    End Function
    <DllImport("advapi32.dll", SetLastError:=True)> _
    Friend Shared Function LookupPrivilegeValue(ByVal host As String, ByVal name As String, ByRef pluid As Long) As Boolean
    End Function
    <DllImport("advapi32.dll", ExactSpelling:=True, SetLastError:=True)> _
    Friend Shared Function AdjustTokenPrivileges(ByVal htok As IntPtr, ByVal disall As Boolean, ByRef newst As TokPriv1Luid, ByVal len As Integer, ByVal prev As IntPtr, ByVal relen As IntPtr) As Boolean
    End Function
    <DllImport("user32.dll", ExactSpelling:=True, SetLastError:=True)> _
    Friend Shared Function ExitWindowsEx(ByVal DoFlag As Integer, ByVal rea As Integer) As Boolean
    End Function
    Friend Const SE_PRIVILEGE_ENABLED As Integer = &H00000002
    Friend Const TOKEN_QUERY As Integer = &H00000008
    Friend Const TOKEN_ADJUST_PRIVILEGES As Integer = &H00000020
    Friend Const SE_SHUTDOWN_NAME As String = "SeShutdownPrivilege"
    Friend Const EWX_LOGOFF As Integer = &H00000000
    Friend Const EWX_SHUTDOWN As Integer = &H00000001
    Friend Const EWX_REBOOT As Integer = &H00000002
    Friend Const EWX_FORCE As Integer = &H00000004
    Friend Const EWX_POWEROFF As Integer = &H00000008
    Friend Const EWX_FORCEIFHUNG As Integer = &H00000010

    Private Shared Sub DoExitWin(ByVal DoFlag As Integer)
        Dim ok As Boolean
        Dim tp As TokPriv1Luid
        Dim hproc As IntPtr = GetCurrentProcess()
        Dim htok As IntPtr = IntPtr.Zero
        ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, htok)
        tp.Count = 1
        tp.Luid = 0
        tp.Attr = SE_PRIVILEGE_ENABLED
        ok = LookupPrivilegeValue(Nothing, SE_SHUTDOWN_NAME, tp.Luid)
        ok = AdjustTokenPrivileges(htok, False, tp, 0, IntPtr.Zero, IntPtr.Zero)
        ok = ExitWindowsEx(DoFlag, 0)
    End Sub

    Public Shared Sub Reboot()
        DoExitWin(EWX_FORCE Or EWX_REBOOT)
    End Sub

    Public Shared Sub PowerOff()
        DoExitWin(EWX_FORCE Or EWX_POWEROFF)
    End Sub

    Public Shared Sub LogOff()
        DoExitWin(EWX_FORCE Or EWX_LOGOFF)
    End Sub
End Class

 

 

------------------

 

调用,如

 

shutdown.LogOff()

 

 


 回到顶部