5

I am currently writing an application which uses lowlevel mouse hooks. Because I have to start the application with the system with administrator privileges I wanted to create a service(see here: Is this a practical use of a service?). Now I've just found out that a service is not able use hook mouse-hooks. So I need a new concept but I really don't know where to start. I've already tried it with a simple process but I was not able to use the task scheduler to run it as administrator on system start.

So what would recommend to use? Should I still use a Windows Service to start a process? Isn't that a bit overkill?

EDIT: I've just tried to start my process which performs the hook out of the service. The process starts but it seems like, it behaves the same as a simple service behaves. I can install the hook but the callback does not gets called. I am starting the process with this code:

STARTUPINFO info={sizeof(info)};
PROCESS_INFORMATION processInfo;
if (!CreateProcess(path, NULL, NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo))
{
    std::wstring msg(L"Could not start \"");
    msg.append(path);
    msg.append(L"\". CreateProcess");
    WriteErrorLogEntry((PWSTR)msg.c_str());
    return;
}

How can I solve my problem? The process has to run with administrator rights but needs another context?

Community
  • 1
  • 1
Florian
  • 5,918
  • 3
  • 47
  • 86
  • 1
    It's only overkill if there is not a simpler way to do it - which I can't think of. If you have a service that spawns a process then I think that's fine. Just make sure that the service control signals (stop/start/pause etc) are passed to the process too. In this way you have a logical service, that's actually split across two physical processes. – Preet Sangha Aug 05 '13 at 22:15
  • @PreetSangha I think your comment is good enough to be an answer. – Geeky Guy Aug 05 '13 at 22:19
  • And just don't forget that there are multiple user accounts may be active simultaneously (Windows XP with "fast switch" feature, and any server version of Windows that allows even two admins to login simulteneously) – Yury Schkatula Aug 05 '13 at 22:29
  • @Renan - thanks - please feel to copy/paste to an answer if you like. – Preet Sangha Aug 05 '13 at 22:56
  • what exactly do you mean with "fast switch" feature? @YurySchkatula – Florian Aug 06 '13 at 17:34
  • @thefiloe - it means that multiple users can share the same PC simultaneously without logging off (so all the apps launched by each logged-in user are still running) http://en.wikipedia.org/wiki/Fast_user_switching – Yury Schkatula Aug 07 '13 at 13:12
  • 2
    The correct solution is to split your functionality. Implement the mouse-hook in a process running in the user's session (there are lots of ways to launch such a process when a user logs in) but implement the parts that require administrative access in a service. – Harry Johnston Aug 08 '13 at 00:57
  • Just edited my answer @HarryJohnston – Florian Aug 08 '13 at 16:44
  • Starting a subprocess from the service won't make any difference. I'm not sure where you got that idea from. You need to launch a copy of the hooking process *in each end user's session*. That *can* be done from a service (via `CreateProcessAsUser` and friends) but it is much easier to use one of many ways of making a process run when a user logs in, e.g., the Run registry key. The user's process can then contact the service. – Harry Johnston Aug 09 '13 at 02:51

2 Answers2

0

My guess is mouse input is per-session, so I'm going to guess that you're not getting the behavior you want because your process is not running in the WindowsStation that you want.

DougN
  • 4,407
  • 11
  • 56
  • 81
-1

I think you might be able to use the Task Scheduler to start a program with administrator privileges.

Here's a website demonstrating how to create a shortcut that runs with Administrative privileges with no UAC prompts, it uses the Task Scheduler as its way of launching the program.

http://www.howtogeek.com/howto/windows-vista/create-administrator-mode-shortcuts-without-uac-prompts-in-windows-vista/

If you don't want to invoke it manually, you can still use the Task Scheduler's usual features to start the program automatically.

Dwedit
  • 618
  • 5
  • 11
  • well. as i told you before. i've already tried that in many differnt ways. but it often das not work as expected. – Florian Aug 06 '13 at 17:33