8

Can anyone provide a Delphi example of code that changes the priority class of a process?

I need to get the process by name from the Windows XP Task manager and change its priority using delphi code.

NobodyNada
  • 7,529
  • 6
  • 44
  • 51
mortalis
  • 2,060
  • 24
  • 34

1 Answers1

12

you must use the SetPriorityClass function.

This function is part of the windows unit, this is a sample

SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
RRUZ
  • 134,889
  • 20
  • 356
  • 483
  • Wow, I never heard about this possibility, +10 if I can :) – TLama Dec 19 '11 at 21:28
  • 1
    @TLama, altering the priority of a process is a bad idea without an extremely good reason, as it can delay (or kill) OS functionality. Rodrigo didn't point that out, but I'm sure he knew that as well. :) Rodrigo, +1. – Ken White Dec 20 '11 at 00:42
  • ok. tnx. But if i have in the Task manager (Windows) process "prog.exe" how do i change the priority of this process using this function? – mortalis Dec 20 '11 at 06:40
  • or i have to use other method? – mortalis Dec 20 '11 at 06:40
  • You can refer to John Ayres book entitled **The Tomes of Delphi Win32 Core API** (ISBN 1-55622-750-7). It's a must. – menjaraz Dec 20 '11 at 11:16
  • @St.Anger_Roman, to use this function with a file name , you must take an Snapshot of the Running Processes using the [CreateToolhelp32Snapshot](http://msdn.microsoft.com/en-us/library/windows/desktop/ms682489%28v=vs.85%29.aspx) function, then you must search for the name of the process which you want to change and finally open the process to get a handle (this is the handle which you must pass to the `SetPriorityClass` function) using the [OpenProcess](http://msdn.microsoft.com/en-us/library/windows/desktop/ms684320%28v=vs.85%29.aspx) passing the `PROCESS_SET_INFORMATION` value as parameter. – RRUZ Dec 20 '11 at 11:40