borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic ( how to create a menu with the help of win api function without using menu editor ??)Next Topic (How to disabled move the Userform) New Topic New Poll Post Reply
AndreaVB Forum : API : Help w/ SystemShutdown API Call
Poster Message
Killa4Hire_ShoGuN
Level: Guest


icon Help w/ SystemShutdown API Call

The code at AndreaVB.com is inncorrect, me and my VB have taken a look and havn't found good source code, we have the .DLL file so that isn't the problem. Can someone please post a workable snippit or a working model with code (Project - Not EXE).

22-05-2002 at 02:02 AM
| Quote Reply
Claudio
Level: Guest

icon Re: Help w/ SystemShutdown API Call

Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Private Sub Form_Load()
    'KPD-Team 1998
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    msg = MsgBox("This program is going to reboot your computer. Press OK to continue or Cancel to stop.", vbCritical + vbOKCancel + 256, App.Title)
    If msg = vbCancel Then End
    'reboot the computer
    ret& = ExitWindowsEx(EWX_FORCE Or EWX_REBOOT, 0)
End Sub

· uFlags
Specifies the type of shutdown. This parameter must be some combination of the following values:
EWX_FORCE
Forces processes to terminate. When this flag is set, Windows does not send the messages WM_QUERYENDSESSION and WM_ENDSESSION to the applications currently running in the system. This can cause the applications to lose data. Therefore, you should only use this flag in an emergency.
EWX_LOGOFF
Shuts down all processes running in the security context of the process that called the ExitWindowsEx function. Then it logs the user off.
EWX_POWEROFF
Shuts down the system and turns off the power. The system must support the power-off feature.
Windows NT: The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Remarks section.
Windows 95: Security privileges are not supported or required.
EWX_REBOOT
Shuts down the system and then restarts the system.
Windows NT: The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Remarks section.
Windows 95: Security privileges are not supported or required.
EWX_SHUTDOWN
Shuts down the system to a point at which it is safe to turn off the power. All file buffers have been flushed to disk, and all running processes have stopped.
Windows NT: The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Remarks section.
Windows 95: Security privileges are not supported or required.

· dwReserved
Reserved; this parameter is ignored.

09-06-2002 at 07:43 PM
| Quote Reply
Picky
Level: Guest

icon Re: Help w/ SystemShutdown API Call

Try this code from vbCodeLibrary

Option Explicit
    Private Const EWX_LogOff As Long = 0
    Private Const EWX_SHUTDOWN As Long = 1
    Private Const EWX_REBOOT As Long = 2
    Private Const EWX_FORCE As Long = 4
    Private Const EWX_POWEROFF As Long = 8

'The ExitWindowsEx function either logs off, shuts down, or shuts
'down and restarts the system.

Private Declare Function ExitWindowsEx Lib "user32" _
    (ByVal dwOptions As Long, _
    ByVal dwReserved As Long) As Long

'The GetLastError function returns the calling thread's last-error
'code value. The last-error code is maintained on a per-thread basis.
'Multiple threads do not overwrite each other's last-error code.
Private Declare Function GetLastError Lib "kernel32" () As Long

Private Const mlngWindows95 = 0
Private Const mlngWindowsNT = 1
Public glngWhichWindows32 As Long
'The GetVersion function returns the operating system in use.
Private Declare Function GetVersion Lib "kernel32" () As Long

Private Type LUID
    UsedPart As Long
    IgnoredForNowHigh32BitPart As Long
End Type

Private Type LUID_AND_ATTRIBUTES
    TheLuid As LUID
    Attributes As Long
End Type
Private Type TOKEN_PRIVILEGES
    PrivilegeCount As Long
    TheLuid As LUID
    Attributes As Long
End Type
'The GetCurrentProcess function returns a pseudohandle for the
'current process.
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
'The OpenProcessToken function opens the access token associated with
'a process.
Private Declare Function OpenProcessToken Lib "advapi32" _
    (ByVal ProcessHandle As Long, _
    ByVal DesiredAccess As Long, _
    TokenHandle As Long) As Long
'The LookupPrivilegeValue function retrieves the locally unique
'identifier (LUID) used on a specified system to locally represent
'the specified privilege name.
Private Declare Function LookupPrivilegeValue Lib "advapi32" _
    Alias "LookupPrivilegeValueA" _
    (ByVal lpSystemName As String, _
    ByVal lpName As String, _
    lpLuid As LUID) As Long
