473,223 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,223 software developers and data experts.

Windows Service - Shutdown Windows

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(ServicesToRu n)

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
Nov 21 '05 #1
2 1765
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
Nov 21 '05 #2
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
"Dragon" <no@spam.please> wrote in message
news:uG**************@TK2MSFTNGP14.phx.gbl...
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

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bill Sonia | last post by:
I'm written a Windows Service to send e-mails on events like OnStart, OnStop, OnShutDown using System.Web.Mail. It works for everything but OnShutdown. My guess is that once my send mail code is...
8
by: Bill Sonia | last post by:
I've written a Windows Service to send e-mails on events like OnStart, OnStop, OnShutDown using System.Web.Mail. It works for everything but OnShutdown. My guess is that for OnShutDown, once my...
6
by: carbon_dragon | last post by:
Ok, so here is the problem. I'm working on a headless server program implemented as a .NET C# Console project. There is a UPS mounted to this server (though not a windows compliant UPS). I can only...
4
by: Bill Sonia | last post by:
I'm written a Windows Service to send e-mails on events like OnStart, OnStop, OnShutDown using System.Web.Mail. It works for everything but OnShutdown. My guess is that once my send mail code is...
0
by: Stefan Krah | last post by:
Hello, I'm trying to run a Python script as a Windows service with a defined shutdown. The script (enigma-client.py) handles the communications with the server in a distributed computing effort...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.