Showing posts with label Windows. Show all posts
Showing posts with label Windows. Show all posts

Monday, December 10, 2012

VB6 Code - Fungsi Untuk Menampilkan Dialog Shutdown

Di bawah ini merupakan fungsi VB6 untuk menampilkan dialog shutdown. Karena menggunakan metode Early Binding maka, Untuk keperluan ini Anda harus mereferensi objek "Microsoft Shell Controls And Automation" atau "Shell32.dll" yang biasa terletak pada SystemRoot\System32 (c:\Windows\System32\Shell32.dll).

Jika Anda ingin menggunakan metode Late Binding maka gantilah kode berikut:

Dim Sh as new Shell32.Shell

Menjadi:

Dim Sh as Object
Set Sh = CreateObject("Shell.Application")
Option Explicit

Sub ShowShutDown()
Dim sh As New Shell32.Shell
sh.ShutdownWindows
Set sh = Nothing
End Sub

'Contoh penggunaan
Private Sub Command1_Click()
Call ShowShutDown
End Sub
READ MORE - VB6 Code - Fungsi Untuk Menampilkan Dialog Shutdown

VB6 Code - Fungsi Untuk Me-Restore Seluruh Windows

Di bawah ini merupakan fungsi VB6 untuk Me-Restore Seluruh Windows. Karena menggunakan metode Early Binding maka, Untuk keperluan ini Anda harus mereferensi objek "Microsoft Shell Controls And Automation" atau "Shell32.dll" yang biasa terletak pada SystemRoot\System32 (c:\Windows\System32\Shell32.dll).

Jika Anda ingin menggunakan metode Late Binding maka gantilah kode berikut:

Dim Sh as new Shell32.Shell

Menjadi:

Dim Sh as Object
Set Sh = CreateObject("Shell.Application")
Sub RestoreAll()
Dim sh As New Shell32.Shell
sh.UndoMinimizeALL
Set sh = Nothing
End Sub

'Contoh Penggunaan Procedure Untuk Me-Minimize Seluruh Windows
Private Sub Form_Load()
RestoreAll
End Sub
READ MORE - VB6 Code - Fungsi Untuk Me-Restore Seluruh Windows

Thursday, December 6, 2012

VB Fungsi API - Mengetahui Ukuran Screen Yang Sebenarnya

Contoh fungsi API untuk mengetahui ukuran layar (screen) yang sebenarnya (dikurangi tinggi taskbar).

Option Explicit

Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long

Public Function ScreenWidth() As Single
    Dim R As RECT
    GetWindowRect GetDesktopWindow(), R
    ScreenWidth = R.Right * Screen.TwipsPerPixelX
End Function

Public Function ScreenHeight() As Single
    Dim R As RECT
    GetWindowRect GetDesktopWindow(), R
    ScreenHeight = R.Bottom * Screen.TwipsPerPixelY
End Function
READ MORE - VB Fungsi API - Mengetahui Ukuran Screen Yang Sebenarnya

Sunday, June 17, 2012

Menutup Aplikasi Lain Berdasarkan Caption Menggunakan VB6

Mengenai cara menutup (close) aplikasi lain/luar berdasarkan caption yang ditentukan menggunakan Visual Basic 6.0 - Bagaimana kode menutup aplikasi lain menggunakan VB6 ini, bisa Anda simak kodenya di bawah ini:
Option Explicit

Private Const PROCESS_ALL_ACCESS = &H1F0FFF
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Private Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wFlag As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal process As Long, lpExitCode As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal process As Long, ByVal uExitCode As Long) As Long

'Code that does the work
Public Function EndApplication(ByRef caption As String, ByRef frm As Form) As Boolean
Dim hwnd As Long
Dim appInstance As Long
Dim process As Long
Dim processID
Dim result As Boolean
Dim exitCode As Long
Dim returnValue As Long

On Error GoTo Error

If Trim(caption) = "" Then Exit Function
Do
hwnd = FindWindowByTitle(caption, frm)
If hwnd = 0 Then Exit Do
appInstance = GetWindowThreadProcessId(hwnd, processID)
'Get a handle for the process we're looking for
process = OpenProcess(PROCESS_ALL_ACCESS, 0&, processID)
If process <> 0 Then
'Next get our exit code (for use later)
GetExitCodeProcess process, exitCode
'Check for an exit code of 9 (zero)
If exitCode <> 0 Then
'It's not zero so close the window
returnValue = TerminateProcess(process, exitCode)
If result = False Then result = returnValue > 0
End If
End If
Loop
EndApplication = result
Error:
' MsgBox (Err.Number & ": " & Err.Description)
End Function