'The AdjustTokenPrivileges function enables or disables privileges
'in the specified access token. Enabling or disabling privileges
'in an access token requires TOKEN_ADJUST_PRIVILEGES access.
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 Sub SetLastError Lib "kernel32" _
    (ByVal dwErrCode As Long)
Private Sub AdjustToken()
'********************************************************************
'* This procedure sets the proper privileges to allow a log off or a
'* shut down to occur under Windows NT.
'********************************************************************
Const TOKEN_ADJUST_PRIVILEGES = &H20
Const TOKEN_QUERY = &H8
Const SE_PRIVILEGE_ENABLED = &H2
Dim hdlProcessHandle As Long
Dim hdlTokenHandle As Long
Dim tmpLuid As LUID
Dim tkp As TOKEN_PRIVILEGES
Dim tkpNewButIgnored As TOKEN_PRIVILEGES
Dim lBufferNeeded As Long
'Set the error code of the last thread to zero using the
'SetLast Error function. Do this so that the GetLastError
'function does not return a value other than zero for no
'apparent reason.
    SetLastError 0
'Use the GetCurrentProcess function to set the hdlProcessHandle
'variable.
    hdlProcessHandle = GetCurrentProcess()
    If GetLastError <> 0 Then
        systemMessage.AddItem "GetCurrentProcess Error==" & GetLastError
    End If
    OpenProcessToken hdlProcessHandle, _
        (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hdlTokenHandle
    If GetLastError <> 0 Then
        systemMessage.AddItem "OpenProcessToken Error==" & GetLastError
    End If
'Get the LUID for shutdown privilege
    LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
    If GetLastError <> 0 Then
        systemMessage.AddItem "LookupPrivilegeValue Error==" & GetLastError
    End If
    tkp.PrivilegeCount = 1    ' One privilege to set
    tkp.TheLuid = tmpLuid
    tkp.Attributes = SE_PRIVILEGE_ENABLED
'Enable the shutdown privilege in the access token of this process
    AdjustTokenPrivileges hdlTokenHandle, _
    False, _
    tkp, _
    Len(tkpNewButIgnored), _
    tkpNewButIgnored, _
    lBufferNeeded
    If GetLastError <> 0 Then
        systemMessage.AddItem "AdjustTokenPrivileges Error==" & GetLastError
    End If
End Sub
Private Sub cmdLogoff_Click()
    ExitWindowsEx (EWX_LogOff), &HFFFF
    systemMessage.AddItem "ExitWindowsEx's GetLastError " & GetLastError
End Sub
Private Sub cmdForceLogoff_Click()
    ExitWindowsEx (EWX_LogOff Or EWX_FORCE), &HFFFF
    systemMessage.AddItem "ExitWindowsEx's GetLastError " & GetLastError
End Sub
Private Sub cmdShutdown_Click()
    If glngWhichWindows32 = mlngWindowsNT Then
        AdjustToken
        systemMessage.AddItem "Post-AdjustToken GetLastError " & GetLastError
    End If
    ExitWindowsEx (EWX_SHUTDOWN), &HFFFF
    systemMessage.AddItem "ExitWindowsEx's GetLastError " & GetLastError
End Sub
Private Sub cmdForceShutdown_Click()
    If glngWhichWindows32 = mlngWindowsNT Then
        AdjustToken
        systemMessage.AddItem "Post-AdjustToken GetLastError " & GetLastError
    End If
    ExitWindowsEx (EWX_SHUTDOWN Or EWX_FORCE), &HFFFF
    systemMessage.AddItem "ExitWindowsEx's GetLastError " & GetLastError
End Sub
Private Sub exit_Click()
    End
End Sub
Private Sub Form_Load()
'********************************************************************
'* When the project starts, check the operating system used by
'* calling the GetVersion function.
'********************************************************************
Dim lngVersion As Long
    lngVersion = GetVersion()
    If ((lngVersion And &H80000000) = 0) Then
        glngWhichWindows32 = mlngWindowsNT
        systemType.Text = "Windows NT"
    Else
        glngWhichWindows32 = mlngWindows95
        systemType.Text = "Windows 95"
    End If
End Sub

27-08-2002 at 12:06 AM
| Quote Reply
AndreaVB Forum : API : Help w/ SystemShutdown API Call
Previous Topic ( how to create a menu with the help of win api function without using menu editor ??)Next Topic (How to disabled move the Userform) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder