How to change the Startup Mode of a Service?

F

FB's .NET Dev PC

On one PC, I have several services, all of which need to run, some of which
I am writing, and some of which (such as SQL server) I am not writing.

My overall goal is to have a restartable, stable system. One of my services
is a "system monitor" which controls the startup of the other services I am
writing, based on the SQL server having started.

I am using WMI to monitor services, and
System.ServiceProcess.ServiceController to start and stop them. This works.

What I need to do, if all other conditions fail to resolve problems, is set
my System Monitor to "Manual" startup from the "Automatic" which I set as a
ServiceStartMode in the installer.

How can I change the start mode of a service from within that service (or
any other service, for that matter)?

Thanks
Fred Bourdelier
Tucson AZ


PS I use the code below to reboot the computer - this only works if no
service or app is hung. Is there a way to force a reboot through hanging
programs?
System.Diagnostics.Process.Start("shutdown", "-s -t 00")
System.Diagnostics.Process.Start("shutdown", "-r -t 00")

System.Diagnostics.Process.Start("shutdown", "-l -t 00")
 
K

Ken Tucker [MVP]

Hi,

Invoke the win32_service wmi classes changestartmode method.

http://msdn.microsoft.com/library/d...gestartmode_method_in_class_win32_service.asp

Ken
------------------
On one PC, I have several services, all of which need to run, some of which
I am writing, and some of which (such as SQL server) I am not writing.

My overall goal is to have a restartable, stable system. One of my services
is a "system monitor" which controls the startup of the other services I am
writing, based on the SQL server having started.

I am using WMI to monitor services, and
System.ServiceProcess.ServiceController to start and stop them. This works.

What I need to do, if all other conditions fail to resolve problems, is set
my System Monitor to "Manual" startup from the "Automatic" which I set as a
ServiceStartMode in the installer.

How can I change the start mode of a service from within that service (or
any other service, for that matter)?

Thanks
Fred Bourdelier
Tucson AZ


PS I use the code below to reboot the computer - this only works if no
service or app is hung. Is there a way to force a reboot through hanging
programs?
System.Diagnostics.Process.Start("shutdown", "-s -t 00")
System.Diagnostics.Process.Start("shutdown", "-r -t 00")

System.Diagnostics.Process.Start("shutdown", "-l -t 00")
 
A

Alex Clark

The -f switch on XP forces hung apps to end immediately, alternatively you
could use a Win API method specifically designed to shut down the system
(not sure what it will be called but do a quick MSDN search and you'll find
it, probably in Advapi.dll) --- this would probably have a flag that you
could specify to force a system shutdown.

Cheers,
Alex Clark
 
F

FB's .NET Dev PC

Thanks, Ken!

In case anyone is interested, the WMI class uses these API calls, which you
can also call directly. The CONSTs of interest are defined in WINSVC.H.

Declare Function OpenSCManager Lib "advapi32.dll" Alias "OpenSCManagerA"
_
(ByVal lpMachineName As String, _
ByVal lpDatabaseName As String, _
ByVal dwDesiredAccess As Integer) _
As Integer

Declare Function OpenService Lib "advapi32.dll" Alias "OpenServiceA" _
(ByVal hSCManager As Integer, _
ByVal lpServiceName As String, _
ByVal dwDesiredAccess As Integer) _
As Integer

Declare Function ChangeServiceConfig Lib "advapi32.dll" Alias
"ChangeServiceConfigA" _
(ByVal hService As Integer, _
ByVal dwServiceType As Integer, _
ByVal dwStartType As Integer, _
ByVal dwErrorControl As Integer, _
ByVal lpBinaryPathName As String, _
ByVal lpLoadOrderGroup As String, _
ByRef lpdwTagId As Integer, _
ByVal lpDependencies As String, _
ByVal lpServiceStartName As String, _
ByVal lpPassword As String, _
ByVal lpDisplayName As String) _
As Integer
 
F

FB's .NET Dev PC

Many thanks, Alex.

I found this one

BOOL InitiateSystemShutdown(
LPTSTR lpMachineName,
LPTSTR lpMessage,
DWORD dwTimeout,
BOOL bForceAppsClosed,
BOOL bRebootAfterShutdown
);

which takes a Force Apps Closed parameter.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top