Problem in launching events and processes from a Windows service

0

I am developing a project in C # in which there is a Windows service ( Windows Service Applications ) whose function is to generate keyboard, mouse events and launch processes.

The service itself can be installed and run without problems, but when you try to launch a process or generate an event, it does not.

Out of the service, all kinds of processes and events can be launched.

When installed, the LocalSystem account is established, which, in theory, gives system permissions.

Here is the code of the OnStart method, which should launch the iExplore process with a google window:

    ///<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." );


        //
        // Esta es la sentencia que debería crear el proceso de iExplore.
        // Al ejecutarla fuera del servicio si funciona, además, el servicio
        // sin esta sentencia funciona a la perfección.
        // NOTA: Esta no es la funcionalidad real, pero bueno, tampoco
        // funciona.
        //
        Process.Start(@"C:\Program Files\Internet Explorer\iexplore.exe", "www.google.es");

        // 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 );
    }

For sending events, use SendInput () ( SendInput function ) and the case is the same as when launching processes.

    
asked by JOverlord 04.08.2017 в 13:19
source

1 answer

0

If it is a problem that you need administrator permission, what you have to do is:

Go to your solution and Click derecho en el proyecto-> agregar-> Nuevo Elemento A window will appear and there you will have to find Archivo de manifiesto de aplicación you add it to your project and this will generate a file called app.manifest with predefined data.

You open this file and you will have to modify the requestedPrivileges section in order to be granted administrator permissions.

The section should look like this:

<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">        
    <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>

EDIT:
Right click on the project to add a new element

We are looking for the app.Manifest

We modified the new file so that it remains the same as here

    
answered by 04.08.2017 в 14:04