VB 如何寫程式控制電腦關機

VB 如何寫程式控制電腦關機

VB 如何寫程式控制電腦關機

方法 1 :

' Call API ExitWindowsEx

' 宣告型態

Private Type LUID

LowPart As Long

HighPart As Long

End Type

Private Type LUID_AND_ATTRIBUTES

pLuid As LUID

Attributes As Long

End Type

Private Type TOKEN_PRIVILEGES

PrivilegeCount As Long

Privileges(1) As LUID_AND_ATTRIBUTES

End Type

' 宣告 API

Private Declare Function GetCurrentProcess Lib "kernel32" () As Long

Private Declare Function OpenProcessToken Lib "advapi32" _

(ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long

Private Declare Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" _

(ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long

Private Declare Function AdjustTokenPrivileges Lib "advapi32" _

(ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, _

NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, _

PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long

Private Declare Function ExitWindowsEx Lib "user32" _

(ByVal uFlags As Long, ByVal dwReserved As Long) As Long

Private Sub Form_Load()

If Len(Environ("OS")) > 0 Then EnableShutDown

ExitWindowsEx 5, 0 ' 呼叫 API 進行關機

End Sub

Private Sub EnableShutDown()

Dim hProc As Long, hToken As Long

Dim mLUID As LUID

Dim mPriv As TOKEN_PRIVILEGES

Dim mNewPriv As TOKEN_PRIVILEGES

hProc = GetCurrentProcess()

OpenProcessToken hProc, 40, hToken

LookupPrivilegeValue "", "SeShutdownPrivilege", mLUID

mPriv.PrivilegeCount = 1

mPriv.Privileges(0).Attributes = 2

mPriv.Privileges(0).pLuid = mLUID

AdjustTokenPrivileges hToken, False, mPriv, 4 + (12 * mPriv.PrivilegeCount), _

mNewPriv, 4 + (12 * mNewPriv.PrivilegeCount)

End Sub

PS : 以上為 VB6 的 Code , 若在 VB.Net 使用需調整

================================================================

方法 2 :

' 使用 WMI 物件 進行關機

Dim objOS As Object

For Each objOS In GetObject("winmgmts:{(Shutdown)}").InstancesOf("Win32_OperatingSystem")

objOS.Win32Shutdown 5

Next

PS : objOS.Win32Shutdown 5 ' 5 可以替換成如下的參數

0 -> Log Off

4 -> Forced Log Off

1 -> Shutdown

5 -> Forced Shutdown

2 -> Reboot

6 -> Forced Reboot

8 -> Power Off

12 -> Forced Power Off

================================================================

方法 3 :

' 用Shell 函數來呼叫 WinXP ShutDown 指令

Shell "shutdown -s -f -t 0"

Syntax
shutdown [{-l|-s|-r|-a}] [-f] [-m [\\ComputerName]] [-t xx] [-c "message"] [-d[u][p]:xx:yy]


Parameters
-l : Logs off the current user, this is also the defualt. -m ComputerName takes precedence.

-s : Shuts down the local computer.

-r : Reboots after shutdown.

-a : Aborts shutdown. Ignores other parameters, except -l and ComputerName.

You can only use -a during the time-out period.

-f : Forces running applications to close.

-m [\\ComputerName] : Specifies the computer that you want to shut down.

-t xx : Sets the timer for system shutdown in xx seconds. The default is 20 seconds.

-c "message" : Specifies a message to be displayed in the Message area of the System Shutdown window.

You can use a maximum of 127 characters. You must enclose the message in quotation marks.

-d [u][p]:xx:yy : Lists the reason code for the shutdown. The following table lists the different values.

PS : Win2K 不提供 ShutDown 囉

================================================================

<< VB.Net 寫法 >>

請先加入參考 System.Management

Imports System.Management

Public Class Form1

Private Sub Button1_Click(ByVal s As Object, ByVal e As EventArgs) Handles Button1.Click

Dim mc As New ManagementClass("Win32_OperatingSystem")

mc.Scope.Options.EnablePrivileges = True

For Each mo As ManagementObject In mc.GetInstances()

mo.InvokeMethod("Win32Shutdown", New Object() {12})

Next

mc.Dispose()

End Sub

End Class