以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  [求助]求关机代码,不是Shutdown那种!  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=119968)

--  作者:zyqzyy
--  发布时间:2018/6/5 8:20:00
--  [求助]求关机代码,不是Shutdown那种!
求关机代码,不是Shutdown那种!
用Shutdown关机,杀毒软件回阻止!

--  作者:有点甜
--  发布时间:2018/6/5 8:42:00
--  

搜索 https://www.baidu.com/baidu?wd=c%23+%E5%85%B3%E6%9C%BA+api

 

然后转换c#为vb.net代码 http://converter.telerik.com/

 


--  作者:lzw001
--  发布时间:2018/6/5 8:55:00
--  
老师,比如,这样复杂过去的话,这个代码应该放在哪里可以执行呢
图片点击可在新窗口打开查看此主题相关图片如下:qq截图20180605085437.png
图片点击可在新窗口打开查看


--  作者:lzw001
--  发布时间:2018/6/5 8:55:00
--  
复制过去
--  作者:zyqzyy
--  发布时间:2018/6/5 9:17:00
--  
语句不能出现在方法体内?
--  作者:有点甜
--  发布时间:2018/6/5 9:26:00
--  
以下是引用zyqzyy在2018/6/5 9:17:00的发言:
语句不能出现在方法体内?

 

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

 

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

 

 


--  作者:zyqzyy
--  发布时间:2018/6/5 9:45:00
--  
没有搞定,运行不了!不懂API接口调用!
--  作者:有点甜
--  发布时间: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()

 

 


--  作者:zyqzyy
--  发布时间:2018/6/5 10:39:00
--  
谢谢,谢谢!高大上!
--  作者:evolymft
--  发布时间:2019/6/3 16:41:00
--  
请问老师:如何执行关机命令。