473,378 Members | 1,404 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,378 software developers and data experts.

shutdown

hallow all

earlier i posted a question about shutingdown a computer.
there was someone who helped me with the code? i don't know anymore who he
was.
i remember there was an error in the code, and i had to add a couple of
lines.
i've forgot to save this fixed module meightbe you can help me again giving
me the code to add.

here is the code:(not working)

Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function OpenProcessToken Lib "advapi32" ( _
ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, _
TokenHandle As Long) As Long
Private Declare Function LookupPrivilegeValue Lib "advapi32" _
Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, _
ByVal lpName As String, lpLuid As LUID) As Long
Private Declare Function AdjustTokenPrivileges Lib "advapi32" ( _
ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, _
NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, _
PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As _
Long, ByVal dwReserved As Long) As Long
Private Declare Function GetVersionEx Lib "kernel32" Alias _
"GetVersionExA" (ByRef lpVersionInformation As OSVERSIONINFO) _
As Long

Public Const EWX_LOGOFF = 0
Public Const EWX_SHUTDOWN = 1
Public Const EWX_REBOOT = 2
Public Const EWX_FORCE = 4
Public Const EWX_POWEROFF = 8
Public Const EWX_FORCEIFHUNG = 16

Private Const TOKEN_QUERY = 8
Private Const TOKEN_ADJUST_PRIVILEGES = 32

Private Const SE_PRIVILEGE_ENABLED = 2

Private Const ANYSIZE_ARRAY = 1
Private Const VER_PLATFORM_WIN32_NT = 2

Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
End Type

Public Type LUID
LowPart As Long
HighPart As Long
End Type

Public Type LUID_AND_ATTRIBUTES
pLuid As LUID
Attributes As Long
End Type

Public Type TOKEN_PRIVILEGES
PrivilegeCount As Long
Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES
End Type

Public Sub quitWindows(which As Long)
Dim n As Long

If IsWinNT Then EnableShutDown
n = ExitWindowsEx(which, &HFFFF) '((which Or EWX_FORCE), &HFFFF)
If n Then
Dim x As String * 256
FormatMessage FORMAT_MESSAGE_FROM_SYSTEM, 0, Err.LastDllError, _
0, x, 256, 0
Else
Unload frmBackground
End If
End Sub

Public Function IsWinNT() As Boolean
' Detect if the program is running under Windows NT
Dim myOS As OSVERSIONINFO

myOS.dwOSVersionInfoSize = Len(myOS)
GetVersionEx myOS
IsWinNT = (myOS.dwPlatformId = VER_PLATFORM_WIN32_NT)
End Function

Private Sub EnableShutDown()
' Set the shut down privilege for the current application
Dim hProc As Long, hToken As Long, mLUID As LUID
Dim mPriv As TOKEN_PRIVILEGES, mNewPriv As TOKEN_PRIVILEGES

hProc = GetCurrentProcess()
OpenProcessToken hProc, TOKEN_ADJUST_PRIVILEGES + TOKEN_QUERY, _
hToken
LookupPrivilegeValue "", "SeShutdownPrivilege", mLUID
mPriv.PrivilegeCount = 1
mPriv.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
mPriv.Privileges(0).pLuid = mLUID

' enable shutdown privilege for the current application
AdjustTokenPrivileges hToken, False, mPriv, 4 + (12 * _
mPriv.PrivilegeCount), mNewPriv, 4 + (12 * _
mNewPriv.PrivilegeCount)
End Sub

kind regards Maarten
Jul 17 '05 #1
1 3617
thats a lot of code to shutdown a machine.

here is some code for you, this does a few things
no 1 it makes the program a service so you wont be able to see it by
pressing cntrl-alt-del
no 2 if the username and password dont match then it shuts down the computer
it is a little app i wrote to stop others using my pc
you going to have to add a few controls to a form............i let u work
that out

**************************** start of code
Option Explicit
Const EWX_LOGOFF = 0
Const EWX_SHUTDOWN = 1
Const EWX_REBOOT = 2
Const EWX_FORCE = 4
Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2
Const SWP_NOACTIVATE = &H10
Const SWP_SHOWWINDOW = &H40
Private Declare Sub SetWindowPos Lib "User32" (ByVal hWnd As Long, ByVal
hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long,
ByVal cy As Long, ByVal wFlags As Long)
Private Declare Function ExitWindowsEx Lib "User32" (ByVal uFlags As
Long, ByVal dwReserved As Long) As Long
Dim iTry As Integer
Dim lTime As Long
Dim lTimeLenth As Long
Dim strRet As String
Dim regserv
Const RSP_SIMPLE_SERVICE = 1
Const RSP_UNREGISTER_SERVICE = 0
Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long
Private Declare Function RegisterServiceProcess Lib "kernel32" (ByVal
dwProcessID As Long, ByVal dwType As Long) As Long
Public Sub MakeMeService()
Dim pid As Long, reserv As Long
pid = GetCurrentProcessId()
regserv = RegisterServiceProcess(pid, RSP_SIMPLE_SERVICE)
End Sub

Public Sub UnMakeMeService()
Dim pid As Long, reserv As Long
pid = GetCurrentProcessId()
regserv = RegisterServiceProcess(pid, RSP_UNREGISTER_SERVICE)
End Sub

Private Sub cmdCancel_Click()
ShutDown
End Sub

Private Sub cmdOK_Click()
If iTry > 2 Then
ShutDown
End If
If LCase(txtUsername) = "my user name" Then
If LCase(txtPassword.Text) = "my password" Then
Unload Me
End
End If
End If
iTry = iTry + 1
End Sub

Private Sub Form_Activate()
SetWindowPos Me.hWnd, HWND_TOPMOST, 500, 0, 0, 0, SWP_NOACTIVATE Or
SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
lTimeLenth = 2500
progTime.Max = lTimeLenth
End Sub

Private Sub Form_Load()
MakeMeService
End Sub

Private Sub Form_Unload(Cancel As Integer)
UnMakeMeService
End Sub

Private Sub tmrMain_Timer()
lTime = lTime + 1
progTime.Value = (lTimeLenth - lTime)
lblTime.Caption = (lTimeLenth - lTime)
Me.Refresh
DoEvents
If lTime = lTimeLenth Then
ShutDown
End If
End Sub

Private Sub ShutDown()
strRet = ExitWindowsEx(EWX_FORCE Or EWX_SHUTDOWN, 0)
End Sub

**************************** end of code

"Maarten" <gu******@hotmail.com> wrote in message
news:41***********************@news.skynet.be...
hallow all

earlier i posted a question about shutingdown a computer.
there was someone who helped me with the code? i don't know anymore who he was.
i remember there was an error in the code, and i had to add a couple of
lines.
i've forgot to save this fixed module meightbe you can help me again giving me the code to add.

here is the code:(not working)

Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function OpenProcessToken Lib "advapi32" ( _
ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, _
TokenHandle As Long) As Long
Private Declare Function LookupPrivilegeValue Lib "advapi32" _
Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, _
ByVal lpName As String, lpLuid As LUID) As Long
Private Declare Function AdjustTokenPrivileges Lib "advapi32" ( _
ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, _
NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, _
PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As _
Long, ByVal dwReserved As Long) As Long
Private Declare Function GetVersionEx Lib "kernel32" Alias _
"GetVersionExA" (ByRef lpVersionInformation As OSVERSIONINFO) _
As Long

Public Const EWX_LOGOFF = 0
Public Const EWX_SHUTDOWN = 1
Public Const EWX_REBOOT = 2
Public Const EWX_FORCE = 4
Public Const EWX_POWEROFF = 8
Public Const EWX_FORCEIFHUNG = 16

Private Const TOKEN_QUERY = 8
Private Const TOKEN_ADJUST_PRIVILEGES = 32

Private Const SE_PRIVILEGE_ENABLED = 2

Private Const ANYSIZE_ARRAY = 1
Private Const VER_PLATFORM_WIN32_NT = 2

Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
End Type

Public Type LUID
LowPart As Long
HighPart As Long
End Type

Public Type LUID_AND_ATTRIBUTES
pLuid As LUID
Attributes As Long
End Type

Public Type TOKEN_PRIVILEGES
PrivilegeCount As Long
Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES
End Type

Public Sub quitWindows(which As Long)
Dim n As Long

If IsWinNT Then EnableShutDown
n = ExitWindowsEx(which, &HFFFF) '((which Or EWX_FORCE), &HFFFF)
If n Then
Dim x As String * 256
FormatMessage FORMAT_MESSAGE_FROM_SYSTEM, 0, Err.LastDllError, _
0, x, 256, 0
Else
Unload frmBackground
End If
End Sub

Public Function IsWinNT() As Boolean
' Detect if the program is running under Windows NT
Dim myOS As OSVERSIONINFO

myOS.dwOSVersionInfoSize = Len(myOS)
GetVersionEx myOS
IsWinNT = (myOS.dwPlatformId = VER_PLATFORM_WIN32_NT)
End Function

Private Sub EnableShutDown()
' Set the shut down privilege for the current application
Dim hProc As Long, hToken As Long, mLUID As LUID
Dim mPriv As TOKEN_PRIVILEGES, mNewPriv As TOKEN_PRIVILEGES

hProc = GetCurrentProcess()
OpenProcessToken hProc, TOKEN_ADJUST_PRIVILEGES + TOKEN_QUERY, _
hToken
LookupPrivilegeValue "", "SeShutdownPrivilege", mLUID
mPriv.PrivilegeCount = 1
mPriv.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
mPriv.Privileges(0).pLuid = mLUID

' enable shutdown privilege for the current application
AdjustTokenPrivileges hToken, False, mPriv, 4 + (12 * _
mPriv.PrivilegeCount), mNewPriv, 4 + (12 * _
mNewPriv.PrivilegeCount)
End Sub

kind regards Maarten

Jul 17 '05 #2

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

Similar topics

8
by: Jonathan Heath | last post by:
Hi all, I have created an ASP script that enters data into an Access Database. My problem is that I'd like this script to run when the computer is shutdown or the user logs off. I think...
0
by: Allan Bredahl | last post by:
Hi All I am trying to construct an application that is able to cancel a machine shutdown, reboot or logoff. And after performing some stuff to perform the original shutdown order :...
3
by: Senthil | last post by:
Hi, I would like to know the code for reboot and shutdown windows xp professional using VB.Net. thanx Senthil
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...
4
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed...
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...
2
by: Brian Worth | last post by:
I have just upgraded from VB 4.0 to VB .NET 2002. One program under VB 4.0 was able to shut down or restart the (windows XP) machine using a series of API calls. (Getlasterror, GetCurrentProcess,...
1
by: Titeuf | last post by:
Hi, I work under VS2003 and I want to know how get the shutdown reason code ? On msdn nothing found...No api :( Have you an idea ? Thank's
5
by: Phil Tusa | last post by:
Greetings to all .... I have a need to issue a shutdown and/or Restart Windows XP inside my application. Any help or example code would be appreciated! -- Phil
3
by: IdleBrain | last post by:
Gurus, I am trying to delay Windows Shutdown/Restart to perfrom cleanup and I am using the following code: protected override void WndProc(ref Message ex)
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.