Windows Service - Shutdown Windows

M

Mr Newbie

I have an annoying teenager who wont go to bed because he is allways on his
computer. I want to write a windows service which will shut down his
computer at night and not allow this to be restarted until the morning.

So I have decided to try and write a windows service to do this. Stage one
is to see if I can get the damn thing to shutdown so I managed to get it
installed but it wont shutdown the PC. I tried the same code in a windows
forms app and it looked as if it was going to then stopped.

Any ideas ?

Inherits System.ServiceProcess.ServiceBase

' Constants

Const SE_PRIVILEGE_ENABLED As Integer = &H2

Const TOKEN_QUERY As Integer = &H8

Const TOKEN_ADJUST_PRIVILEGES As Integer = &H20

Const SE_SHUTDOWN_NAME As String = "SeShutdownPrivilege"

' Exit Windows Constants

Const EWX_LOGOFF As Integer = &H0

Const EWX_SHUTDOWN As Integer = &H1

Const EWX_REBOOT As Integer = &H2

Const EWX_FORCE As Integer = &H4

Const EWX_POWEROFF As Integer = &H8

Const EWX_FORCEIFHUNG As Integer = &H10

Private Declare Function ExitWindows _

Lib "User32" Alias "ExitWindowsEx" _

(ByVal dwOptions As Long, ByVal dwReserved As Long) As Long

Private Sub DoExitWindows(ByVal flg As Integer)

'Exit Windows

ExitWindows(flg, 0)

End Sub





#Region " Component Designer generated code "

Public Sub New()

MyBase.New()

' This call is required by the Component Designer.

InitializeComponent()

' Add any initialization after the InitializeComponent() call

End Sub

'UserService overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

' The main entry point for the process

<MTAThread()> _

Shared Sub Main()

Dim ServicesToRun() As System.ServiceProcess.ServiceBase

' More than one NT Service may run within the same process. To add

' another service to this process, change the following line to

' create a second service object. For example,

'

' ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1,
New MySecondUserService}

'

ServicesToRun = New System.ServiceProcess.ServiceBase() {New Service1}

System.ServiceProcess.ServiceBase.Run(ServicesToRun)

End Sub

'Required by the Component Designer

Private components As System.ComponentModel.IContainer

' NOTE: The following procedure is required by the Component Designer

' It can be modified using the Component Designer.

' Do not modify it using the code editor.

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

components = New System.ComponentModel.Container

Me.ServiceName = "Service1"

End Sub

#End Region

Protected Overrides Sub OnStart(ByVal args() As String)

' Add code here to start your service. This method should set things

' in motion so your service can do its work.

' Shutdown

DoExitWindows(EWX_SHUTDOWN And EWX_FORCE And EWX_POWEROFF And
EWX_FORCEIFHUNG And EWX_LOGOFF)





End Sub
 
D

Dragon

Hi,

You should correct your declaration:


~
Private Declare Function ExitWindowsEx Lib "user32.dll" ( _
ByVal uFlags As Integer, _
ByVal dwReason As Integer, _
) As Boolean

REM Analogue of ExitWindows macro:
Private Function ExitWindows(ByVal dwReserved As Integer, ByVal
uReserver As Integer) As Boolean
ExitWindowsEx(EWX_LOGOFF, &HFFFFFFFF)
End Function

~

BTW,
When using this function, you should place Or instead of And if you
combine flags.

Roman
 
M

Mr Newbie

Thanks, I seem to be having a really bad day today, I put it down to an
Indian meal I had last night, I really cannot think straight at all.

Anyway, much thanks for your help.


Regards
 

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