PC Review


Reply
Thread Tools Rate Thread

How to change the Startup Mode of a Service?

 
 
FB's .NET Dev PC
Guest
Posts: n/a
 
      27th Oct 2004
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")


 
Reply With Quote
 
 
 
 
Ken Tucker [MVP]
Guest
Posts: n/a
 
      28th Oct 2004
Hi,

Invoke the win32_service wmi classes changestartmode method.

http://msdn.microsoft.com/library/de...32_service.asp

Ken
------------------
"FB's .NET Dev PC" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
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")



 
Reply With Quote
 
Alex Clark
Guest
Posts: n/a
 
      28th Oct 2004
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


> 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")
>




 
Reply With Quote
 
FB's .NET Dev PC
Guest
Posts: n/a
 
      29th Oct 2004
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



"Ken Tucker [MVP]" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Hi,
>
> Invoke the win32_service wmi classes changestartmode method.
>
> http://msdn.microsoft.com/library/de...32_service.asp
>
> Ken
> ------------------
> "FB's .NET Dev PC" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> 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")
>
>
>



 
Reply With Quote
 
FB's .NET Dev PC
Guest
Posts: n/a
 
      29th Oct 2004
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.


"Alex Clark" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>
>> 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")
>>

>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
configure service startup mode to Manual kamesh Windows XP 2 22nd Jun 2005 09:13 AM
How to change the Startup Type of a Service with VB.NET? DraguVaso Microsoft Dot NET 3 14th Feb 2005 05:41 PM
How to change the Startup Type of a Service with VB.NET? DraguVaso Microsoft Dot NET Framework 3 14th Feb 2005 05:41 PM
VB script to change service startup Patrick Microsoft Windows 2000 Registry 3 15th Jan 2004 03:28 AM
VB script to change service startup Patrick Microsoft Windows 2000 Registry Archive 0 15th Jan 2004 01:49 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:03 AM.