Private Function FindWindowByTitle(ByRef str As String, ByRef frm As Form) As Long
Dim handle As Long
Dim caption As String
Dim sTitle As String

handle = frm.hwnd
sTitle = LCase(str)
Do
DoEvents
If handle = 0 Then Exit Do
caption = LCase$(GetWindowCaption(handle))

If InStr(caption, sTitle) Then
FindWindowByTitle = handle
Exit Do
Else
FindWindowByTitle = 0
End If
handle = GetNextWindow(handle, 2)
Loop
End Function

Private Function GetWindowCaption(ByRef handle As Long) As String
Dim str As String
Dim length As Long

length& = GetWindowTextLength(handle)
str = String(length, 0)
Call GetWindowText(handle, str, length + 1)
GetWindowCaption = str
End Function
Contoh penggunaan kode di atas:
Private Sub Command1_Click()
Shell "Regedit", vbNormalFocus 'membuka regedit.exe
End Sub

Private Sub Command2_Click()
EndApplication "Registry Editor", Me 'menutup regedit.exe yang memiliki caption 'Registry Editor'
End Sub
READ MORE - Menutup Aplikasi Lain Berdasarkan Caption Menggunakan VB6

Friday, June 8, 2012

Effect Bayangan (Shadow) Pada Form Menggunakan VB6

Mengenai cara menambahkan effect bayangan (shadow effect) pada form - effect bayangan (shadow effect) ini akan terlihat bagus terutama pada form tanpa border (property BorderStyle = 0 - none). Bagaimana kode mengenai shadow effect ini?
Option Explicit 

Private Declare Function
GetClassLong Lib "user32" Alias "GetClassLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function
SetClassLong Lib "user32" Alias "SetClassLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const
CS_DROPSHADOW As Long = &H20000
Private Const GCL_STYLE As Long = -26

Private Sub
DropShadow(ByVal hWnd As Long)
Call SetClassLong(hWnd, GCL_STYLE, GetClassLong(hWnd, GCL_STYLE) Or CS_DROPSHADOW)
End Sub

Private Sub
Form_Load()
DropShadow Me.hWnd
End Sub

Catatan: Effect bayangan (shadow effect) akan bekerja pada saat Show shadow under menus dicheck (default). Show shadow under menus terdapat pada start >> Settings >> Control Panel >> System >> Advanced >> Settings >> Show shadow under menus.
READ MORE - Effect Bayangan (Shadow) Pada Form Menggunakan VB6

Menonaktifkan Keyboard dan Mouse - BlockInput

Option Explicit 

Private Declare Function
BlockInput Lib "user32" (ByVal fBlock As Long) As Long

Private Sub
Command1_Click()
Timer1.Enabled = True
BlockInput True
End Sub

'Gunakan kode di bawah, agar komputer Anda tidak usah di restart
Private Sub Form_Load()
Timer1.Interval = 1000 '1 detik
Timer1.Enabled = False
End Sub

'Timer1.Interval = 1000 '1 detik
Private Sub Timer1_Timer()
Static i As Integer
i = i +
1
If i > 5 Then 'tunggu 5 detik
BlockInput False 'aktifkan kembali keyboard dan mouse
i = 0
End If
End Sub
READ MORE - Menonaktifkan Keyboard dan Mouse - BlockInput

Mouse Properties Dialog, Bagaimana Cara Menampilkannya?

Di bawah merupakan kode untuk menampilkan mouse properties dialog menggunakan VB6 (Visual Basic 6) - Bagaimana menampilkan mouse properties dialog ini, bisa Anda lihat di bawah:
Option Explicit 

Private Sub
Command1_Click()
Dim dblReturn As Double
dblReturn = Shell("rundll32.exe shell32.dll,Control_RunDLL main.cpl @0", 5)
End Sub
READ MORE - Mouse Properties Dialog, Bagaimana Cara Menampilkannya?

