Home » » Tips Visual Basic - Cara Merestart dan Shutdown Windows

Tips Visual Basic - Cara Merestart dan Shutdown Windows

Posted by INFO COMPUTER on Tuesday, June 1, 2010

              
Visual Basic Shutdown-RestartTerkadang dalam membuat sebuah program, kita harus men-shutdown dan merestart sistem operasi Windows untuk suatu alasan tertentu. Dalam halaman ini kita akan mempraktekkan cara tersebut dengan menggunakan Visual Basic.

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:

Option Explicit

Private Declare Function ExitWindows Lib "User32" Alias _
    "ExitWindowsEx" (ByVal dwReserved As Long, ByVal uReturnCode _
    As Long) As Long
Private Declare Function ExitWindowsEx Lib "User32" (ByVal _
    uFlags As Long, ByVal dwReserved As Long) As Long
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 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

Previous
« Prev Post

0 comments:

Post a Comment

Search