This code finds a process then changes its priority. It only works in windows 2000 / XP. Changing the priority can have some bad consequences - it may even cause the process to crash!

Enjoy

VB Code:
  1. Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
  2. Private Declare Function SetPriorityClass Lib "kernel32" (ByVal hProcess As Long, ByVal dwPriorityClass As Long) As Long
  3. Private Declare Function GetPriorityClass Lib "kernel32" (ByVal hProcess As Long) As Long
  4. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  5. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
  6. Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
  7. Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
  8. Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
  9.  
  10. Private Const PROCESS_QUERY_INFORMATION = &H400
  11. Private Const TH32CS_SNAPPROCESS As Long = 2&
  12. Private Const PROCESS_ALL_ACCESS = 0
  13. Private Const PROCESS_SET_INFORMATION = &H200
  14.  
  15. Private Const IDLE_PRIORITY_CLASS = &H40
  16. Private Const BELOW_NORMAL_PRIORITY_CLASS = &H4000
  17. Private Const NORMAL_PRIORITY_CLASS = &H20
  18. Private Const ABOVE_NORMAL_PRIORITY_CLASS = &H8000
  19. Private Const HIGH_PRIORITY_CLASS = &H80
  20. Private Const REALTIME_PRIORITY_CLASS = &H100 'please don't use this
  21.  
  22. Private Const MAX_PATH& = 260
  23.  
  24. Private Type PROCESSENTRY32
  25.   dwSize As Long
  26.   cntUsage As Long
  27.   th32ProcessID As Long
  28.   th32DefaultHeapID As Long
  29.   th32ModuleID As Long
  30.   cntThreads As Long
  31.   th32ParentProcessID As Long
  32.   pcPriClassBase As Long
  33.   dwFlags As Long
  34.   szexeFile As String * MAX_PATH
  35. End Type
  36.  
  37. Function ChangePriority(sProcessName As String, lngPriority As Long) As Boolean
  38.  
  39.     On Error GoTo errHnd
  40.     Dim hProcess As Long
  41.     Dim iRet As Long
  42.  
  43.     'Find the process we want to change priority
  44.     hProcess = FindProcessId(sProcessName)
  45.     If hProcess <> 0 Then
  46.         ' Get the current Priority
  47.         iRet = GetPriorityClass(hProcess)
  48.         'If the priority is different then change
  49.    
  50.         If iRet <> lngPriority Then
  51.             iRet = SetPriorityClass(hProcess, lngPriority)
  52.             ChangePriority = (iRet <> 0)
  53.         Else
  54.             ChangePriority = True
  55.         End If
  56.     End If
  57.    
  58.     Call CloseHandle(hProcess)
  59.    
  60.     Exit Function
  61. errHnd:
  62.     ChangePriority = False
  63. End Function
  64.  
  65. Public Function FindProcessId(sProcessName As String) As Long
  66.     Dim uProcess As PROCESSENTRY32
  67.     Dim rProcessFound As Long
  68.     Dim hSnapshot As Long
  69.     Dim szExename As String
  70.     Dim hProcess As Long
  71.     On Local Error GoTo errHnd
  72.    
  73.     uProcess.dwSize = Len(uProcess)
  74.     hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
  75.     rProcessFound = ProcessFirst(hSnapshot, uProcess)
  76.     'loop around all processes until found
  77.    
  78.     Do While rProcessFound
  79.         szExename = StripNulls(uProcess.szexeFile)
  80.         If LCase$(szExename) = LCase$(sProcessName) Then
  81.             hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_SET_INFORMATION, False, uProcess.th32ProcessID)
  82.    
  83.             FindProcessId = hProcess
  84.             Call CloseHandle(hSnapshot)
  85.                
  86.             Exit Function
  87.         End If
  88.         rProcessFound = ProcessNext(hSnapshot, uProcess)
  89.     Loop
  90.          
  91.     Call CloseHandle(hSnapshot)
  92.     Exit Function
  93.    
  94. errHnd:
  95.     FindProcessId = 0
  96. End Function
  97. Private Function StripNulls(s As String) As String
  98.     Dim i As Long
  99.     i = InStr(1, s, Chr$(0))
  100.     StripNulls = Trim$(Left$(s, i - 1))
  101. End Function
  102. Private Sub Form_Load()
  103.     'load notepad and set its priority to be low
  104.     Shell "notepad.exe"
  105.    
  106.     ChangePriority "notepad.exe", IDLE_PRIORITY_CLASS
  107.  
  108. End Sub