Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shutdown Windows 1

Status
Not open for further replies.

NiteCrawlr

Programmer
Mar 16, 2001
140
BR
Hi, :)
I would like to know if someone could help me with my problem.
I need to make a vb app to shutdown the Windows just by pushing a button or by selecting from a menu....

Thanks in advance,

Frederico
 
Hi,

Try this


------------------------------------------------------------
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long

Public Const EXIT_LOGOFF = 0
Public Const EXIT_SHUTDOWN = 1
Public Const EXIT_REBOOT = 2


Private Sub Command1_Click()
Call ExitWindowsEx(EXIT_SHUTDOWN, 0)
End Sub
------------------------------------------------------------


Sunaj
 
Shut down Windows... either Win9x or NT:
[tt]
Public Const EWX_LOGOFF = 0
Public Const EWX_SHUTDOWN = 1
Public Const EWX_REBOOT = 2
Public Const EWX_FORCE = 4
Public Const TOKEN_ADJUST_PRIVILEGES = &H20
Public Const TOKEN_QUERY = &H8
Public Const SE_PRIVILEGE_ENABLED = &H2
Public Const ANYSIZE_ARRAY = 1
Public 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

Private Declare Function GetCurrentProcess Lib _
"kernel32.dll" () As Long
Private Declare Function OpenProcessToken Lib _
"advapi32.dll" (ByVal ProcessHandle As Long, _
ByVal DesiredAccess As Long, TokenHandle As Long) _
As Long
Private Declare Function LookupPrivilegeValue Lib _
"advapi32.dll" Alias "LookupPrivilegeValueA" _
(ByVal lpSystemName As String, ByVal lpName As String, _
lpLuid As LUID) As Long
Private Declare Function AdjustTokenPrivileges Lib _
"advapi32.dll" (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.dll" (ByVal uFlags As Long, _
ByVal dwReserved As Long) As Long
Private Declare Function GetVersionEx Lib _
"kernel32.dll" Alias "GetVersionExA" _
(ByRef lpVersionInformation As OSVERSIONINFO) As Long


Public Function IsWinNT() As Boolean
Dim myOS As OSVERSIONINFO
myOS.dwOSVersionInfoSize = Len(myOS)
GetVersionEx myOS
IsWinNT = (myOS.dwPlatformId = VER_PLATFORM_WIN32_NT)
End Function

Private Sub EnableShutDown()
Dim hProc As Long
Dim hToken As Long
Dim mLUID As LUID
Dim mPriv As TOKEN_PRIVILEGES
Dim 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[/tt]

Form level....
[tt]
Private Sub Form_Load()
Command1.Caption = "Shut Down"
Command2.Caption = "Reboot"
Command3.Caption = "Log Off"
End Sub

Private Sub Command1_Click()
'SHUTDOWN THE COMPUTER
Dim ret As Long
Dim Flags As Long
Flags = EWX_SHUTDOWN
Force = True '[/tt]for example[tt]
If Force Then Flags = Flags + EWX_FORCE
If IsWinNT Then EnableShutDown
ExitWindowsEx Flags, 0
End Sub

Private Sub Command2_Click()
'REBOOT THE COMPUTER
Dim ret As Long
Dim Flags As Long
Flags = EWX_REBOOT
If Force Then Flags = Flags + EWX_FORCE
If IsWinNT Then EnableShutDown
ExitWindowsEx Flags, 0
End Sub

Private Sub Command3_Click()
'LOGOFF CURRENT USER
Dim ret As Long
Dim Flags As Long
Flags = EWX_LOGOFF
If Force Then Flags = Flags + EWX_FORCE
ExitWindowsEx Flags, 0
End Sub
[/tt]
VCA.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top