Cara Menampilkan Dialog Properties KeyBoard Menggunakan VB6

Berikut merupakan VB6 kode untuk menampilkan kotak dialog properties keyboard:
Option Explicit 

Private Sub
Command1_Click()
Dim dblReturn As Double
dblReturn = Shell("rundll32.exe shell32.dll,Control_RunDLL main.cpl @1", 5)
End Sub
READ MORE - Cara Menampilkan Dialog Properties KeyBoard Menggunakan VB6

Mengenai cara menampilkan kotak dialog About

Mengenai cara menampilkan kotak dialog About default windows menggunakan Visual Basic 6 - Apabila kita malas membuat tampilan kotak dialog About, mungkin kode di bawah adalah alternatif yang tepat, selain itu ia memiliki tampilan (GUI) yang menarik, adapun cara menampilkan kotak dialog about default windows adalah sebagai berikut:
'simpan kode di bawah ini pada module 
Option Explicit

Public Declare Function
ShellAbout Lib "shell32.dll" Alias "ShellAboutA" (ByVal hwnd As Long, ByVal szApp As String, ByVal szOtherStuff As String, ByVal hIcon As Long) As Long

Public Sub
ShowAboutDialog(hwnd As Long, Caption As String, Txt As String, Icon As Long)
Call ShellAbout(hwnd, Caption, Txt, Icon)
End Sub
Contoh penggunaan kode untuk menampilkan kotak dialog about:
'simpan kode di bawah ini dalam module 
Option Explicit

Private Sub
cmdAbout_Click()
ShowAboutDialog Me.hwnd, "About My-Sofware", "Software Versi ke-1", Me.Icon
End Sub
READ MORE - Mengenai cara menampilkan kotak dialog About

Membuat About Box Dengan Memanfaatkan Default Windows

Mengenai cara menampilkan kotak dialog About default windows menggunakan Visual Basic 6 - Apabila kita malas membuat tampilan kotak dialog About, mungkin kode di bawah adalah alternatif yang tepat, selain itu ia memiliki tampilan (GUI) yang menarik, adapun cara menampilkan kotak dialog about default windows adalah sebagai berikut:
'simpan kode di bawah ini dalam module 
Option Explicit

Public Declare Function
ShellAbout Lib "shell32.dll" Alias "ShellAboutA" (ByVal hwnd As Long, ByVal szApp As String, ByVal szOtherStuff As String, ByVal hIcon As Long) As Long

Public Sub
ShowAboutDialog(hwnd As Long, Caption As String, Txt As String, Icon As Long)
Call ShellAbout(hwnd, Caption, Txt, Icon)
End Sub
Contoh penggunaan kode untuk menampilkan kotak dialog about:
Private Sub cmdAbout_Click() 
ShowAboutDialog Me.hwnd, "About My-Sofware", "Software Versi ke-1", Me.Icon
End Sub
READ MORE - Membuat About Box Dengan Memanfaatkan Default Windows

Menonaktifkan/Disable Task Manager Menggunakan VB6

Posting yang menjelaskan mengenai cara menonaktifkan task manager melalui kode Visual Basic 6.0 - Mendisable atau menonaktifkan task manager terkadang diperlukan untuk pembuatan aplikasi-aplikasi tertentu sebut saja billing warnet. Umumnya kode yang digunakan untuk menonaktifkan task manager dengan mengubah nilai registry, namun berbeda dengan kode di bawah ini yang mendisable task manager tanpa mengubah nilai registry. Bagaimana implementasi kode untuk mendisable task manager, bisa Anda lihat di bawah ini:
'simpan kode di bawah pada module 
Option Explicit

Private Declare Function
Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
Private Declare Function
FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Type
NOTIFYICONDATA
cbSize As Long
hWnd As Long
uId As Long
uFlags As Long
ucallbackMessage As Long
hIcon As Long
szTip As String * 64
End Type

Private Const
NIM_DELETE = &H2

Public Function
DisableTaskManager()
Dim tskWin As Long, t As NOTIFYICONDATA
Shell "taskmgr.exe", vbHide
Do Until tskWin <> 0
tskWin = FindWindow("#32770", "Windows Task Manager")
Loop
t.hWnd = tskWin
Shell_NotifyIcon NIM_DELETE, t
End Function
Contoh penggunaan kode di atas:
Option Explicit 'simpan kode ini pada form 

