Change The Priority Of Any Running Process

With this code, you can change the priority of any process running in windows.
You can increase or decrease your application process priority, or other application priority.

Module Code

Option Explicit

Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Declare Function OpenProcess Lib "kernel32" _
  (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
  ByVal dwProcessId As Long) As Long

Declare Function GetWindowThreadProcessId Lib "user32" _
  (ByVal hwnd As Long, lpdwProcessId As Long) As Long

Declare Function SetPriorityClass Lib _
    "kernel32" (ByVal hProcess As Long, ByVal dwPriorityClass As Long) As Long

Public Const REALTIME_PRIORITY_CLASS = &H100
Public Const HIGH_PRIORITY_CLASS = &H80
Public Const NORMAL_PRIORITY_CLASS = &H20
Public Const IDLE_PRIORITY_CLASS = &H40

Form Code

Private Sub Form_Load()
Dim lRet As Long
Dim
lProcessID As Long
Dim
lProcessHandle As Long

'You can replace me.hwnd with the handle of the
'window that you want to change its priority

GetWindowThreadProcessId Me.hwnd, lProcessID

'Get the process handled
lProcessHandle = OpenProcess(0, False, lProcessID)

'Sets the priority
'use any priority from the following (from the highest priority to the lowest):
'REALTIME_PRIORITY_CLASS
'HIGH_PRIORITY_CLASS
'NORMAL_PRIORITY_CLASS
'IDLE_PRIORITY_CLASS

'if lRet <> 0 then the changing operation action was successful

lRet = SetPriorityClass(lProcessHandle, HIGH_PRIORITY_CLASS)

'Close the handle so system retains
'accurate count of open handles
'to process.  Returns non-zero if successful

lRet = CloseHandle(lProcessHandle)

End Sub

Go Back