0

I am creating a service to manipulate mouse movement and I need my application to be able to communicate with the service to tell it how to move the mouse.

side note, I am doing this in a service because I believe that will also allow it to work on elevated applications as using SendInputs from a normal desktop app will not work when a elevated app has focus.

I've done some prelim research and seen people say named pipes is the best way to communicate with a service, but will this be able to handle ~500 messages per second (I want the mouse movement to be as fluid as possible)?

I have some basic experience with named pipes but nothing that handles such rapid data.

Below it the basic code that I use in other areas of the application; will this work in the service without needing any modifications? is parsing the data as a string then deserializing my data out of that string the most efficient way to go, in my experience working with string can slow things down at this speed.

    NamedPipeServerStream s = new NamedPipeServerStream("MyPipe", PipeDirection.In, 2);
    Action<NamedPipeServerStream> a = callBack;
    a.BeginInvoke(s, ar => { }, null);

    private void callBack(NamedPipeServerStream pipe)
    {
        while (!SharedProperties.IsClosing)
        {
            pipe.WaitForConnection();
            StreamReader sr = new StreamReader(pipe);
            string message = sr.ReadToEnd();
            processPipeMessage(message);
            pipe.Disconnect();
        }
    }
Wobbles
  • 3,033
  • 1
  • 25
  • 51
  • 1
    This won't work because Windows Services cannot move the mouse: they run in a different desktop session and (by default) run under a different user account that doesn't have permission to access other sessions. – Dai Dec 04 '18 at 21:10
  • To confim, by "service" you mean a Windows Service (aka Daemon), and not a service in the hub-sand-spoke architecture? – Dai Dec 04 '18 at 21:11
  • @Dai I cannot call user32.dll SendInput from a service? – Wobbles Dec 04 '18 at 21:11
  • @Dai correct, windows service – Wobbles Dec 04 '18 at 21:12
  • Correct, this person a few years ago didn't have any success either: https://stackoverflow.com/questions/32474969/c-sendinput-possible-in-windows-service – Dai Dec 04 '18 at 21:13
  • @Dai but I am reading that if the service is run on the system service account with "interact with desktop" enabled it should work, ref: https://www.experts-exchange.com/questions/20796151/Using-PostMessage-and-SendInput-in-a-Service.html – Wobbles Dec 04 '18 at 21:16
  • That ExpertSexChange thread is from 2003 before UAC and Session restrictions (and numerous other necessary security changes) were added in Windows Vista. The way to do it now would be to have *another* process that runs in the desktop context of the logged-in user that communicates with the service through another pipe (or local TCP loopback connection) which then moves the mouse. Why do you want to use a Service in the first place? – Dai Dec 04 '18 at 21:17
  • @Dai crap.... How do remote desktop and multi computer single kb/m apps handle this then? I alwayse assumed it was by a service, – Wobbles Dec 04 '18 at 21:19
  • @Dai to allow mouse control in elevated programs, lock scree, login screen etc without the user having to explicitly elevate my app every time. – Wobbles Dec 04 '18 at 21:20
  • Remote Desktop is a feature of the Windows kernel and is hard-baked into the operating system. The "Terminal Services" / "Remote Desktop Services" entry in the Services MMC is only a small part of the whole system. – Dai Dec 04 '18 at 21:34
  • @Dai so other applications that achieve this are just leveraging RDP??? I dont know, I've had programs like team viewer work when RDP was disabled by GPO – Wobbles Dec 05 '18 at 13:38
  • No, other applications like TeamViewer and VNC don't use Windows' RDP infrastructure, but they also don't work by using a Windows Service either (TeamViewer does install a service, but it isn't needed to run TeamViewer (as a client or server) at all. – Dai Dec 05 '18 at 22:33

0 Answers0