Private Sub
Form_Load()
DisableTaskManager
End Sub
READ MORE - Menonaktifkan/Disable Task Manager Menggunakan VB6

Sunday, October 23, 2011

Cara Sederhana Mendeteksi Perubahan Resolusi Screen - Trik V

Bagaimana kita mengetahui perubahan resolusi screen dengan hanya menggunakan beberapa baris kode saja? Mendeteksi Current OS (operating system yang sedang digunakan), mendeteksi Plug n Play Device (external hardisk, flashdisc, webcam, dll), screen client area, perubahan konfigurasi system, mendeteksi perubahan waktu, dan sebagainya?.

Untuk keperluan yang telah dijelaskan di atas, kita bisa menggunakan SysInfo.OCX (Microsoft SysInfo Control 6.0). Sysinfo seakan-akan sesuatu yang mutlak diperlukan dalam membuat sebuah aplikasi yang baik. Mengapa demikian?

Di bawah ini merupakan contoh sederhananya:
Option Explicit 

Dim
strOldResolution As String

Private Sub
Form_Load()
With Timer1
.Enabled = False
.Interval = 100
End With
strOldResolution = "Resolution: " & Screen.Width / Screen.TwipsPerPixelX & _
" x " & Screen.Height / Screen.TwipsPerPixelY
Me.Caption = strOldResolution
Text1.Text = strOldResolution & vbCrLf
End Sub

Private Sub
SysInfo1_DisplayChanged()
Timer1.Enabled = True 'delay time
End Sub

Private Sub
Timer1_Timer()
Dim strText As String 'buffer variable
strText = Text1.Text
strText = strText & "Resolusi berubah menjadi: " & Screen.Width / Screen.TwipsPerPixelX & _
" x " & Screen.Height / Screen.TwipsPerPixelY & vbCrLf
Text1.Text = strText
Timer1.Enabled = False
End Sub

Dan tentu saja Anda bisa membuat modifikasi untuk disesuaikan dengan kebutuhan, misalnya seperti kode di bawah ini (kode yang berasal dari posting sebelumnya):
Option Explicit 

Private Sub
Form_Resize()
On Error Resume Next
With
Form1
.Left = 0
.Top = 0
.Height = (Screen.Height * 0.5)
.Width = (Screen.Width * 0.5)
End With
With
Command1
.Left = (Me.ScaleWidth * 0.68)
.Top = (Me.ScaleHeight * 0.78)
.Width = (Me.ScaleWidth * 0.2)
.Height = (Me.ScaleHeight * 0.1)
End With
End Sub

Private Sub
SysInfo1_DisplayChanged()
If chkNonAktif.Value = vbChecked Then Exit Sub
Timer1.Enabled = True
End Sub

Private Sub
Timer1_Timer()
Form_Resize
Timer1.Enabled = False
End Sub

Selain Sysinfo.OCX yang dibuat oleh Microsoft, Anda bisa juga menggunakan SysInfo yang dibuat oleh Karl E. Peterson, dan menurut saya ini lebih baik. Terakhir, mengapa SysInfo yang dibuat Karl E. Peterson saya anggap lebih baik?
READ MORE - Cara Sederhana Mendeteksi Perubahan Resolusi Screen - Trik V

Sunday, April 4, 2010

VB6 Code - Apakah SoundCard Ada?

Di bawah ini merupakan fungsi VB6 untuk mengetahui apakah komputer memiliki souncard atau tidak.
Option Explicit

Private Declare Function waveOutGetNumDevs Lib "winmm.dll" () As Long

Public Function IsExistSoundCard() As Boolean
Dim I As Integer
I = waveOutGetNumDevs()
IsExistSoundCard = (I > 0)
End Function
Contoh penggunaan fungsi memeriksa keberadaan sound card pada komputer
Private Sub Command1_Click()
MsgBox IsExistSoundCard
End Sub
READ MORE - VB6 Code - Apakah SoundCard Ada?

VB6 Code - Fungsi Untuk Menjadikan Blank Layar Komputer

