Click here to Skip to main content
11,927,378 members (46,451 online)
Rate this:
Please Sign up or sign in to vote.
See more: service mode
I am trying to find a way to just simply display the Start Mode for a specific
Windows Services. For example...If I just want to click on a button and have it display the Service Status and Start Type for W3SVC ?
I tried using Servicecontroller but that does not give me this option. Is there a simple way of doing this?
Posted 25-Jan-13 15:12pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Basically, you need to open service manages, with the manager, open a service, and then change it configuration. Three steps:[^],[^],[^].

By first step, you get the handle of the Service Manager, SC_HANDLE, using OpenSCManager, then, by second step, use it as a first parameter for the call to OpenService and then, finally, use the resulting handle to the service, to call ChangeServiceConfig.

You will need P/Invoke. If you need to learn P/Invoke, start from here:[^],[^].

This CodeProject can also be useful: Essential P/Invoke[^].

One alternative to P/Invoke is to use C++/CLI mixed-mode (managed+unmanaged) project. This project could build a DLL required by the global hooks, but it can contain some managed wrapper CLI code, so, from the standpoint of your .NET application, you can use it as a regular .NET assembly, that is, reference it. Please see:[^],[^],[^],[^].

This way, you can write the code in C++, wrap it in C++/CLI managed class and reference the resulting module as a .NET assembly.

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

By the way, one extremely primitive alternative to Solution 1: use "SC.EXE" (Service Control Manager) via System.Diagnostics.Process.Start:

System.ServiceProcess.ServiceStartMode startMode = // ...
string processName = // ...


string application = "SC";
string commandLine = string.Format(
    "CONFIG {0}, start= {1}", // important: blank space after '='
    processName, startMode);
System.Diagnostics.Process.Start(application, commandLine);

You can also redirect the stream StandardOutput to capture output of the application. Just in case, redirect also StandardError. You will find the code sample for redirection here:[^].


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.151126.1 | Last Updated 25 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100