How to Change the Priority of any Running Process
Posted on January 5, 2009
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | 'System & API - How to Change the Priority of any Running Process 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 Private Sub Form_Load() Dim lRet As Long, lProcessID As Long, 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 |