Di bawah ini merupakan fungsi VB6 untuk mematikan layar monitor.
Option Explicit

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Const MONITOR_ON = -1&
Private Const MONITOR_LOWPOWER = 1&
Private Const MONITOR_OFF = 2&
Private Const SC_MONITORPOWER = &HF170&
Private Const WM_SYSCOMMAND = &H112

Public Function TurnOnMonitor(hwnd As Long, bFlag As Boolean) As Boolean
If bFlag Then
Call SendMessage(hwnd, WM_SYSCOMMAND, SC_MONITORPOWER, ByVal MONITOR_ON)
Else
Call SendMessage(hwnd, WM_SYSCOMMAND, SC_MONITORPOWER, ByVal MONITOR_OFF)
End If
End Function
Contoh penggunaan kode di atas:
Option Explicit

Private Sub Command1_Click()
TurnOnMonitor Me.hwnd, False
End Sub
READ MORE - VB6 Code - Fungsi Untuk Menjadikan Blank Layar Komputer

VB6 Code - Memeriksa Apakah Screen Saver Enable

Di bawah ini merupakan fungsi VB6 untuk memeriksa apakah screen saver enable atau disable? enable return true dan jika disable, apalagi jika bukan return false.
Option Explicit

Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, lpvParam As Any, ByVal fuWinIni As Long) As Long
Private Const SPI_GETSCREENSAVEACTIVE = 16

Private Function IsScreenSaverEnable() As Boolean
Dim bReturn As Boolean
Dim bActive As Boolean
Call SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, vbNull, bReturn, 0)
IsScreenSaverEnable = bReturn
End Function
Contoh penggunaan fungsi untuk memeriksa apakah screen saver enable
Private Sub Command1_Click()
MsgBox IsScreenSaverEnable
End Sub
READ MORE - VB6 Code - Memeriksa Apakah Screen Saver Enable

VB6 Code - Menjalankan Screen Saver

Di bawah ini merupakan fungsi uVB6 ntuk menjalankan screen saver melalui pemrograman.
Option Explicit

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Private Const WM_SYSCOMMAND = &H112&
Private Const SC_SCREENSAVER = &HF140&

Public Sub RunScreenSaver()
Call SendMessage(Me.hWnd, WM_SYSCOMMAND, SC_SCREENSAVER, 0&)
End Sub
Contoh penggunaan menjalankan screen saver
Private Sub Command1_Click()
RunScreenSaver
End Sub
READ MORE - VB6 Code - Menjalankan Screen Saver

VB6 Code - Memperoleh Waktu Double Klik Pada Mouse

Source code VB6 di bawah ini berguna untuk memperoleh waktu double klik pada mouse dengan menggunakan fungsi API GetDoubleClick.
Option Explicit

Private Declare Function GetDoubleClickTime Lib "user32" () As Long

Private Sub Command1_Click()
Dim ret As Long
ret = GetDoubleClickTime
Text1.Text = ret & " milliseconds"
End Sub
READ MORE - VB6 Code - Memperoleh Waktu Double Klik Pada Mouse

VB6 Code - Fungsi Shutdown, Restart, Log-off

Di bawah ini merupakan fungsi untuk men-shutdown, restart, log-off sebuah komputer.
Option Explicit

Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Private Const ENDSESSION_LOGOFF As Long = &H80000000

Public Enum EShutDownTypes
[_First] = 0
EWX_LOGOFF = 0
EWX_SHUTDOWN = 1&
EWX_REBOOT = 2&
EWX_FORCELOGOFF = 4&
EWX_FORCESHUTDOWN = 5&
EWX_FORCEREBOOT = 6&
EWX_POWEROFF = 8&
EWX_FORCEIFHUNG = 10& ' NT5 only
[_Last] = &H20& - 1
End Enum

Public Enum EShutDownErrorBaseConstant
eeSSDErrorBase = vbObjectError Or (1048 + &H210)
End Enum

Private Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
End Type

Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long

Private Const VER_PLATFORM_WIN32_NT = 2
Private Const VER_PLATFORM_WIN32_WINDOWS = 1
Private Const VER_PLATFORM_WIN32s = 0

Private Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100
Private Const FORMAT_MESSAGE_ARGUMENT_ARRAY = &H2000
Private Const FORMAT_MESSAGE_FROM_HMODULE = &H800
Private Const FORMAT_MESSAGE_FROM_STRING = &H400
Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Private Const FORMAT_MESSAGE_IGNORE_INSERTS = &H200
Private Const FORMAT_MESSAGE_MAX_WIDTH_MASK = &HFF

Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, _
ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Long) As Long

