send events from a windows service

时间:2017-08-05 12:31:35

标签: c# .net windows-services windows-10 keyboard-events

I am developing a project in C# to control a device. For that, I am using a Windows Service (Windows Service Applications) that reads the input from the service and then sends the resulting output using SendInput() (SendInput function).

The service itself is already working, but it isn't able to generate any kind of event.

This is the code of the OnStart() function of the service:

   ///<summary>
    /// Executed at reception of Start command.
    ///</summary>
    ///<param name="args">
    /// It must contain the source and name used for the debug log, or be empty. 
    /// The source and log must be already registered, otherwise a write attempt would fail and leave the service in an undefined state.
    ///</param>
    protected override void OnStart( string[] args ) {
        // Update the service state to Start Pending
        ServiceStatus serviceStatus = new ServiceStatus() {
            dwServiceType = ServiceConstants.serviceType,
            dwCurrentState = ServiceState.SERVICE_START_PENDING,
            dwWaitHint = ServiceConstants.startAndStopLatency,
            dwControlsAccepted = ServiceControls.SERVICE_ACCEPT_STOP
        };
        SetServiceStatus( this.ServiceHandle, ref serviceStatus );

        // We use the default source and log names
        string logSourceName = LogConstants.logSourceName;
        string logName = LogConstants.logName;

        // If the arguments define an existent source and log, we actualize our variables
        if( args.Count() > 0 && System.Diagnostics.EventLog.SourceExists( args[0] ) ) logSourceName = args[0];
        if( args.Count() > 1 && System.Diagnostics.EventLog.Exists( args[1] ) ) logName = args[1];

        debugLog.Source = logSourceName;
        debugLog.Log = logName;

        debugLog.WriteEntry( "In OnStart." );

        //This is the part of the code that should generate an "H" key
        //pressing.
        //It works out of the service.
        //InputFactory.getKeyDown() and InputFactory.getKeyUp() generate the
        //corresponding INPUT struct to press and release a key.
        try {
            EventLib.INPUT i1 = InputFactory.getKeyDownINPUT( EventLib.ScanCodeShort.KEY_H );
            EventLib.INPUT i2 = InputFactory.getKeyUpINPUT( EventLib.ScanCodeShort.KEY_H );
            EventLib.INPUT[] ii1 = new EventLib.INPUT[] { i1 };
            EventLib.INPUT[] ii2 = new EventLib.INPUT[] { i2 };
            EventLib.SendInput( 1, ii1, EventLib.INPUT.Size );
            EventLib.SendInput( 1, ii2, EventLib.INPUT.Size );
        } catch( InvalidOperationException ioe ) {
            debugLog.WriteEntry( "Error: " + ioe.Message );
        } catch( System.ComponentModel.Win32Exception w32e ) {
            debugLog.WriteEntry( "Error: " + w32e.Message );
        } catch( System.IO.FileNotFoundException fnfe ) {
            debugLog.WriteEntry( "Error: " + fnfe.Message );
        }

        // Update the service state to Running
        serviceStatus.dwCurrentState = ServiceState.SERVICE_RUNNING;
        serviceStatus.dwControlsAccepted = ServiceControls.SERVICE_ACCEPT_STOP | ServiceControls.SERVICE_ACCEPT_PAUSE_CONTINUE;
        SetServiceStatus( this.ServiceHandle, ref serviceStatus );
    }

The service runs in the LocalSystem account.

0 个答案:

没有答案