使用VBA注销Windows

时间:2018-09-28 20:03:53

标签: excel vba windows

我正在VBA中创建一个应用程序,并且在闲置15分钟左右后,该程序打开了一个单独的用户窗体,并从60开始递减计数。在这段时间过后,我希望注销Windows,以便所有数据不会受到学生的妥协,也不会破坏任何东西。然而;在花了近一个小时浏览论坛和我的教科书之后,我找不到有效的,简单的代码。任何摘录或建议,欢迎:)

非常感谢:)

2 个答案:

答案 0 :(得分:1)

您可以锁定工作站

Declare Function LockWorkStation Lib "user32.dll" () As Long
Public Sub LockPC()
    LockWorkStation
End Sub

更新,您也可以注销,但要注意所有应用程序均已关闭,通常不会保存任何内容。

Option Explicit

Private Declare Function ExitWindowsEx Lib "User32" ( _
  ByVal uFlags As Long, _
  dwReserved As Long) As Long

Private Const EWX_FORCE = 4
Private Const EWX_LOGOFF = 0
Private Const EWX_REBOOT = 2
Private Const EWX_SHUTDOWN = 1
Private Const EWX_POWEROFF = 8

Sub Logoff()
Dim Retval As Long

    Retval = ExitWindowsEx(EWX_LOGOFF, 0&)
    If Retval = 0 Then MsgBox "Could not log off", vbOKOnly + vbInformation, "Logoff"

End Sub

注意事项对于 64位Excel ,您必须调整 api声明,请查看here。通常,您只需要添加PtrSafe并将Long替换为LongLong

答案 1 :(得分:0)

您还可以使用shutdown.exe注销。下面的例子

Sub LogOffComputer()
'https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/shutdown
    Dim oShell
    Set oShell = CreateObject("Shell.Application")
    oShell.ShellExecute "cmd.exe", "shutdown.exe /l /f"
    Set oShell = Nothing
End Sub
相关问题