-----Original Message-----
After a simple search in Google I found this page at the
top of the results
http://beta.experts-
exchange.com/Programming/Programming_Languages/Visual_Bas ic
/Q_10162978.html
which contains the following code
Public Const VER_PLATFORM_WIN32_NT = 2
Public Const EWX_LOGOFF = 0
Public Const EWX_SHUTDOWN = 1
Public Const EWX_REBOOT = 2
Public Const EWX_FORCE = 4
Public Const CCDEVICENAME = 32
Public Const CCFORMNAME = 32
Public Const DM_BITSPERPEL = &H40000
Public Const DM_PELSWIDTH = &H80000
Public Const DM_PELSHEIGHT = &H100000
Public Const CDS_UPDATEREGISTRY = &H1
Public Const CDS_TEST = &H4
Public Const DISP_CHANGE_SUCCESSFUL = 0
Public Const DISP_CHANGE_RESTART = 1
Public Const ERROR_NOT_ALL_ASSIGNED = 1300
Public Const SE_PRIVILEGE_ENABLED = 2
Public Const TOKEN_QUERY = &H8
Public Const TOKEN_ADJUST_PRIVILEGES = &H20
Type DEVMODE
dmDeviceName As String * CCDEVICENAME
dmSpecVersion As Integer
dmDriverVersion As Integer
dmSize As Integer
dmDriverExtra As Integer
dmFields As Long
dmOrientation As Integer
dmPaperSize As Integer
dmPaperLength As Integer
dmPaperWidth As Integer
dmScale As Integer
dmCopies As Integer
dmDefaultSource As Integer
dmPrintQuality As Integer
dmColor As Integer
dmDuplex As Integer
dmYResolution As Integer
dmTTOption As Integer
dmCollate As Integer
dmFormName As String * CCFORMNAME
dmUnusedPadding As Integer
dmBitsPerPel As Integer
dmPelsWidth As Long
dmPelsHeight As Long
dmDisplayFlags As Long
dmDisplayFrequency As Long
End Type
Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
End Type
Type LUID
lowpart As Long
highpart As Long
End Type
Type LUID_AND_ATTRIBUTES
pLuid As LUID
Attributes As Long
End Type
Type TOKEN_PRIVILEGES
PrivilegeCount As Long
Privileges As LUID_AND_ATTRIBUTES
End Type
Declare Function GetVersionEx Lib "kernel32"
Alias "GetVersionExA" _
(lpVersionInformation As OSVERSIONINFO) As Long
Declare Function EnumDisplaySettings Lib "user32"
Alias "EnumDisplaySettingsA" _
(ByVal lpszDeviceName As Long, ByVal iModeNum As
Long, _
lpDevMode As Any) As Boolean
Declare Function ChangeDisplaySettings Lib "user32"
Alias "ChangeDisplaySettingsA" _
(lpDevMode As Any, ByVal dwFlags As Long) As Long
Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags
As Long, _
ByVal dwReserved As Long) As Long
Declare Function AdjustTokenPrivileges Lib "advapi32.dll"
(ByVal TokenHandle As Long, _
ByVal DisableAllPriv As Long, NewState As
TOKEN_PRIVILEGES, _
ByVal BufferLength As Long, PreviousState As
TOKEN_PRIVILEGES, _
ReturnLength As Long) As Long
Declare Function LookupPrivilegeValue Lib "advapi32.dll"
Alias "LookupPrivilegeValueA" _
(ByVal lpSystemName As Any, ByVal lpName As
String, lpUid As LUID) As Long
Declare Function OpenProcessToken Lib "advapi32.dll"
(ByVal ProcessHandle As Long, _
ByVal DesiredAccess As Long, TokenHandle As Long)
As Long
Declare Function GetCurrentProcess Lib "kernel32" () As
Long
Form Code:
Declarations section code:
Dim bWindowsNT As Boolean
Form load code:
Private Sub Form_Load()
Dim OSInfo As OSVERSIONINFO
'
' See if we are running Windows 9x or NT.
'
OSInfo.dwOSVersionInfoSize = Len(OSInfo)
Call GetVersionEx(OSInfo)
bWindowsNT = (OSInfo.dwPlatformId = VER_PLATFORM_WIN32_NT)
End Sub
Add a button to shut down Windows with this code:
Private Sub cmdReboot_Click()
Dim lMode As Long
Dim tLuid As LUID
Dim tTokenPriv As TOKEN_PRIVILEGES
Dim tPrevTokenPriv As TOKEN_PRIVILEGES
Dim lResult As Long
Dim lToken As Long
Dim lLenBuffer As Long
'
' Determine the shutdown mode.
'
' EWX_LOGOFF
' Shuts down all processes running and logs off the user.
'
' EWX_REBOOT
' Shuts down and restarts the system.
'
' EWX_SHUTDOWN
' Shuts down the system to a point where it is safe to
turn off the system.
'
' EWX_POWEROFF
' Shuts down the system and turns off power. The system
must support this feature.
'
' EWX_FORCE
' Forcibly shuts down the system. Files are not closed,...
data may be lost.
'
If optShut(0) Then
lMode = EWX_LOGOFF
ElseIf optShut(1) Then
lMode = EWX_REBOOT
ElseIf optShut(2) Then
lMode = EWX_SHUTDOWN
Else
lMode = EWX_FORCE
End If
'
' Execute proper logic depending on the OS.
'
If Not bWindowsNT Then
Call ExitWindowsEx(lMode, 0)
Exit Sub
End if
'
' For NT:
' Get the access token of the current process. Get it
' with the privileges of querying the access token and
' adjusting its privileges.
'
lResult = OpenProcessToken(GetCurrentProcess(), _
TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, lToken)
If lResult = 0 Then Exit Sub 'Failed
'
' Get the locally unique identifier (LUID) which
' represents the shutdown privilege.
'
lResult = LookupPrivilegeValue (0&, "SeShutdownPrivilege",
tLuid)
If lResult = 0 Then Exit Sub 'Failed
'
' Populate the new TOKEN_PRIVILEGES values with the LUID
' and allow your current process to shutdown the computer.
'
With tTokenPriv
.PrivilegeCount = 1
.Privileges.Attributes = SE_PRIVILEGE_ENABLED
.Privileges.pLuid = tLuid
lResult = AdjustTokenPrivileges(lToken, False,
tTokenPriv, _
Len(tPrevTokenPriv), tPrevTokenPriv,
lLenBuffer)
End With
If lResult = 0 Then
Exit Sub 'Failed
Else
If Err.LastDllError = ERROR_NOT_ALL_ASSIGNED Then Exit
Sub 'Failed
End If
'
' Shutdown Windows.
'
Call ExitWindowsEx(lMode, 0)
End Sub
.