views:

707

answers:

2

Hello all,

In my service I have a few threads that each call the CreateProcess() function to launch an external application. I would like to adjust thread (or process) priorities to normal or lower, depending on some other factors. The problem is that SetThreadPriority() function fails with an error 6 (invalid handle?). I'm passing in a handle obtained from PROCESS_INFORMATION::hThread (after calling the CreateProcess() of course).

I've also tried setting the priority on the processes using the SetPriorityClass() function, which also fails.

Is there something special that I need to do to set priorities in a service? The service is logged on as a local user.

Thanks in advance for your thoughts.

A: 

If the processes you are launching are windowed applications you may need to have the services "Allow services to interact with desktop" checkbox selected (look in the services properties dialog, "Log On" tab).

Osseta
Well, they are console apps, so not sure whether they're windowed or not? I'm launching them with SW_HIDE wShowWindow value. I'll try that flag just in case though.
dennisV
Oops, forgot to add - the service is running as a local user (me), so can't use that checkbox.
dennisV
Does your local user have sufficeint permissions to change the priority? If this is Vista you may need a UAC manifest in the exe so the service is started with the actual permission required.
Osseta
Yes, I'm running as admin (Vista, yes), but the service is not launched with admin rights. I don't think a service can have a manifest like a normal program to request admin rights, can it? I mean it wouldn't be able to popup the UAC prompt, so how would it get the rights then?
dennisV
Why would the "Allow processes to interact with the desktop" setting have anything to do with being able to set the thread priority?
1800 INFORMATION
+2  A: 

Maybe you don't have the correct access rights? MSDN on SetThreadPriority says:

hThread [in] A handle to the thread whose priority value is to be set.

The handle must have the THREAD_SET_INFORMATION or THREAD_SET_LIMITED_INFORMATION access right. For more information, see Thread Security and Access Rights.

Windows Server 2003 and Windows XP/2000: The handle must have the THREAD_SET_INFORMATION access right.

1800 INFORMATION
It's possible. I'm trying to pass in a SECURITY_DESCRIPTOR to the CreateProcess() in hopes that I can get full access, but it doesn't appear to have any effect, as I'm still getting an error 6 and priorities are not changed. I may be doing something wrong in setting this though.
dennisV
Yep, that was it. Thanks!
dennisV