
Untuk restart dan shutdown Windows diperlukan bantuan beberapa fungsi API Windows. Untuk lebih jelasnya, langsung saja ke prakteknya berikut. Pertama, buat sebuah project Standard EXE dalam Visual Basic. Tambahkan dua buah kontrol CommandButton ke atas Form dan beri nama masing-masing cmdShutdown dan cmdRestart, tampilannya kurang lebih seperti gambar di samping ini.
Selanjutnya, tuliskan (atau copy) listing kode di bawah ini:
Long, PreviousState As TOKEN_PRIVILEGES, ReturnLength As _
Long) As Long
Private Type LUID
UsedPart As Long
IgnoredForNowHigh32BitPart As Long
End Type
Private Type TOKEN_PRIVILEGES
PrivilegeCount As Long
TheLuid As LUID
Attributes As Long
End Type
Private Const EWX_SHUTDOWN = 1
Private Const EWX_REBOOT = 2
Private Const EWX_FORCE = 4
Private Sub AturPrivilege()
Dim hProc As Long, hToken As Long, lReturn As Long
Dim tLuid As LUID, tKP As TOKEN_PRIVILEGES
Dim tKP_Temp As TOKEN_PRIVILEGES
hProc = GetCurrentProcess()
OpenProcessToken hProc, (&H20 Or &H8), hToken
LookupPrivilegeValue "", "SeShutdownPrivilege", tLuid
tKP.PrivilegeCount = 1
tKP.TheLuid = tLuid
tKP.Attributes = &H2
AdjustTokenPrivileges hToken, False, tKP, Len(tKP_Temp), _
tKP_Temp, lReturn
End Sub
' ---- Ini event untuk tombol cmdRestart
Private Sub cmdRestart_Click()
On Error Resume Next
ExitWindows (EWX_REBOOT Or EWX_FORCE), &HFFFFFFFF
AturPrivilege
ExitWindowsEx (EWX_REBOOT Or EWX_FORCE), &HFFFF
End Sub
' ---- Ini event untuk tombol cmdShutdown
Private Sub cmdShutdown_Click()
On Error Resume Next
ExitWindows (EWX_SHUTDOWN Or EWX_FORCE), &HFFFFFFFF
AturPrivilege
ExitWindowsEx (EWX_SHUTDOWN Or EWX_FORCE), &HFFFF
End Sub
Setelah selesai, jangan lupa simpan project tersebut, lalu jalankan (tekan F5). Sebelum meng-klik pada salah satu tombol di atas Form, pastikan program-program lain yang sedang anda jalankan agar ditutup, terutama jika sedang mengedit dokumen. Karena saat proses shutdown/restart, semua program akan ditutup paksa dan dokumen yang belum tersimpan akan diabaikan.
Oke .. demikian tips VB kali ini, mudah-mudahan bermanfaat.
Long) As Long
Private Type LUID
UsedPart As Long
IgnoredForNowHigh32BitPart As Long
End Type
Private Type TOKEN_PRIVILEGES
PrivilegeCount As Long
TheLuid As LUID
Attributes As Long
End Type
Private Const EWX_SHUTDOWN = 1
Private Const EWX_REBOOT = 2
Private Const EWX_FORCE = 4
Private Sub AturPrivilege()
Dim hProc As Long, hToken As Long, lReturn As Long
Dim tLuid As LUID, tKP As TOKEN_PRIVILEGES
Dim tKP_Temp As TOKEN_PRIVILEGES
hProc = GetCurrentProcess()
OpenProcessToken hProc, (&H20 Or &H8), hToken
LookupPrivilegeValue "", "SeShutdownPrivilege", tLuid
tKP.PrivilegeCount = 1
tKP.TheLuid = tLuid
tKP.Attributes = &H2
AdjustTokenPrivileges hToken, False, tKP, Len(tKP_Temp), _
tKP_Temp, lReturn
End Sub
' ---- Ini event untuk tombol cmdRestart
Private Sub cmdRestart_Click()
On Error Resume Next
ExitWindows (EWX_REBOOT Or EWX_FORCE), &HFFFFFFFF
AturPrivilege
ExitWindowsEx (EWX_REBOOT Or EWX_FORCE), &HFFFF
End Sub
' ---- Ini event untuk tombol cmdShutdown
Private Sub cmdShutdown_Click()
On Error Resume Next
ExitWindows (EWX_SHUTDOWN Or EWX_FORCE), &HFFFFFFFF
AturPrivilege
ExitWindowsEx (EWX_SHUTDOWN Or EWX_FORCE), &HFFFF
End Sub
Setelah selesai, jangan lupa simpan project tersebut, lalu jalankan (tekan F5). Sebelum meng-klik pada salah satu tombol di atas Form, pastikan program-program lain yang sedang anda jalankan agar ditutup, terutama jika sedang mengedit dokumen. Karena saat proses shutdown/restart, semua program akan ditutup paksa dan dokumen yang belum tersimpan akan diabaikan.
Oke .. demikian tips VB kali ini, mudah-mudahan bermanfaat.
Thanks for reading & sharing INFO COMPUTER
0 comments:
Post a Comment