How to start/stop a Windows service from another program 
Author Message
 How to start/stop a Windows service from another program

How can i start and stop a service from a windows program?
And eventually see if the service is running.
Does any one have some sample code or a good link..?

Best regards
Henrik



Fri, 04 Jun 2004 18:21:01 GMT  
 How to start/stop a Windows service from another program
Look at the Management namespace classes in MSDN for details.
Herewith a small sample:

using System;
using System.Management;
....

  ManagementPath path = ManagementPath.DefaultPath;

  ManagementObject mo = new ManagementObject(path);
  if((string)mo["state"] == "Stopped") // If stopped start it again
   mo.InvokeMethod(moHandler,  "StartService", null);
  if((string)mo["state"] == "Running") // if running stop the service
   mo.InvokeMethod(moHandler,  "StopService", null);
...
  mo.Dispose();

Willy.

Quote:

> How can i start and stop a service from a windows program?
> And eventually see if the service is running.
> Does any one have some sample code or a good link..?

> Best regards
> Henrik



Fri, 04 Jun 2004 20:03:23 GMT  
 How to start/stop a Windows service from another program

Hi Willy,

Thanks for our answer - But how do you define the moHandler ....?

REgards, Henrik

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Fri, 04 Jun 2004 21:11:57 GMT  
 How to start/stop a Windows service from another program
Take a look at the System.ServiceProcess namespace.
The ServiceController class contains 2 static function to obtain the
list of all services and devices present on your machine.
- GetServices (=> service)
- GetDevices (=> driver)
This function returns a ServiceController list, which you can
enumerate. ServiceController provide all properties and function to
set or get the service state.

If you were french, I could suggest you my C# book, in which I explain
step by step the built of a service manager application (chapt 4).
Here I can only give you the key concept to add this feature in your
developpement.

Sylvain Gross
Auteur du livre CookBook C#
http://www.microapp.com/fiche_produit.cfm?ref_produit=3180

Quote:

> How can i start and stop a service from a windows program?
> And eventually see if the service is running.
> Does any one have some sample code or a good link..?

> Best regards
> Henrik



Fri, 04 Jun 2004 22:56:38 GMT  
 How to start/stop a Windows service from another program
Remove the moHandler, the snippet I posted comes from a larger piece of code using it for asynchronous notification.
Change it to :
   mo.InvokeMethod( "StartService", null);
and it should work.

Willy.

Quote:

> Hi Willy,

> Thanks for our answer - But how do you define the moHandler ....?

> REgards, Henrik

> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!



Fri, 04 Jun 2004 22:54:51 GMT  
 How to start/stop a Windows service from another program

I use the ServiceController class.  It is in the System.ServiceProcess  namespace.

Here is a code sample...

using System.ServiceProcess;

....

ServiceController[] services = ServiceController.GetServices(host);

foreach(ServiceController service in services)

    .... do whatever you want.

"host" is the node name of a remote system if you are going to manage services from another node.  If not, just make the call as ServiceController.GetServices().

Hope this helps.

    Doug Schoettle

    Memphis

Quote:

> How can i start and stop a service from a windows program?
> And eventually see if the service is running.
> Does any one have some sample code or a good link..?

> Best regards
> Henrik



Sat, 05 Jun 2004 00:50:39 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Starting and Stopping NT Services in VC++

2. starting/stopping nt4 services

3. Stopping/Starting NT Service

4. Service start and stop

5. list / start / stop services

6. Starting and Stopping Win32 Services in Visual C++

7. how to stop a windows service within my code

8. Stop Windows Service

9. How do I stop a 2nd instance of the program from starting

10. A DOS program starting a Windows program?

11. Windows Service not starting with 1053 error - one of possible causes - solution

12. windows service start() has no contract!

 

 
Powered by phpBB® Forum Software