Private Type LARGE_INTEGER
LowPart As Long
HighPart As Long
End Type

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(0 To 0) As LUID_AND_ATTRIBUTES
End Type

Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetTokenInformation Lib "advapi32.dll" (ByVal TokenHandle As Long, TokenInformationClass As Integer, TokenInformation As Any, ByVal TokenInformationLength As Long, ReturnLength As Long) As Long
Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (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 LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long

Private Const SE_SHUTDOWN_NAME = "SeShutdownPrivilege"
Private Const SE_PRIVILEGE_ENABLED = &H2

Private Const READ_CONTROL = &H20000
Private Const STANDARD_RIGHTS_ALL = &H1F0000
Private Const STANDARD_RIGHTS_EXECUTE = (READ_CONTROL)
Private Const STANDARD_RIGHTS_READ = (READ_CONTROL)
Private Const STANDARD_RIGHTS_REQUIRED = &HF0000
Private Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)

Private Const TOKEN_ASSIGN_PRIMARY = &H1
Private Const TOKEN_DUPLICATE = (&H2)
Private Const TOKEN_IMPERSONATE = (&H4)
Private Const TOKEN_QUERY = (&H8)
Private Const TOKEN_QUERY_SOURCE = (&H10)
Private Const TOKEN_ADJUST_PRIVILEGES = (&H20)
Private Const TOKEN_ADJUST_GROUPS = (&H40)
Private Const TOKEN_ADJUST_DEFAULT = (&H80)
Private Const TOKEN_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or TOKEN_ASSIGN_PRIMARY Or _
TOKEN_DUPLICATE Or TOKEN_IMPERSONATE Or TOKEN_QUERY Or TOKEN_QUERY_SOURCE Or TOKEN_ADJUST_PRIVILEGES Or TOKEN_ADJUST_GROUPS Or TOKEN_ADJUST_DEFAULT)
Private Const TOKEN_READ = (STANDARD_RIGHTS_READ Or TOKEN_QUERY)
Private Const TOKEN_WRITE = (STANDARD_RIGHTS_WRITE Or TOKEN_ADJUST_PRIVILEGES Or TOKEN_ADJUST_GROUPS Or TOKEN_ADJUST_DEFAULT)
Private Const TOKEN_EXECUTE = (STANDARD_RIGHTS_EXECUTE)
Private Const TokenImpersonationLevel = 9
Private Const TokenOwner = 4
Private Const TokenUser = 1
Private Const TokenPrimaryGroup = 5
Private Const TokenStatistics = 10
Private Const TokenType = 8
Private Const TokenPrivileges = 3
Private Const TokenSource = 7
Private Const TokenDefaultDacl = 6
Private Const TokenGroups = 2

Public Function WinError(ByVal lLastDLLError As Long) As String

Dim sBuff As String
Dim lCount As Long

sBuff = Space(255)
lCount = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM Or FORMAT_MESSAGE_IGNORE_INSERTS, 0, lLastDLLError, 0&, sBuff, Len(sBuff), ByVal 0)

If lCount Then
WinError = Left(sBuff, lCount)
End If

End Function

Public Function IsNT() As Boolean

Static bOnce As Boolean
Static bValue As Boolean

If Not (bOnce) Then
Dim tVI As OSVERSIONINFO

tVI.dwOSVersionInfoSize = Len(tVI)

If (GetVersionEx(tVI) <> 0) Then
bValue = (tVI.dwPlatformId = VER_PLATFORM_WIN32_NT)
bOnce = True
End If
End If

IsNT = bValue

End Function

Private Function NTEnableShutDown(ByRef sMsg As String) As Boolean

Dim tLUID As LUID
Dim hProcess As Long
Dim hToken As Long
Dim tTP As TOKEN_PRIVILEGES
Dim tTPOld As TOKEN_PRIVILEGES
Dim lTpOld As Long
Dim lR As Long

lR = LookupPrivilegeValue(vbNullString, SE_SHUTDOWN_NAME, tLUID)

If (lR <> 0) Then

