Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Issue Launching UI Application from windows service in c# Windows

Issue Launching UI Application from windows service in c# Windows

Scheduled Pinned Locked Moved C#
helpcsharpdesigntutorial
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    platso_588
    wrote on last edited by
    #1

    I am getting problem when i launch a UI Application from windows Service Application. My requirement is while starting up of the service i need to run a UI Application.for Example i have taken notepad.exe. The below code is the code i have implemented to run the process. Process p = new Process(); p.StartInfo.FileName = @"C:\WINDOWS\system32\notepad.exe"; bool bretval = p.Start(); Problem here is: It is running the process(notepad.exe) in background. I am not able to see it. I have gone through several Articles and i felt that WINDOWS service will supress the UI. Is there any Work around or any other Option so that my requirement can be obtained. Pls help me in this regard.

    C 1 Reply Last reply
    0
    • P platso_588

      I am getting problem when i launch a UI Application from windows Service Application. My requirement is while starting up of the service i need to run a UI Application.for Example i have taken notepad.exe. The below code is the code i have implemented to run the process. Process p = new Process(); p.StartInfo.FileName = @"C:\WINDOWS\system32\notepad.exe"; bool bretval = p.Start(); Problem here is: It is running the process(notepad.exe) in background. I am not able to see it. I have gone through several Articles and i felt that WINDOWS service will supress the UI. Is there any Work around or any other Option so that my requirement can be obtained. Pls help me in this regard.

      C Offline
      C Offline
      Covean
      wrote on last edited by
      #2

      You can try to activate "interact with desktop" for your service. However on Vista or later this flag become obsolete. Generally a service should not have to do anything with UI, because they also can run if nobody is logged in.

      Greetings Covean

      P 1 Reply Last reply
      0
      • C Covean

        You can try to activate "interact with desktop" for your service. However on Vista or later this flag become obsolete. Generally a service should not have to do anything with UI, because they also can run if nobody is logged in.

        Greetings Covean

        P Offline
        P Offline
        platso_588
        wrote on last edited by
        #3

        I Thank you for your awesome reply. It helped me and it worked out. As u told that this option cannot be implemented in Vista. I will tell you my requirement which is, I have a USB Device, when ever it is plugged in i need to Open my Application(.exe). It is just like iTunes Application for iPod. I have code for Detecting my USB Device. So i thought of implementing the device detecting code in windows service and if the device is found i can launch my application. Can you tell me any work around or any other way to Implement this senario so that it will work even in Vista. Regards, Prasad.

        C 1 Reply Last reply
        0
        • P platso_588

          I Thank you for your awesome reply. It helped me and it worked out. As u told that this option cannot be implemented in Vista. I will tell you my requirement which is, I have a USB Device, when ever it is plugged in i need to Open my Application(.exe). It is just like iTunes Application for iPod. I have code for Detecting my USB Device. So i thought of implementing the device detecting code in windows service and if the device is found i can launch my application. Can you tell me any work around or any other way to Implement this senario so that it will work even in Vista. Regards, Prasad.

          C Offline
          C Offline
          Covean
          wrote on last edited by
          #4

          I would try the following way. In your service handler function wait for the SERVICE_CONTROL_SESSIONCHANGE event. (You have to say the system that your service wants this notification (SetServiceStatus with SERVICE_ACCEPT_SESSIONCHANGE)). After you received the SERVICE_CONTROL_SESSIONCHANGE (WTS_SESSION_LOGON) event, get the users new session id from the WTSSESSION_NOTIFICATION structure and "save" it. If you want to start the new process you have to do the following steps. Use WTSQueryUserToken to get the primary access token. Now try to acquire TOKEN_QUERY, TOKEN_DUPLICATE, and TOKEN_ASSIGN_PRIMARY for this token by using SetTokenInformation. Last Step: CreateProcessAsUser to start your process. I'm short in time so I couldn't test it but I think this should do the work.

          Greetings Covean

          P 1 Reply Last reply
          0
          • C Covean

            I would try the following way. In your service handler function wait for the SERVICE_CONTROL_SESSIONCHANGE event. (You have to say the system that your service wants this notification (SetServiceStatus with SERVICE_ACCEPT_SESSIONCHANGE)). After you received the SERVICE_CONTROL_SESSIONCHANGE (WTS_SESSION_LOGON) event, get the users new session id from the WTSSESSION_NOTIFICATION structure and "save" it. If you want to start the new process you have to do the following steps. Use WTSQueryUserToken to get the primary access token. Now try to acquire TOKEN_QUERY, TOKEN_DUPLICATE, and TOKEN_ASSIGN_PRIMARY for this token by using SetTokenInformation. Last Step: CreateProcessAsUser to start your process. I'm short in time so I couldn't test it but I think this should do the work.

            Greetings Covean

            P Offline
            P Offline
            platso_588
            wrote on last edited by
            #5

            Thank you Covean, Sorry,I could not completely Implement the suggestion given by you. Here is what i implemented. PROCESS_INFORMATION pi; STARTUPINFO si =new STARTUPINFO(); System.IntPtr hToken; string commandLine, userPart, domainPart, password, lpCurrentDirectory; // Initialize structs si.cb = Marshal.SizeOf(si); userPart = "Prasad"; password = "pass"; domainPart = ""; commandLine = ""; lpCurrentDirectory = @"D:\DemoApplication\bin\Debug"; // Get the user token if (NativeWin32Methods.LogonUser(userPart, domainPart, password, (int)LOGON_TYPE.LOGON32_LOGON_BATCH, (int)LOGON_PROVIDER.LOGON32_PROVIDER_DEFAULT, out hToken)) { // Create structs SECURITY_ATTRIBUTES saProcessAttributes = new SECURITY_ATTRIBUTES(); SECURITY_ATTRIBUTES saThreadAttributes = new SECURITY_ATTRIBUTES(); // Now create the process as the user if (!NativeWin32Methods.CreateProcessAsUser(hToken, appPath, commandLine, ref saProcessAttributes, ref saThreadAttributes, false, 0, IntPtr.Zero, lpCurrentDirectory, ref si, out pi)) { // Throw exception throw new Exception("Failed to CreateProcessAsUser"); } } If you can tell me the modified one i would be very much thank ful to you. Prasad.

            C 1 Reply Last reply
            0
            • P platso_588

              Thank you Covean, Sorry,I could not completely Implement the suggestion given by you. Here is what i implemented. PROCESS_INFORMATION pi; STARTUPINFO si =new STARTUPINFO(); System.IntPtr hToken; string commandLine, userPart, domainPart, password, lpCurrentDirectory; // Initialize structs si.cb = Marshal.SizeOf(si); userPart = "Prasad"; password = "pass"; domainPart = ""; commandLine = ""; lpCurrentDirectory = @"D:\DemoApplication\bin\Debug"; // Get the user token if (NativeWin32Methods.LogonUser(userPart, domainPart, password, (int)LOGON_TYPE.LOGON32_LOGON_BATCH, (int)LOGON_PROVIDER.LOGON32_PROVIDER_DEFAULT, out hToken)) { // Create structs SECURITY_ATTRIBUTES saProcessAttributes = new SECURITY_ATTRIBUTES(); SECURITY_ATTRIBUTES saThreadAttributes = new SECURITY_ATTRIBUTES(); // Now create the process as the user if (!NativeWin32Methods.CreateProcessAsUser(hToken, appPath, commandLine, ref saProcessAttributes, ref saThreadAttributes, false, 0, IntPtr.Zero, lpCurrentDirectory, ref si, out pi)) { // Throw exception throw new Exception("Failed to CreateProcessAsUser"); } } If you can tell me the modified one i would be very much thank ful to you. Prasad.

              C Offline
              C Offline
              Covean
              wrote on last edited by
              #6

              In your example code I miss the SetTokenInformation part. Thats all I can say at the first look. I also received your e-Mail and I will answer if I have the time for (I hope this will be tommorrow   :) ).

              Greetings Covean

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups