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. "Allow Service to interact with Desktop" Windows Services

"Allow Service to interact with Desktop" Windows Services

Scheduled Pinned Locked Moved C#
question
6 Posts 6 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
    Piyush Vaishnav
    wrote on last edited by
    #1

    "Allow Service to interact with Desktop" in Services property. How can i set this property programmatically. Thanks ........

    S S S 3 Replies Last reply
    0
    • P Piyush Vaishnav

      "Allow Service to interact with Desktop" in Services property. How can i set this property programmatically. Thanks ........

      S Offline
      S Offline
      Scott Dorman
      wrote on last edited by
      #2

      Setting this property at all is not generally advisable. Services aren't meant to provide UI or allow any other type of user interaction.

      Scott Dorman

      Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA [Blog][Articles][Forum Guidelines]


      Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai

      1 Reply Last reply
      0
      • P Piyush Vaishnav

        "Allow Service to interact with Desktop" in Services property. How can i set this property programmatically. Thanks ........

        S Offline
        S Offline
        Steve Messer
        wrote on last edited by
        #3

        You should ask yourself if you should be doing this. The reason I used this code was I had a windows service that logged into a DB2 database via an ODBC driver. They routinely changed their passwords and sometimes would forget to tell us. When we tried to login in after a password had been changed the ODBC driver would pop up (attempt to pop up) a dialog allowing you to enter the new password which of course never showed up as it didn't have permissions. We didn't realize any problem until the client called telling us that they hadn't received any new data. Anyways the following code worked for me. Reference System.Management

        /// Start the service
        protected override void OnStart(string[] args)
        {
        ServiceDesktopPermission();
        }

        /// Using Management Object services the option "Allow service to interact with desktop"
        /// can be enabled. This gets unchecked/disabled each time a new install or update of the
        /// service is performed.
        static public void ServiceDesktopPermission()
        {
        try
        {
        ConnectionOptions coOptions = new ConnectionOptions();

        coOptions.Impersonation = ImpersonationLevel.Impersonate;
        	
        // CIMV2 is a namespace that contains all of the core OS and hardware classes.
        // CIM (Common Information Model) which is an industry standard for describing 
        //        data about applications and devices so that administrators and software 
        //        management programs can control applications and devices on different 
        //        platforms in the same way, ensuring interoperability across a network. 
        		    
        ManagementScope mgmtScope = new ManagementScope(@"root\\CIMV2", coOptions);
        	
        mgmtScope.Connect();
        	
        ManagementObject wmiService;
        
        wmiService = new ManagementObject("Win32\_Service.Name='" + "My Service Name" + "'");
        	
        ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");
        	
        InParam\["DesktopInteract"\] = true;
        	
        wmiService.InvokeMethod("Change", InParam, null);
          }
        

        catch(Exception ex)
        {
        //TODO: Log this error
        }
        }

        A M 2 Replies Last reply
        0
        • S Steve Messer

          You should ask yourself if you should be doing this. The reason I used this code was I had a windows service that logged into a DB2 database via an ODBC driver. They routinely changed their passwords and sometimes would forget to tell us. When we tried to login in after a password had been changed the ODBC driver would pop up (attempt to pop up) a dialog allowing you to enter the new password which of course never showed up as it didn't have permissions. We didn't realize any problem until the client called telling us that they hadn't received any new data. Anyways the following code worked for me. Reference System.Management

          /// Start the service
          protected override void OnStart(string[] args)
          {
          ServiceDesktopPermission();
          }

          /// Using Management Object services the option "Allow service to interact with desktop"
          /// can be enabled. This gets unchecked/disabled each time a new install or update of the
          /// service is performed.
          static public void ServiceDesktopPermission()
          {
          try
          {
          ConnectionOptions coOptions = new ConnectionOptions();

          coOptions.Impersonation = ImpersonationLevel.Impersonate;
          	
          // CIMV2 is a namespace that contains all of the core OS and hardware classes.
          // CIM (Common Information Model) which is an industry standard for describing 
          //        data about applications and devices so that administrators and software 
          //        management programs can control applications and devices on different 
          //        platforms in the same way, ensuring interoperability across a network. 
          		    
          ManagementScope mgmtScope = new ManagementScope(@"root\\CIMV2", coOptions);
          	
          mgmtScope.Connect();
          	
          ManagementObject wmiService;
          
          wmiService = new ManagementObject("Win32\_Service.Name='" + "My Service Name" + "'");
          	
          ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");
          	
          InParam\["DesktopInteract"\] = true;
          	
          wmiService.InvokeMethod("Change", InParam, null);
            }
          

          catch(Exception ex)
          {
          //TODO: Log this error
          }
          }

          A Offline
          A Offline
          aftkak
          wrote on last edited by
          #4

          it worked for me; thanks a lot :)

          1 Reply Last reply
          0
          • P Piyush Vaishnav

            "Allow Service to interact with Desktop" in Services property. How can i set this property programmatically. Thanks ........

            S Offline
            S Offline
            stan_p
            wrote on last edited by
            #5

            Try this C# code href="http://it.expertmonster.com/question/Allow-service-to-interact-with-desktop-112.html

            1 Reply Last reply
            0
            • S Steve Messer

              You should ask yourself if you should be doing this. The reason I used this code was I had a windows service that logged into a DB2 database via an ODBC driver. They routinely changed their passwords and sometimes would forget to tell us. When we tried to login in after a password had been changed the ODBC driver would pop up (attempt to pop up) a dialog allowing you to enter the new password which of course never showed up as it didn't have permissions. We didn't realize any problem until the client called telling us that they hadn't received any new data. Anyways the following code worked for me. Reference System.Management

              /// Start the service
              protected override void OnStart(string[] args)
              {
              ServiceDesktopPermission();
              }

              /// Using Management Object services the option "Allow service to interact with desktop"
              /// can be enabled. This gets unchecked/disabled each time a new install or update of the
              /// service is performed.
              static public void ServiceDesktopPermission()
              {
              try
              {
              ConnectionOptions coOptions = new ConnectionOptions();

              coOptions.Impersonation = ImpersonationLevel.Impersonate;
              	
              // CIMV2 is a namespace that contains all of the core OS and hardware classes.
              // CIM (Common Information Model) which is an industry standard for describing 
              //        data about applications and devices so that administrators and software 
              //        management programs can control applications and devices on different 
              //        platforms in the same way, ensuring interoperability across a network. 
              		    
              ManagementScope mgmtScope = new ManagementScope(@"root\\CIMV2", coOptions);
              	
              mgmtScope.Connect();
              	
              ManagementObject wmiService;
              
              wmiService = new ManagementObject("Win32\_Service.Name='" + "My Service Name" + "'");
              	
              ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");
              	
              InParam\["DesktopInteract"\] = true;
              	
              wmiService.InvokeMethod("Change", InParam, null);
                }
              

              catch(Exception ex)
              {
              //TODO: Log this error
              }
              }

              M Offline
              M Offline
              mehappycamper
              wrote on last edited by
              #6

              Excellent, thank you. Just what I was looking for. I only have one suggestion: do it in the service installer after service installation is completed, before the service starts up. This way the first time the service starts, it's already able to interract with the desktop: private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e) { ServiceDesktopPermission(); var controller = new ServiceController("SERVICE_NAME"); controller.Start(); }

              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