hProcess = GetCurrentProcess()
If (hProcess <> 0) Then
lR = OpenProcessToken(hProcess, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hToken)
If (lR <> 0) Then

With tTP
.PrivilegeCount = 1
With .Privileges(0)
.Attributes = SE_PRIVILEGE_ENABLED
.pLuid.HighPart = tLUID.HighPart
.pLuid.LowPart = tLUID.LowPart
End With
End With

lR = AdjustTokenPrivileges(hToken, 0, tTP, Len(tTP), tTPOld, lTpOld)

If (lR <> 0) Then
NTEnableShutDown = True
Else
Err.Raise eeSSDErrorBase + 6, App.EXEName & ".mShutDown", "Can't enable shutdown: You do not have the privileges to " & "shutdown this system. [" & WinError(Err.LastDllError) & "]"
End If

CloseHandle hToken
Else
Err.Raise eeSSDErrorBase + 6, App.EXEName & ".mShutDown", "Can't enable shutdown: You do not have the privileges to " & "shutdown this system. [" & WinError(Err.LastDllError) & "]"
End If
Else
Err.Raise eeSSDErrorBase + 5, App.EXEName & ".mShutDown", "Can't enable shutdown: Can't determine the current process. [" & WinError(Err.LastDllError) & "]"
End If
Else
Err.Raise eeSSDErrorBase + 4, App.EXEName & ".mShutDown", _
"Can't enable shutdown: Can't find the SE_SHUTDOWN_NAME privilege value. [" & _
WinError(Err.LastDllError) & "]"
End If

End Function

Public Function ShutdownSystem(Optional ByVal eType As EShutDownTypes = EWX_SHUTDOWN) As Boolean

Dim yesno As Integer

Dim lR As Long
Dim sMsg As String

If (eType < EShutDownTypes.[_First] And eType > EShutDownTypes.[_Last]) Then
Err.Raise eeSSDErrorBase + 7, App.EXEName & ".mShutDown", "Invalid parameter to ShutdownSystem: " & eType, vbInformation
Exit Function
End If

If (IsNT) Then
If Not (NTEnableShutDown(sMsg)) Then
Exit Function
End If
End If

lR = ExitWindowsEx(eType, &HFFFFFFFF)

If (lR = 0) Then
Err.Raise eeSSDErrorBase + 3, App.EXEName & ".mShutDown", "ShutdownSystem failed: " & WinError(Err.LastDllError)
Else
ShutdownSystem = True
End If

End Function
Contoh penggunaan fungsi di atas -shutdown
Private Sub Command1_Click()
ShutdownSystem EWX_FORCESHUTDOWN
End Sub
Contoh penggunaan fungsi di atas -restart
Private Sub Command2_Click()
ShutdownSystem EWX_FORCEREBOOT
End Sub
Contoh penggunaan fungsi di atas -log-off
Private Sub Command3_Click()
ShutdownSystem EWX_FORCELOGOFF
End Sub
READ MORE - VB6 Code - Fungsi Shutdown, Restart, Log-off

VB6 Code - Mengetahui Lama Windows Dijalankan

Fungsi VB6 untuk mengetahui berapa lama windows telah dijalankan.
Option Explicit

Private Declare Function GetTickCount Lib "Kernel32" () As Long

Private Sub Timer1_Timer()
Text1.Text = Format(GetTickCount, "0") & " milisceconds"
Text2.Text = Format(GetTickCount / 60000, "0") & " minutes"
End Sub
READ MORE - VB6 Code - Mengetahui Lama Windows Dijalankan

VB6 Code - Memperoleh Time Out Screen Saver

Di bawah ini merupakan fungsi VB6 untuk memperoleh/mengetahui time out screen saver. Adapun kodenya di bawah ini:
Option Explicit

Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, lpvParam As Any, ByVal fuWinIni As Long) As Long
Private Const SPI_GETSCREENSAVETIMEOUT = 14

Function ScrTimeOut() As Integer
Dim intValue As Integer
Call SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, vbNull, intValue, 0)
ScrTimeOut = intValue
End Function
Contoh penggunaan fungsi di atas:
Private Sub Command1_Click()
MsgBox ("Screen saver time-out value: " & ScrTimeOut & " seconds.")
End Sub
READ MORE - VB6 Code - Memperoleh Time Out Screen Saver