Starting a Windows app from a windows service

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ahmed Perlom

    #1

    Starting a Windows app from a windows service

    Hi all,



    I am trying to start a windows application that has a GUI from a Windows
    service written in .NET 2.0. I have been searching on this for few days now
    with no avail. When using the System.Diagnost ic.Process object to start the
    application (i.e Notepad), the new app runs and it is listed on the task
    manger list, but the GUI doesn't show up in the desktop of the current user.
    I am aware windows service (either LocalSystem, NetworkService or
    LocalService) all run in a sandbox environment. I have read some where
    here that there is no secure and easy way to do this, however I have no
    option really and I wonder if some one could give me some hints as how would
    I do this in C# or VB.NET!!.



    Thanks


  • Willy Denoyette [MVP]

    #2
    Re: Starting a Windows app from a windows service


    "Ahmed Perlom" <perlom@hotmail .com> wrote in message
    news:OcDp8CtQGH A.2816@TK2MSFTN GP15.phx.gbl...
    | Hi all,
    |
    |
    |
    | I am trying to start a windows application that has a GUI from a Windows
    | service written in .NET 2.0. I have been searching on this for few days
    now
    | with no avail. When using the System.Diagnost ic.Process object to start
    the
    | application (i.e Notepad), the new app runs and it is listed on the task
    | manger list, but the GUI doesn't show up in the desktop of the current
    user.
    | I am aware windows service (either LocalSystem, NetworkService or
    | LocalService) all run in a sandbox environment. I have read some where
    | here that there is no secure and easy way to do this, however I have no
    | option really and I wonder if some one could give me some hints as how
    would
    | I do this in C# or VB.NET!!.
    |
    |
    |
    | Thanks
    |
    |

    Why do you need to start the UI application from a service in the first
    place, there is no easy and secure way to do this. Why don't you start the
    UI application when the user logs on, set a shortcut to the application in
    the startup folder in the users profile and it will start whenever the user
    logs on.


    Willy.


    Comment

    • Ahmed Perlom

      #3
      Re: Starting a Windows app from a windows service

      Thanks of your reply. The idea is that the windows service would monitor the
      run of that application. If the application halts, or of there is an update,
      the service would ask all instances of the application to stop (assuming
      multiple apps are running for each user session), then ask them to -restart
      again. So the service has to be in a global location monitoring whenever
      users log on/off. The other tough side is that it is the current design of
      the solutions by the architect and I just joined in, not much I can do ;)
      Please let me know about the hard and insecure way.

      Thanx




      "Willy Denoyette [MVP]" <willy.denoyett e@telenet.be> wrote in message
      news:eFEywVtQGH A.2816@TK2MSFTN GP15.phx.gbl...[color=blue]
      >
      > "Ahmed Perlom" <perlom@hotmail .com> wrote in message
      > news:OcDp8CtQGH A.2816@TK2MSFTN GP15.phx.gbl...
      > | Hi all,
      > |
      > |
      > |
      > | I am trying to start a windows application that has a GUI from a Windows
      > | service written in .NET 2.0. I have been searching on this for few days
      > now
      > | with no avail. When using the System.Diagnost ic.Process object to start
      > the
      > | application (i.e Notepad), the new app runs and it is listed on the task
      > | manger list, but the GUI doesn't show up in the desktop of the current
      > user.
      > | I am aware windows service (either LocalSystem, NetworkService or
      > | LocalService) all run in a sandbox environment. I have read some where
      > | here that there is no secure and easy way to do this, however I have no
      > | option really and I wonder if some one could give me some hints as how
      > would
      > | I do this in C# or VB.NET!!.
      > |
      > |
      > |
      > | Thanks
      > |
      > |
      >
      > Why do you need to start the UI application from a service in the first
      > place, there is no easy and secure way to do this. Why don't you start the
      > UI application when the user logs on, set a shortcut to the application in
      > the startup folder in the users profile and it will start whenever the
      > user
      > logs on.
      >
      >
      > Willy.
      >
      >[/color]


      Comment

      • David Levine

        #4
        Re: Starting a Windows app from a windows service

        Make sure your service is enabled to interact with the active desktop - if
        it isn't then the GUI portion may run on a desktop that is not visible to
        the logged on user.


        "Ahmed Perlom" <perlom@hotmail .com> wrote in message
        news:OcDp8CtQGH A.2816@TK2MSFTN GP15.phx.gbl...[color=blue]
        > Hi all,
        >
        >
        >
        > I am trying to start a windows application that has a GUI from a Windows
        > service written in .NET 2.0. I have been searching on this for few days
        > now with no avail. When using the System.Diagnost ic.Process object to
        > start the application (i.e Notepad), the new app runs and it is listed on
        > the task manger list, but the GUI doesn't show up in the desktop of the
        > current user. I am aware windows service (either LocalSystem,
        > NetworkService or LocalService) all run in a sandbox environment. I have
        > read some where here that there is no secure and easy way to do this,
        > however I have no option really and I wonder if some one could give me
        > some hints as how would I do this in C# or VB.NET!!.
        >
        >
        >
        > Thanks
        >
        >[/color]


        Comment

        • Willy Denoyette [MVP]

          #5
          Re: Starting a Windows app from a windows service

          The OP is not talking about a Service with a GUI portion, he wants to start
          another GUI style application from a service.
          Note also that enabling a service to interact with a desktop won't work any
          longer in VISTA and beyond.
          Willy.


          "David Levine" <SnipHereDlevin enntp2AndHere@w i.rr.com> wrote in message
          news:uPB6ADyQGH A.1576@tk2msftn gp13.phx.gbl...
          | Make sure your service is enabled to interact with the active desktop - if
          | it isn't then the GUI portion may run on a desktop that is not visible to
          | the logged on user.
          |
          |
          | "Ahmed Perlom" <perlom@hotmail .com> wrote in message
          | news:OcDp8CtQGH A.2816@TK2MSFTN GP15.phx.gbl...
          | > Hi all,
          | >
          | >
          | >
          | > I am trying to start a windows application that has a GUI from a Windows
          | > service written in .NET 2.0. I have been searching on this for few days
          | > now with no avail. When using the System.Diagnost ic.Process object to
          | > start the application (i.e Notepad), the new app runs and it is listed
          on
          | > the task manger list, but the GUI doesn't show up in the desktop of the
          | > current user. I am aware windows service (either LocalSystem,
          | > NetworkService or LocalService) all run in a sandbox environment. I
          have
          | > read some where here that there is no secure and easy way to do this,
          | > however I have no option really and I wonder if some one could give me
          | > some hints as how would I do this in C# or VB.NET!!.
          | >
          | >
          | >
          | > Thanks
          | >
          | >
          |
          |


          Comment

          • Willy Denoyette [MVP]

            #6
            Re: Starting a Windows app from a windows service

            I don't get it really, you are talking about multiple applications (Windows
            Apps.) you want to start, stop, restart etc. What's the interactive user's
            role here? Anyway, what you are trying to achieve is exactly what MSFT is
            trying to prevent by all means, and is nearly impossible to make it secure
            and robust. Note also that nothing in the framework will help you with this
            either, you will have to PInvoke all Win32 API's and make sure you don't
            forget any.
            Before you go, you should have a good understanding of:
            1. Windows desktops, window stations, how they interact with each other and
            how they relate to the applications and their threads using these secured
            executive objects. Please consult the MSDN docs and read as much as you can
            about Desktops and WinStations (search MSDN for About Window Stations and
            Desktops).
            2. The security context of the user running as a service (the service
            account) and the context of the application launched by such a service.

            When launching an application from a service you will need to:
            - Create a logon session by calling Win32 LogonUser (via PInvoke) followed
            by,
            - a call to LoadUserProfile , this is needed to load it's environment and
            profile, when done you can call...
            - CreateProcessAs User, here you need to fill the STARTUPINFO structure and
            especially you need to set the 'lpdesktop' member to "Winsta0\Defaul t", by
            doing this you will start the application in the visible desktop. But there
            is a catch, before you call CreateProcessAs User you will need to add the
            Logon users SID to the DACL of the Winsta0 object. Failing to do this will
            prevent the launched application to use the visible desktop.
            With any luck you'll have your application running, but, remember that you
            have created a new logon session, that means that the application doesn't
            run in the already existing interactive logon session, so each time you do
            this you will create a new session and you will load the Users Registry Hive
            and the environmat block into that session as HKCU, this will consume a lot
            of resources when the previous session did not terminate.

            Willy.




            "Ahmed Perlom" <perlom@hotmail .com> wrote in message
            news:efCBVetQGH A.1160@TK2MSFTN GP09.phx.gbl...
            | Thanks of your reply. The idea is that the windows service would monitor
            the
            | run of that application. If the application halts, or of there is an
            update,
            | the service would ask all instances of the application to stop (assuming
            | multiple apps are running for each user session), then ask them
            to -restart
            | again. So the service has to be in a global location monitoring whenever
            | users log on/off. The other tough side is that it is the current design
            of
            | the solutions by the architect and I just joined in, not much I can do ;)
            | Please let me know about the hard and insecure way.
            |
            | Thanx
            |
            |
            |
            |
            | "Willy Denoyette [MVP]" <willy.denoyett e@telenet.be> wrote in message
            | news:eFEywVtQGH A.2816@TK2MSFTN GP15.phx.gbl...
            | >
            | > "Ahmed Perlom" <perlom@hotmail .com> wrote in message
            | > news:OcDp8CtQGH A.2816@TK2MSFTN GP15.phx.gbl...
            | > | Hi all,
            | > |
            | > |
            | > |
            | > | I am trying to start a windows application that has a GUI from a
            Windows
            | > | service written in .NET 2.0. I have been searching on this for few
            days
            | > now
            | > | with no avail. When using the System.Diagnost ic.Process object to
            start
            | > the
            | > | application (i.e Notepad), the new app runs and it is listed on the
            task
            | > | manger list, but the GUI doesn't show up in the desktop of the current
            | > user.
            | > | I am aware windows service (either LocalSystem, NetworkService or
            | > | LocalService) all run in a sandbox environment. I have read some
            where
            | > | here that there is no secure and easy way to do this, however I have
            no
            | > | option really and I wonder if some one could give me some hints as how
            | > would
            | > | I do this in C# or VB.NET!!.
            | > |
            | > |
            | > |
            | > | Thanks
            | > |
            | > |
            | >
            | > Why do you need to start the UI application from a service in the first
            | > place, there is no easy and secure way to do this. Why don't you start
            the
            | > UI application when the user logs on, set a shortcut to the application
            in
            | > the startup folder in the users profile and it will start whenever the
            | > user
            | > logs on.
            | >
            | >
            | > Willy.
            | >
            | >
            |
            |


            Comment

            • John Timney \( MVP \)

              #7
              Re: Starting a Windows app from a windows service

              I gotta agree with Willy on this one. Using a service like this is not a
              good approach.

              A cleaner alternative would be to create a monitor application, that starts
              with the user - and checks the running processes. That way, it runs and
              dies with that users and has all the correct rights to operate in that users
              context/desktop. One feature of this is that you can add a socket
              server/listener to it that accepts remote commands, allowing the app to
              spawn localised applications easily at the active desktop. It will only be
              a handful of code lines to create.

              --
              Regards

              John Timney
              Microsoft MVP

              "Willy Denoyette [MVP]" <willy.denoyett e@telenet.be> wrote in message
              news:em0wuG3QGH A.196@TK2MSFTNG P10.phx.gbl...[color=blue]
              > The OP is not talking about a Service with a GUI portion, he wants to
              > start
              > another GUI style application from a service.
              > Note also that enabling a service to interact with a desktop won't work
              > any
              > longer in VISTA and beyond.
              > Willy.
              >
              >
              > "David Levine" <SnipHereDlevin enntp2AndHere@w i.rr.com> wrote in message
              > news:uPB6ADyQGH A.1576@tk2msftn gp13.phx.gbl...
              > | Make sure your service is enabled to interact with the active desktop -
              > if
              > | it isn't then the GUI portion may run on a desktop that is not visible
              > to
              > | the logged on user.
              > |
              > |
              > | "Ahmed Perlom" <perlom@hotmail .com> wrote in message
              > | news:OcDp8CtQGH A.2816@TK2MSFTN GP15.phx.gbl...
              > | > Hi all,
              > | >
              > | >
              > | >
              > | > I am trying to start a windows application that has a GUI from a
              > Windows
              > | > service written in .NET 2.0. I have been searching on this for few
              > days
              > | > now with no avail. When using the System.Diagnost ic.Process object to
              > | > start the application (i.e Notepad), the new app runs and it is listed
              > on
              > | > the task manger list, but the GUI doesn't show up in the desktop of
              > the
              > | > current user. I am aware windows service (either LocalSystem,
              > | > NetworkService or LocalService) all run in a sandbox environment. I
              > have
              > | > read some where here that there is no secure and easy way to do this,
              > | > however I have no option really and I wonder if some one could give me
              > | > some hints as how would I do this in C# or VB.NET!!.
              > | >
              > | >
              > | >
              > | > Thanks
              > | >
              > | >
              > |
              > |
              >
              >[/color]


              Comment

              • Terry

                #8
                Re: Starting a Windows app from a windows service

                the way I have done this in the past is to have the service modify a
                registry setting / file / etc when it needs to update teh UI app.
                Have your UI application periodically check the registry key - if it is set,
                then have the UI app shell another (very small, generic) app that takes an
                exe as a command line parameter. The UI app then ends.
                The 2nd app has a timer that wait 10 secs (to allow the first app to end)
                and then shells the command line parameter passed - which is the name of the
                first app to be restarted.
                Consideration must be given for the case when the user is currently in the
                middle of doing something - if the app dies then you shall not thanked.
                cheers
                Terry


                "Ahmed Perlom" <perlom@hotmail .com> wrote in message
                news:OcDp8CtQGH A.2816@TK2MSFTN GP15.phx.gbl...[color=blue]
                > Hi all,
                >
                >
                >
                > I am trying to start a windows application that has a GUI from a Windows
                > service written in .NET 2.0. I have been searching on this for few days
                > now with no avail. When using the System.Diagnost ic.Process object to
                > start the application (i.e Notepad), the new app runs and it is listed on
                > the task manger list, but the GUI doesn't show up in the desktop of the
                > current user. I am aware windows service (either LocalSystem,
                > NetworkService or LocalService) all run in a sandbox environment. I have
                > read some where here that there is no secure and easy way to do this,
                > however I have no option really and I wonder if some one could give me
                > some hints as how would I do this in C# or VB.NET!!.
                >
                >
                >
                > Thanks
                >
                >[/color]


                Comment

                Working...