0

I'm trying to create a windows service in which a process should start. This process needs to be logged into another account.

    protected override void OnStart(string[] args)
        {
            _thread.Start();
        }

    private void ThreadFunction()
    {
        var process = new Process
                          {
                                  StartInfo =
                                      {
                                          UserName = "User",
                                          Password = "Pass",
                                          UseShellExecute = false,
                                          FileName = @"C:\Program Files\Default Company Name\ServiceProcessInstaller\ConsoleProcess.exe"
                                      }
                              };
        process.Start();
    }

I thought this to be rather simple, but it seems like process.start and windows service are like oil and water.

When I start the process nothing happens and eventually an exception i given: Windows could not start the "AServiceProcess" service on Local Computer. Error 1067: The process terminated unexpectedly.

I would really appreciate all the help I can get and if anything isn't clear enough let me know.

  • You should definitely try-catch and log issues somewhere. – Wiktor Zychla Aug 14 '12 at 10:27
  • Possible duplicate of http://stackoverflow.com/questions/1454502/how-can-i-restart-a-windows-service-programatically-in-net – Ivan Golović Aug 14 '12 at 10:44
  • After logging the exception: System.ComponentModel.Win32Exception (0x80004005): Access is denied at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo) at System.Diagnostics.Process.Start() at ServiceProcess.Service1.ThreadFunction() –  Aug 14 '12 at 11:29
  • Why are you trying to run service using `Process` class? `ServiceController` class and it's methods are used for that. – Ivan Golović Aug 14 '12 at 13:49

2 Answers2

0

I would suggest you take a step back and review some of the conceptual info regarding Windows service processes. A service is just a process that starts and runs outside of any logged on user, in much the same way conceptually as a Unix daemon process runs. You said your code represents your service that you want to use to start another process, but the Start method you show above is starting something called "ServiceProcess.exe," which looks very confusing - as if that's the real service you intend to run.

The service process requires one set of credentials, and the credentials provided (or used implicitly) appear (based on your information) not to have the permissions necessary to start external processes.

Unless there's a very unusual requirement in play here, I would suggest you consider - if possible - implementing the actions you need within the service process itself. You may well be able to make the structure above work, but I'm not sure you would find it maintainable over the long term.

David W
  • 10,062
  • 34
  • 60
  • You make a valid point. My original problem is that I need to logon to an account because this account has access to a network drive. And i'm pretty sure the SYSTEM account won't be able to access this. Can one solve this by logging into the service? –  Aug 15 '12 at 08:50
  • You can configure a Windows Service to use any credential you want (and for which you have the password :) ). You are correct to say that the SYSTEM account, which is local to the machine, won't have network access. The account credential you use needs to have "Log on as a Service" right. – David W Aug 15 '12 at 10:46
0

I ended up solving the problem by using the function 'CreateUserAsProcess', with a WinNT50 and New Credentials setup.