Этот пример я нашёл тут на вашем сайте, всё отлично работает под Win98/Me, а мне к сожалению нужно выключить комп в Win2k/WinXP как бы это сделать и что нужно переделать или легче написать новый код??
Пример : под Win98/Me Выглядит так:
'КОДФОРМЫ
Private Sub mnuCompOff_Click() Call ShutDown(EWX_SHUTDOWN) End Sub
Private Sub mnuCompReboot_Click() Call ShutDown(EWX_REBOOT) End Sub
Public Enum TokenRights TOKEN_ASSIGN_PRIMARY = &H1 TOKEN_DUPLICATE = &H2 TOKEN_IMPERSONATE = &H4 TOKEN_QUERY = &H8 TOKEN_QUERY_SOURCE = &H10 TOKEN_ADJUST_PRIVILEGES = &H20 TOKEN_ADJUST_GROUPS = &H40 TOKEN_ADJUST_DEFAULT = &H80 TOKEN_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or TOKEN_ASSIGN_PRIMARY Or TOKEN_DUPLICATE Or TOKEN_IMPERSONATE Or TOKEN_QUERY Or TOKEN_QUERY_SOURCE Or TOKEN_ADJUST_PRIVILEGES Or TOKEN_ADJUST_GROUPS Or TOKEN_ADJUST_DEFAULT) TOKEN_READ = (STANDARD_RIGHTS_READ Or TOKEN_QUERY) TOKEN_WRITE = (STANDARD_RIGHTS_WRITE Or TOKEN_ADJUST_PRIVILEGES Or TOKEN_ADJUST_GROUPS Or TOKEN_ADJUST_DEFAULT) TOKEN_EXECUTE = (STANDARD_RIGHTS_EXECUTE) End Enum
Public Enum PrivilegeAttributes SE_PRIVILEGE_ENABLED_BY_DEFAULT = &H1 SE_PRIVILEGE_ENABLED = &H2 SE_PRIVILEGE_USED_FOR_ACCESS = &H80000000 End Enum
Public Enum ExitOptions EWX_LOGOFF = 0 EWX_SHUTDOWN = 1 EWX_REBOOT = 2 EWX_FORCE = 4 End Enum
Type LUID lowPart As Long HighPart As Long End Type
Type LUID_AND_ATTRIBUTES pLuid As LUID Attributes As PrivilegeAttributes End Type
Type PTOKEN_PRIVILEGES PrivilegeCount As Long Privileges(0) As LUID_AND_ATTRIBUTES End Type
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long Private Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As TokenRights, ByRef TokenHandle As Long) As Long Private Declare Function LookupPrivilegeValueA Lib "advapi32" (ByVal lpSystemName As String, ByVal lpName As String, ByRef lpLuid As LUID) As Long Private Declare Function AdjustTokenPrivileges Lib "advapi32" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, ByRef NewState As PTOKEN_PRIVILEGES, ByVal BufferLength As Long, ByRef PreviousState As Long, ByRef ReturnLenght As Long) As Long Private Declare Function AdjustTokenPrivilegesOld Lib "advapi32" Alias "AdjustTokenPrivileges" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, ByRef NewState As PTOKEN_PRIVILEGES, ByVal BufferLength As Long, ByRef PreviousState As PTOKEN_PRIVILEGES, ByRef ReturnLenght As Long) As Long Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As ExitOptions, ByVal dwReserved As Long) As Long
Public Function ShutDown(Operation As ExitOptions) As Long Dim lngProcess As Long Dim lngReturn As Long Dim lngToken As Long Dim udtLUID As LUID Dim lngTokenPrivileges As TokenRights Dim udtTokenPrivNew As PTOKEN_PRIVILEGES lngProcess = GetCurrentProcess() lngTokenPrivileges = TOKEN_ADJUST_PRIVILEGES lngReturn = OpenProcessToken(lngProcess, lngTokenPrivileges, lngToken) lngReturn = LookupPrivilegeValueA(vbNullString, "SE_SHUTDOWN_NAME", udtLUID) udtTokenPrivNew.PrivilegeCount = 1 udtTokenPrivNew.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED udtTokenPrivNew.Privileges(0).pLuid = udtLUID lngReturn = AdjustTokenPrivileges(lngToken, 0, udtTokenPrivNew, 0&, 0, 0&) ShutDown = ExitWindowsEx(Operation, 0) End Function
Private Const EWX_OFF = 8 Private Const EWX_LOGOFF = 0 Private Const EWX_SHUTDOWN = 1 Private Const EWX_REBOOT = 2 Private Const EWX_FORCE = 4 Private Const TOKEN_ADJUST_PRIVILEGES = &H20 Private Const TOKEN_QUERY = &H8 Private Const SE_PRIVILEGE_ENABLED = &H2 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 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(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES End Type 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 'Detect if the program is running under Windows NT Public Function IsWinNT() As Boolean Dim myOS As OSVERSIONINFO myOS.dwOSVersionInfoSize = Len(myOS) GetVersionEx myOS IsWinNT = (myOS.dwPlatformId = VER_PLATFORM_WIN32_NT) End Function 'set the shut down privilege for the current application 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 ' Shut Down NT Public Sub ShutDownNT(Force As Boolean) Dim ret As Long Dim Flags As Long Flags = EWX_SHUTDOWN If Force Then Flags = Flags + EWX_FORCE If IsWinNT Then EnableShutDown ExitWindowsEx Flags Or EWX_OFF, 0 End Sub 'Restart NT Public Sub RebootNT(Force As Boolean) 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 'Log off the current user Public Sub LogOffNT(Force As Boolean) Dim ret As Long Dim Flags As Long Flags = EWX_LOGOFF If Force Then Flags = Flags + EWX_FORCE ExitWindowsEx Flags, 0 End Sub
Padre ты бы не мог прокомментировать свой ответ и вооще пояснить в чём фишка отличий программирования для Win2K/NT/XP .Плыыз. Это для того чтобы и я мог делать это сам а не попрошайничать тут. Заранее спасибо
Фишка в том, что при выключении компьютера под ХР надо иметь на это необходимую привелегию, которая по умолчанию выключена даже у администратора, и вся эта дополнительная галиматья в коде как раз и призвана для получения твоей программой необходимой привелегии.