none
How run client application as a windows service in C#?

    Question

  • Hi,

    I have a WCF web service and a client project. The client application has no UI. It has connection with the service. It contains only four classes. One class is main class all the other classes have been called in the main class. I want to run the client as a service. So I created one window service project, added all the four classes in "App_Code" folder, also added service reference, also added project installer class. I specified the StartType property of SelfHostedService as "Automatic" and also I gave the name of the service in Service name property. In WindowsService.cs file I have these codings.

     

    public partial class WindowsService : ServiceBase
    {
    public ServiceHost ClientInfoService = null;
    public WindowsService()
    {
    InitializeComponent();
    ServiceName =
    "ClientService";
    }

    protected override void OnStart(string[] args)
    {
    //// TODO: Add code here to start your service.
    try
    {
    Process process = new Process();
    process.StartInfo.FileName =
    AppDomain.CurrentDomain.BaseDirectory + "MyClient.exe";
    process.Start();
    }
    catch //Do Nothing
    {
    }
    } 
     

    protected override void OnStop()
    {
    // TODO: Add code here to perform any tear-down necessary to stop your service.
    if (ClientInfoService != null)
    {
    ClientInfoService.Close();
    ClientInfoService =
    null;
    }
    }
    }

    When I Install the client installer I am Showing simple UI for configure the port number, Server machine name. In that UI I have one button like "Start". I am starting the serivce through this button click event. Below are the codes present in under this event. 

    ServiceController controller = new ServiceController();
    controller.MachineName =
    ".";
    controller.ServiceName = "MyClient";
    string status = controller.Status.ToString();
    if (btnStart.Text == "Start")
    {
     
    if (status != "Stopped")
    {
    controller.Stop();
    }
    System.Threading.
    Thread.Sleep(5000);
    controller.Start();
    System.Threading.
    Thread.Sleep(500);
    MessageBox.Show(this, "Started", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Information);
    btnStart.Text =
    "Stop";
    } 
     

    else

    { 

    if (status != "Stopped" 

    Thread.Sleep(500);  

    MessageBoxEx.Show(this, "Stopped", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Information);

     

    else

    {

    controller.Start();

    System.Threading.

    MessageBoxEx.Show(this, "stopped", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Information); 

    Thread.Sleep(500); 
     controller.Stop();
    System.Threading.Thread.Sleep(500);  

     Some times Service is starting successfully and some times it is throwing an exception like "Service cannot be started. The service process could
    not connect to the service controller
    For more information, see Help and Support Center at http://go.microsoft.com/fwlink/events.asp." The codings are executed even the MyClient.exe is running also.Can you anyone help me how can i run the client as a windows service successfully?

    Thanks in Advance,
    Jayahar

     

     

    Tuesday, July 07, 2009 12:50 PM

Answers

  • Windows services run under the "System" user.  You are not going to be able to see any UI from a windows service.

    Now there is a workaround to start a process under a specific user, but you have to know the username/password:

    [StructLayout(LayoutKind.Sequential)]
       internal struct PROCESS_INFORMATION
       {
           public IntPtr hProcess;
           public IntPtr hThread;
           public uint dwProcessId;
           public uint dwThreadId;
       }



       [StructLayout(LayoutKind.Sequential)]
       internal struct SECURITY_ATTRIBUTES
       {
           public uint nLength;
           public IntPtr lpSecurityDescriptor;
           public bool bInheritHandle;
       }


       [StructLayout(LayoutKind.Sequential)]
       public struct STARTUPINFO
       {
           public uint cb;
           public string lpReserved;
           public string lpDesktop;
           public string lpTitle;
           public uint dwX;
           public uint dwY;
           public uint dwXSize;
           public uint dwYSize;
           public uint dwXCountChars;
           public uint dwYCountChars;
           public uint dwFillAttribute;
           public uint dwFlags;
           public short wShowWindow;
           public short cbReserved2;
           public IntPtr lpReserved2;
           public IntPtr hStdInput;
           public IntPtr hStdOutput;
           public IntPtr hStdError;

       }

       internal enum SECURITY_IMPERSONATION_LEVEL
       {
           SecurityAnonymous,
           SecurityIdentification,
           SecurityImpersonation,
           SecurityDelegation
       }

       internal enum TOKEN_TYPE
       {
           TokenPrimary = 1,
           TokenImpersonation
       }

       public class ProcessAsUser
       {

           [DllImport("advapi32.dll", SetLastError = true)]
           private static extern bool CreateProcessAsUser(
               IntPtr hToken,
               string lpApplicationName,
               string lpCommandLine,
               ref SECURITY_ATTRIBUTES lpProcessAttributes,
               ref SECURITY_ATTRIBUTES lpThreadAttributes,
               bool bInheritHandles,
               uint dwCreationFlags,
               IntPtr lpEnvironment,
               string lpCurrentDirectory,
               ref STARTUPINFO lpStartupInfo,
               out PROCESS_INFORMATION lpProcessInformation);


           [DllImport("advapi32.dll", EntryPoint = "DuplicateTokenEx", SetLastError = true)]
           private static extern bool DuplicateTokenEx(
               IntPtr hExistingToken,
               uint dwDesiredAccess,
               ref SECURITY_ATTRIBUTES lpThreadAttributes,
               Int32 ImpersonationLevel,
               Int32 dwTokenType,
               ref IntPtr phNewToken);


           [DllImport("advapi32.dll", SetLastError = true)]
           private static extern bool OpenProcessToken(
               IntPtr ProcessHandle,
               UInt32 DesiredAccess,
               ref IntPtr TokenHandle);

           [DllImport("userenv.dll", SetLastError = true)]
           private static extern bool CreateEnvironmentBlock(
                   ref IntPtr lpEnvironment,
                   IntPtr hToken,
                   bool bInherit);


           [DllImport("userenv.dll", SetLastError = true)]
           private static extern bool DestroyEnvironmentBlock(
                   IntPtr lpEnvironment);

           [DllImport("kernel32.dll", SetLastError = true)]
           private static extern bool CloseHandle(
               IntPtr hObject);

           private const short SW_SHOW = 5;
           private const uint TOKEN_QUERY = 0x0008;
           private const uint TOKEN_DUPLICATE = 0x0002;
           private const uint TOKEN_ASSIGN_PRIMARY = 0x0001;
           private const int GENERIC_ALL_ACCESS = 0x10000000;
           private const int STARTF_USESHOWWINDOW = 0x00000001;
           private const int STARTF_FORCEONFEEDBACK = 0x00000040;
           private const uint CREATE_UNICODE_ENVIRONMENT = 0x00000400;


           private static bool LaunchProcessAsUser(string cmdLine, IntPtr token, IntPtr envBlock)
           {
               bool result = false;


               PROCESS_INFORMATION pi = new PROCESS_INFORMATION();
               SECURITY_ATTRIBUTES saProcess = new SECURITY_ATTRIBUTES();
               SECURITY_ATTRIBUTES saThread = new SECURITY_ATTRIBUTES();
               saProcess.nLength = (uint)Marshal.SizeOf(saProcess);
               saThread.nLength = (uint)Marshal.SizeOf(saThread);

               STARTUPINFO si = new STARTUPINFO();
               si.cb = (uint)Marshal.SizeOf(si);


               //if this member is NULL, the new process inherits the desktop
               //and window station of its parent process. If this member is
               //an empty string, the process does not inherit the desktop and
               //window station of its parent process; instead, the system
               //determines if a new desktop and window station need to be created.
               //If the impersonated user already has a desktop, the system uses the
               //existing desktop.

               si.lpDesktop = @"WinSta0\Default"; //Modify as needed
               si.dwFlags = STARTF_USESHOWWINDOW | STARTF_FORCEONFEEDBACK;
               si.wShowWindow = SW_SHOW;
               //Set other si properties as required.

               result = CreateProcessAsUser(
                   token,
                   null,
                   cmdLine,
                   ref saProcess,
                   ref saThread,
                   false,
                   CREATE_UNICODE_ENVIRONMENT,
                   envBlock,
                   null,
                   ref si,
                   out pi);


               if (result == false)
               {
                   int error = Marshal.GetLastWin32Error();
                   string message = String.Format("CreateProcessAsUser Error: {0}", error);
                   Debug.WriteLine(message);

               }

               return result;
           }


           private static IntPtr GetPrimaryToken(int processId)
           {
               IntPtr token = IntPtr.Zero;
               IntPtr primaryToken = IntPtr.Zero;
               bool retVal = false;
               Process p = null;

               try
               {
                   p = Process.GetProcessById(processId);
               }

               catch (ArgumentException)
               {

                   string details = String.Format("ProcessID {0} Not Available", processId);
                   Debug.WriteLine(details);
                   throw;
               }


               //Gets impersonation token
               retVal = OpenProcessToken(p.Handle, TOKEN_DUPLICATE, ref token);
               if (retVal == true)
               {

                   SECURITY_ATTRIBUTES sa = new SECURITY_ATTRIBUTES();
                   sa.nLength = (uint)Marshal.SizeOf(sa);

                   //Convert the impersonation token into Primary token
                   retVal = DuplicateTokenEx(
                       token,
                       TOKEN_ASSIGN_PRIMARY | TOKEN_DUPLICATE | TOKEN_QUERY,
                       ref sa,
                       (int)SECURITY_IMPERSONATION_LEVEL.SecurityIdentification,
                       (int)TOKEN_TYPE.TokenPrimary,
                       ref primaryToken);

                   //Close the Token that was previously opened.
                   CloseHandle(token);
                   if (retVal == false)
                   {
                       string message = String.Format("DuplicateTokenEx Error: {0}", Marshal.GetLastWin32Error());
                       Debug.WriteLine(message);
                   }

               }

               else
               {

                   string message = String.Format("OpenProcessToken Error: {0}", Marshal.GetLastWin32Error());
                   Debug.WriteLine(message);

               }

               //We'll Close this token after it is used.
               return primaryToken;

           }

           private static IntPtr GetEnvironmentBlock(IntPtr token)
           {

               IntPtr envBlock = IntPtr.Zero;
               bool retVal = CreateEnvironmentBlock(ref envBlock, token, false);
               if (retVal == false)
               {

                   //Environment Block, things like common paths to My Documents etc.
                   //Will not be created if "false"
                   //It should not adversley affect CreateProcessAsUser.

                   string message = String.Format("CreateEnvironmentBlock Error: {0}", Marshal.GetLastWin32Error());
                   Debug.WriteLine(message);

               }
               return envBlock;
           }

           public static bool Launch(string appCmdLine /*,int processId*/)
           {

               bool ret = false;

               //Either specify the processID explicitly
               //Or try to get it from a process owned by the user.
               //In this case assuming there is only one explorer.exe

               Process[] ps = Process.GetProcessesByName("explorer");
               int processId = -1;//=processId
               if (ps.Length > 0)
               {
                   processId = ps[0].Id;
               }

               if (processId > 1)
               {
                   IntPtr token = GetPrimaryToken(processId);

                   if (token != IntPtr.Zero)
                   {

                       IntPtr envBlock = GetEnvironmentBlock(token);
                       ret = LaunchProcessAsUser(appCmdLine, token, envBlock);
                       if (envBlock != IntPtr.Zero)
                           DestroyEnvironmentBlock(envBlock);

                       CloseHandle(token);
                   }

               }
               return ret;
           }

       } 
    Tuesday, July 07, 2009 1:27 PM

All replies

  • Windows services run under the "System" user.  You are not going to be able to see any UI from a windows service.

    Now there is a workaround to start a process under a specific user, but you have to know the username/password:

    [StructLayout(LayoutKind.Sequential)]
       internal struct PROCESS_INFORMATION
       {
           public IntPtr hProcess;
           public IntPtr hThread;
           public uint dwProcessId;
           public uint dwThreadId;
       }



       [StructLayout(LayoutKind.Sequential)]
       internal struct SECURITY_ATTRIBUTES
       {
           public uint nLength;
           public IntPtr lpSecurityDescriptor;
           public bool bInheritHandle;
       }


       [StructLayout(LayoutKind.Sequential)]
       public struct STARTUPINFO
       {
           public uint cb;
           public string lpReserved;
           public string lpDesktop;
           public string lpTitle;
           public uint dwX;
           public uint dwY;
           public uint dwXSize;
           public uint dwYSize;
           public uint dwXCountChars;
           public uint dwYCountChars;
           public uint dwFillAttribute;
           public uint dwFlags;
           public short wShowWindow;
           public short cbReserved2;
           public IntPtr lpReserved2;
           public IntPtr hStdInput;
           public IntPtr hStdOutput;
           public IntPtr hStdError;

       }

       internal enum SECURITY_IMPERSONATION_LEVEL
       {
           SecurityAnonymous,
           SecurityIdentification,
           SecurityImpersonation,
           SecurityDelegation
       }

       internal enum TOKEN_TYPE
       {
           TokenPrimary = 1,
           TokenImpersonation
       }

       public class ProcessAsUser
       {

           [DllImport("advapi32.dll", SetLastError = true)]
           private static extern bool CreateProcessAsUser(
               IntPtr hToken,
               string lpApplicationName,
               string lpCommandLine,
               ref SECURITY_ATTRIBUTES lpProcessAttributes,
               ref SECURITY_ATTRIBUTES lpThreadAttributes,
               bool bInheritHandles,
               uint dwCreationFlags,
               IntPtr lpEnvironment,
               string lpCurrentDirectory,
               ref STARTUPINFO lpStartupInfo,
               out PROCESS_INFORMATION lpProcessInformation);


           [DllImport("advapi32.dll", EntryPoint = "DuplicateTokenEx", SetLastError = true)]
           private static extern bool DuplicateTokenEx(
               IntPtr hExistingToken,
               uint dwDesiredAccess,
               ref SECURITY_ATTRIBUTES lpThreadAttributes,
               Int32 ImpersonationLevel,
               Int32 dwTokenType,
               ref IntPtr phNewToken);


           [DllImport("advapi32.dll", SetLastError = true)]
           private static extern bool OpenProcessToken(
               IntPtr ProcessHandle,
               UInt32 DesiredAccess,
               ref IntPtr TokenHandle);

           [DllImport("userenv.dll", SetLastError = true)]
           private static extern bool CreateEnvironmentBlock(
                   ref IntPtr lpEnvironment,
                   IntPtr hToken,
                   bool bInherit);


           [DllImport("userenv.dll", SetLastError = true)]
           private static extern bool DestroyEnvironmentBlock(
                   IntPtr lpEnvironment);

           [DllImport("kernel32.dll", SetLastError = true)]
           private static extern bool CloseHandle(
               IntPtr hObject);

           private const short SW_SHOW = 5;
           private const uint TOKEN_QUERY = 0x0008;
           private const uint TOKEN_DUPLICATE = 0x0002;
           private const uint TOKEN_ASSIGN_PRIMARY = 0x0001;
           private const int GENERIC_ALL_ACCESS = 0x10000000;
           private const int STARTF_USESHOWWINDOW = 0x00000001;
           private const int STARTF_FORCEONFEEDBACK = 0x00000040;
           private const uint CREATE_UNICODE_ENVIRONMENT = 0x00000400;


           private static bool LaunchProcessAsUser(string cmdLine, IntPtr token, IntPtr envBlock)
           {
               bool result = false;


               PROCESS_INFORMATION pi = new PROCESS_INFORMATION();
               SECURITY_ATTRIBUTES saProcess = new SECURITY_ATTRIBUTES();
               SECURITY_ATTRIBUTES saThread = new SECURITY_ATTRIBUTES();
               saProcess.nLength = (uint)Marshal.SizeOf(saProcess);
               saThread.nLength = (uint)Marshal.SizeOf(saThread);

               STARTUPINFO si = new STARTUPINFO();
               si.cb = (uint)Marshal.SizeOf(si);


               //if this member is NULL, the new process inherits the desktop
               //and window station of its parent process. If this member is
               //an empty string, the process does not inherit the desktop and
               //window station of its parent process; instead, the system
               //determines if a new desktop and window station need to be created.
               //If the impersonated user already has a desktop, the system uses the
               //existing desktop.

               si.lpDesktop = @"WinSta0\Default"; //Modify as needed
               si.dwFlags = STARTF_USESHOWWINDOW | STARTF_FORCEONFEEDBACK;
               si.wShowWindow = SW_SHOW;
               //Set other si properties as required.

               result = CreateProcessAsUser(
                   token,
                   null,
                   cmdLine,
                   ref saProcess,
                   ref saThread,
                   false,
                   CREATE_UNICODE_ENVIRONMENT,
                   envBlock,
                   null,
                   ref si,
                   out pi);


               if (result == false)
               {
                   int error = Marshal.GetLastWin32Error();
                   string message = String.Format("CreateProcessAsUser Error: {0}", error);
                   Debug.WriteLine(message);

               }

               return result;
           }


           private static IntPtr GetPrimaryToken(int processId)
           {
               IntPtr token = IntPtr.Zero;
               IntPtr primaryToken = IntPtr.Zero;
               bool retVal = false;
               Process p = null;

               try
               {
                   p = Process.GetProcessById(processId);
               }

               catch (ArgumentException)
               {

                   string details = String.Format("ProcessID {0} Not Available", processId);
                   Debug.WriteLine(details);
                   throw;
               }


               //Gets impersonation token
               retVal = OpenProcessToken(p.Handle, TOKEN_DUPLICATE, ref token);
               if (retVal == true)
               {

                   SECURITY_ATTRIBUTES sa = new SECURITY_ATTRIBUTES();
                   sa.nLength = (uint)Marshal.SizeOf(sa);

                   //Convert the impersonation token into Primary token
                   retVal = DuplicateTokenEx(
                       token,
                       TOKEN_ASSIGN_PRIMARY | TOKEN_DUPLICATE | TOKEN_QUERY,
                       ref sa,
                       (int)SECURITY_IMPERSONATION_LEVEL.SecurityIdentification,
                       (int)TOKEN_TYPE.TokenPrimary,
                       ref primaryToken);

                   //Close the Token that was previously opened.
                   CloseHandle(token);
                   if (retVal == false)
                   {
                       string message = String.Format("DuplicateTokenEx Error: {0}", Marshal.GetLastWin32Error());
                       Debug.WriteLine(message);
                   }

               }

               else
               {

                   string message = String.Format("OpenProcessToken Error: {0}", Marshal.GetLastWin32Error());
                   Debug.WriteLine(message);

               }

               //We'll Close this token after it is used.
               return primaryToken;

           }

           private static IntPtr GetEnvironmentBlock(IntPtr token)
           {

               IntPtr envBlock = IntPtr.Zero;
               bool retVal = CreateEnvironmentBlock(ref envBlock, token, false);
               if (retVal == false)
               {

                   //Environment Block, things like common paths to My Documents etc.
                   //Will not be created if "false"
                   //It should not adversley affect CreateProcessAsUser.

                   string message = String.Format("CreateEnvironmentBlock Error: {0}", Marshal.GetLastWin32Error());
                   Debug.WriteLine(message);

               }
               return envBlock;
           }

           public static bool Launch(string appCmdLine /*,int processId*/)
           {

               bool ret = false;

               //Either specify the processID explicitly
               //Or try to get it from a process owned by the user.
               //In this case assuming there is only one explorer.exe

               Process[] ps = Process.GetProcessesByName("explorer");
               int processId = -1;//=processId
               if (ps.Length > 0)
               {
                   processId = ps[0].Id;
               }

               if (processId > 1)
               {
                   IntPtr token = GetPrimaryToken(processId);

                   if (token != IntPtr.Zero)
                   {

                       IntPtr envBlock = GetEnvironmentBlock(token);
                       ret = LaunchProcessAsUser(appCmdLine, token, envBlock);
                       if (envBlock != IntPtr.Zero)
                           DestroyEnvironmentBlock(envBlock);

                       CloseHandle(token);
                   }

               }
               return ret;
           }

       } 
    Tuesday, July 07, 2009 1:27 PM
  • Actually looking at my sample, it grabs the user token from explorer.exe, so you don't need username/password.

    I surprise even myself sometimes....
    Tuesday, July 07, 2009 1:32 PM
  • Hi,

    How to use it? I am new to c# programming.

     

    Many thanks

    Thursday, December 23, 2010 2:03 PM
  • never mind god it.

    ProcessAsUser.Launch(myCMDString);

    Thursday, December 23, 2010 2:54 PM
  • Hello,

     

    Everything is working fine with the above code when I start my application. Unfortunattly, I cannot stop it. Can someone please help me.

     

    Many thanks,

    This is my code:

     protected override void OnStop()
        {
          base.OnStop();
          //Stop();
          foreach (Process clsProcess in Process.GetProcesses())
          {
    
    
            //check the proces
            if (clsProcess.ProcessName.Contains("MyProg"))
            {
              clsProcess.Kill();
            }
    
    
          }
          
          
        }
    Monday, January 10, 2011 5:57 PM
  • Hi everyone,

    It's works fine with Login as "Local System Account". But how can we run it with specific user account.

    Suggestions and comments are highly appreciated.

    Thanks!


    Zubair Khalid
    Thursday, February 03, 2011 10:42 AM
  • I don't think you can. It uses the user credentials etc that the windows explorer is using.

    I need to run a console app (IIS Express) from a windows service and tried this as well but it's not gunna work.

    Thursday, February 10, 2011 1:09 AM
  • Hi Jonathan,

    This code rocks!  Thank you...

    Johnny

    • Edited by Webzest Saturday, October 29, 2011 2:04 PM
    Saturday, October 29, 2011 2:04 PM
  • Hi Jonathan

    Nice piece of code, I want to use this code to run an application without administrative rights (not elevated mode), I mean even if user run my program as administrator, it reluanch itself without administrator rights, as your code run it with "Explorer.exe" access token.

    when I luanch my application as normal user, your code works fine and it can create another process, but when I run my application as administrator CreateProcessAsUser return "1314" error. did a quick search on Google, couldn't find the reason. do you have any idea why this happening?

    note: I did use CreateRestrictedToken instead of duplicating token from "Explorer.exe", same result, same error.

    Thanks
    Mahyar


    • Edited by Mahyar Sh Wednesday, February 08, 2012 3:07 AM
    Wednesday, February 08, 2012 3:06 AM
  • This is exactly what I was looking for...

    Thanks a bunch!

    Thursday, April 19, 2012 9:40 PM
  • Thanks, I never would have thought to grab the token off of explorer.exe, but that is exactly what I needed!
    Friday, June 15, 2012 9:45 PM
  • Dear Jonathan,

        My service connected to WCF (in other server) and I can pass username&password to this service via the WCf server from other PC.

    Your code perfect when I install and start the service when I'm logged on to windows.

    But if I boot the PC (the service will run), then the service successfully connect to my server (WCF), I successfully pass the username & password , then I logon user by:

    IntPtr token = IntPtr.Zero;
                
    try
    {          
        if (RevertToSelf())
        {
            if (LogonUser(
                userName,
                domain,
                password,
                LOGON32_LOGON_INTERACTIVE,
                LOGON32_PROVIDER_DEFAULT,
                ref token) != 0)
            {
                if (DuplicateToken(token, 2, ref m_AuthToken) != 0)
                {
                        m_NewWindowsIdentity = new WindowsIdentity(m_AuthToken);
                        m_ImpersonationContext = m_NewWindowsIdentity.Impersonate();
                }
                else
                {
                    int nErrorCode = Marshal.GetLastWin32Error();           
                    throw new Win32Exception(nErrorCode);
                }
            }
            else
            {
                int nErrorCode = Marshal.GetLastWin32Error();
                throw new Win32Exception(nErrorCode);
            }
        }
        else
        {
            int nErrorCode = Marshal.GetLastWin32Error();
            throw new Win32Exception(nErrorCode);
        }
    }
    finally
    {
        if (token != IntPtr.Zero)
        {
            CloseHandle(token);
        }
    }

    But after that I call ProcessAsUser.Launch, unfortunately it failed because explore.exe not running (processId < 1)!!!!

    What I can do, what wrong :( ?

    thanks in advanced


    Friday, February 15, 2013 2:50 PM