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.