Setting service priority - How to?

 I have my own application which is running as service.
  I set its priority as IDLE using this statement.

  SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);

  I am using <u>apache</u> as one of the <u>service</u> used by my application.
  I want to set apache's priority as IDLE_PRIORITY_CLASS.

  How to do this? I can't run this through the batch file or using start command. I am starting these services when Installing my application.

  In some the thread, somebody told that Micro$oft doen't support to change the priority of a service.
  But using 's Process Explorer, It can be set the priority for services also.

  So, it is possible to change the prority of a service programatically.

  How to do this?

  Any type of Ideas will be appreciated.
Who is Participating?
Svetlin_PanayotovConnect With a Mentor Commented:
I can think of two possible solutions - the easier - if you're starting Apache with OpenProcess - you have the handle - and don't need to search for the process.If you don't have the handle - well before you start your instance of Apache - do a similar search and keep the PIDs for all instances of Apache you find.The PID actually is the th32ProcessID field in PROCESSENTRY32 struct.Finding your instance then is easy - it's the one that does not match any of the PIDs above.
Hope this'll work for you.

Try to open this service process using OpenProcess with PROCESS_SET_INFORMATION flag, and change it's priority.
AlexFM is right but when setting the priority for another process - you must find it first.So you'll have something like:

#include <TlHelp32.h>

HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 pe  = {0};
pe.dwSize = sizeof(PROCESSENTRY32);
if (!Process32First(hSnapshot, &pe))
return FALSE;
//find your process
//and set its priority by calling OpenProcess and SetPriorityClass
while (Process32Next(hSnapshot, &pe));

Also for more info - there's sample in Platform SDK- just look at the help for CreateToolhelp32Snapshot


Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

aravindtjAuthor Commented:
Hi AlexFM and Svetlin Panayotov,
 Thanks for your reply.
 But, I am starting the application by OpenProcess method.

 I am starting a application, apache.exe as a service in my application.
I am using creating the service by CreateService method by giving the propreties
All this is made at my product installation process.
But, I need to run it as a service which is starting at boot time.
I am using StartService method to start that service.
Now, how can I set the priority?

Thanks in advance.
So this should only make it easier - just use the handle returned from OpenProcess and pass it to

aravindtjAuthor Commented:
Hi Svetlin_Panayotov,
 Thanks for reply.
 I am running the Apache as a service. There are some other Apache server is running on other ports.
 Bow, when I am listing the processes, there are 2 Apache services are started. Now, How can I get the Apache service I am running for my own purpose?

 Thanks in advance.
All Courses

From novice to tech pro — start learning today.