Ovo stavis u Modul:
Code:
Option Explicit
Public Declare Function ExitWindowsEx Lib "user32" (ByVal dwOptions As Long, ByVal dwReserved As Long) As Long
Public Declare Function GetCurrentProcess Lib "kernel32" () As Long
Public Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Public Declare Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
Public Declare Function AdjustTokenPrivileges Lib "advapi32" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As Any, ReturnLength As Long) As Long
Public Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long
Public Declare Function LockWorkStation Lib "user32.dll" () As Long
Public Const TOKEN_ADJUST_PRIVILEGES As Long = &H20
Public Const TOKEN_QUERY As Long = &H8
Public Const SE_PRIVILEGE_ENABLED As Long = &H2
Public Const EWX_LOGOFF As Long = &H0
Public Const EWX_SHUTDOWN As Long = &H1
Public Const EWX_REBOOT As Long = &H2
Public Const EWX_FORCE As Long = &H4
Public Const EWX_POWEROFF As Long = &H8
Public Const VER_PLATFORM_WIN32_NT As Long = 2
Public Type OSVERSIONINFO
OSVSize As Long
dwVerMajor As Long
dwVerMinor As Long
dwBuildNumber As Long
PlatformID As Long
szCSDVersion As String * 128
End Type
Public Type LUID
dwLowPart As Long
dwHighPart As Long
End Type
Public Type LUID_AND_ATTRIBUTES
udtLUID As LUID
dwAttributes As Long
End Type
Public Type TOKEN_PRIVILEGES
PrivilegeCount As Long
laa As LUID_AND_ATTRIBUTES
End Type
Public Success As Long
Public Function IsWinNTPlus() As Boolean
#If Win32 Then
Dim OSV As OSVERSIONINFO
OSV.OSVSize = Len(OSV)
If GetVersionEx(OSV) = 1 Then
IsWinNTPlus = (OSV.PlatformID = VER_PLATFORM_WIN32_NT) And _
(OSV.dwVerMajor >= 4)
End If
#End If
End Function
Public Function EnableShutdownPrivledges() As Boolean
Dim hProcessHandle As Long
Dim hTokenHandle As Long
Dim lpv_la As LUID
Dim token As TOKEN_PRIVILEGES
hProcessHandle = GetCurrentProcess()
If hProcessHandle <> 0 Then
If OpenProcessToken(hProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hTokenHandle) <> 0 Then
If LookupPrivilegeValue(vbNullString, "SeShutdownPrivilege", lpv_la) <> 0 Then
With token
.PrivilegeCount = 1
.laa.udtLUID = lpv_la
.laa.dwAttributes = SE_PRIVILEGE_ENABLED
End With
If AdjustTokenPrivileges(hTokenHandle, False, token, ByVal 0&, ByVal 0&, ByVal 0&) <> 0 Then
EnableShutdownPrivledges = True
End If
End If
End If
End If
End Function
A ovo u Form:
Code:
Private Sub Command1_Click()
If IsWinNTPlus() Then
Success = EnableShutdownPrivledges()
If Success Then
Call ExitWindowsEx(EWX_SHUTDOWN, 0&)
Else
Call ExitWindowsEx(EWX_SHUTDOWN, 0&)
End If
End If
End Sub
Private Sub Command2_Click()
If IsWinNTPlus() Then
Success = EnableShutdownPrivledges()
If Success Then
Call ExitWindowsEx(EWX_REBOOT, 0&)
Else
Call ExitWindowsEx(EWX_REBOOT, 0&)
End If
End If
End Sub
Private Sub Command3_Click()
If IsWinNTPlus() Then
Success = EnableShutdownPrivledges()
If Success Then
Call ExitWindowsEx(EWX_LOGOFF, 0&)
Else
Call ExitWindowsEx(EWX_LOGOFF, 0&)
End If
End If
End Sub
Moze li neko da okaci primer za Windows 7?