473,396 Members | 2,023 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,396 software developers and data experts.

problem shutting systems down when user no logged in

I am trying shut down work stations that are left on over night. This
works great when a user is logged in or the machine is locked but fails
miserably when no body is using the work
station, the programme starts but does not execute. The program below
runs ok right down to shut down call then fails,

any help will be gratefully received
Imports System
Imports System.Runtime.InteropServices

Module Module1
Sub Main()
Shutdown()
End Sub
' 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
'Structure
<StructLayout(LayoutKind.Sequential, Pack:=1)_
Friend Structure Luid
Public Count As Integer
Public Luid As Long
Public Attr As Integer
End Structure 'TokPriv1Luid
' Lock Workstation
<DllImport("user32.dll", ExactSpelling:=True)_
Function LockWorkStation() As Boolean
End Function
' Get Current Processes
<DllImport("kernel32.dll", ExactSpelling:=True)_
Function GetCurrentProcess() As IntPtr
End Function
' Open Process Token
<DllImport("advapi32.dll", SetLastError:=True)_
Function OpenProcessToken(ByVal h As IntPtr, ByVal acc As Integer,
ByRef phtok As IntPtr) As Boolean
End Function
' Look up Priviledge Value
<DllImport("advapi32.dll", SetLastError:=True)_
Friend Function LookupPrivilegeValue(ByVal host As String, ByVal
name As String, ByRef pluid As Long) As Boolean
End Function
' Adjust Token Priviledges
<DllImport("advapi32.dll", ExactSpelling:=True,
SetLastError:=True)_
Friend Function AdjustTokenPrivileges(ByVal htok As IntPtr, ByVal
disall As Boolean, ByRef newst As Luid, ByVal len As Integer, ByVal
prev As IntPtr, ByVal relen As IntPtr) As Boolean
End Function
' Exit Windows
<DllImport("user32.dll", ExactSpelling:=True, SetLastError:=True)>
_
Friend Function ExitWindowsEx(ByVal flg As Integer, ByVal rea As
Integer) As Boolean
End Function
' Exit Windows Sub
Private Sub DoExitWindows(ByVal flg As Integer)
Dim tp As Luid
Dim hproc As IntPtr = GetCurrentProcess()
Dim htok As IntPtr = IntPtr.Zero
OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY,
htok)
tp.Count = 1
tp.Luid = 0
tp.Attr = SE_PRIVILEGE_ENABLED
LookupPrivilegeValue(Nothing, SE_SHUTDOWN_NAME, tp.Luid)
AdjustTokenPrivileges(htok, False, tp, 0, IntPtr.Zero,
IntPtr.Zero)
ExitWindowsEx(flg, 0)
End Sub
' Shutdown
Public Sub Shutdown()
DoExitWindows(EWX_SHUTDOWN Or EWX_FORCE)
End Sub
' Restart
Public Sub Restart()
DoExitWindows(EWX_REBOOT Or EWX_FORCE)
End Sub
' Log off
Public Sub LogOff()
DoExitWindows(EWX_LOGOFF)
End Sub
' Lock Workstation
Public Sub LockTheComputer()
LockWorkStation()
End Sub
End Module

Aug 4 '06 #1
0 1300

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

Similar topics

4
by: Max | last post by:
Hello. This is the first time I've posted to a newsgroup, and I do this because I'm in desperate need of help. I'm working a user management system, and when I activate a user that has registered...
10
by: Jesper | last post by:
Does anyone know some c++ code for shutting down windows? I want to write a program using borland c++ builder to shut down windows. --
3
by: Shailesh S. Gokhale | last post by:
Hi All! Recently I installed mysql on a win98. Some 2 days after the installation the system started experiencing a problem shutting down. When shutting down the hard-disk kept spinning...
1
by: Phoenix | last post by:
Is there any way to disable being able to shut down a program totally? I would have to allow it to be closed when the computer is shutting down or the user is logging out, but other than that it...
5
by: Vishal | last post by:
Hello, I already asked this question in the ASP.NET forums, but no help came. So I am hoping that somebody can help me out. This is really very URGENT me. For my e-commerce application, I...
0
by: Suresh | last post by:
Hi Guys I have Db2 server installed on remote server. i am connecting to that remote server by using VPN. I want to connect that remote DB2 server instance using my local machine DB2...
5
by: Suresh | last post by:
Hi Guys I have Db2 server installed on remote server. i am connecting to that remote server by using VPN. I want to connect that remote DB2 server instance using my local machine DB2...
4
by: nottheface | last post by:
I have a chunk of code I'm writing in a C# code behind. Not sure if this is the right place to post, but I'm hoping someone has come across this before... I have on a page a pair of drop down...
3
by: leocwh | last post by:
Dear all, I would like to know how to run the execuatable before windows shutting down. Here is my simple code: Private Sub Command1_Click() Shell "C:\abc.bat", vbNormalFocus End Sub ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.