Results 1 to 18 of 18

Thread: How do i write a code to.....

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2006
    Posts
    11

    How do i write a code to.....

    How do i write a code to close down the computer automatically? This is becuase i wanna write a program like when someone click the command button, then the window will close down by itself. I make it for my game project ....

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Re: How do i write a code to.....

    So, do you want to close the window, or the entire machine?
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    Re: How do i write a code to.....

    This code should help you...

    VB Code:
    1. End Type
    2. Private Type Luid
    3.     lowpart As Long
    4.     highpart As Long
    5. End Type
    6. Private Type LUID_AND_ATTRIBUTES
    7.     'pLuid As Luid
    8.     pLuid As LARGE_INTEGER
    9.     Attributes As Long
    10. End Type
    11. Private Type TOKEN_PRIVILEGES
    12.     PrivilegeCount As Long
    13.     Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES
    14. End Type
    15. Private Declare Function InitiateSystemShutdown Lib "advapi32.dll" Alias "InitiateSystemShutdownA" (ByVal lpMachineName As String, ByVal lpMessage As String, ByVal dwTimeout As Long, ByVal bForceAppsClosed As Long, ByVal bRebootAfterShutdown As Long) As Long
    16. Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
    17. Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
    18. Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LARGE_INTEGER) As Long
    19. 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
    20. Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    21. Private Declare Function GetLastError Lib "kernel32" () As Long
    22. Public Function InitiateShutdownMachine(ByVal Machine As String, Optional Force As Variant, Optional Restart As Variant, Optional AllowLocalShutdown As Variant, Optional Delay As Variant, Optional Message As Variant) As Boolean
    23.     Dim hProc As Long
    24.     Dim OldTokenStuff As TOKEN_PRIVILEGES
    25.     Dim OldTokenStuffLen As Long
    26.     Dim NewTokenStuff As TOKEN_PRIVILEGES
    27.     Dim NewTokenStuffLen As Long
    28.     Dim pSize As Long
    29.     If IsMissing(Force) Then Force = False
    30.     If IsMissing(Restart) Then Restart = True
    31.     If IsMissing(AllowLocalShutdown) Then AllowLocalShutdown = False
    32.     If IsMissing(Delay) Then Delay = 0
    33.     If IsMissing(Message) Then Message = ""
    34.     'Make sure the Machine-name doesn't start with '\\'
    35.     If InStr(Machine, "\\") = 1 Then
    36.         Machine = Right(Machine, Len(Machine) - 2)
    37.     End If
    38.     'check if it's the local machine that's going to be shutdown
    39.     If (LCase(GetMyMachineName) = LCase(Machine)) Then
    40.         'may we shut this computer down?
    41.         If AllowLocalShutdown = False Then Exit Function
    42.         'open access token
    43.         If OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hProc) = 0 Then
    44.             MsgBox "OpenProcessToken Error: " & GetLastError()
    45.             Exit Function
    46.         End If
    47.         'retrieve the locally unique identifier to represent the Shutdown-privilege name
    48.         If LookupPrivilegeValue(vbNullString, SE_SHUTDOWN_NAME, OldTokenStuff.Privileges(0).pLuid) = 0 Then
    49.             MsgBox "LookupPrivilegeValue Error: " & GetLastError()
    50.             Exit Function
    51.         End If
    52.         NewTokenStuff = OldTokenStuff
    53.         NewTokenStuff.PrivilegeCount = 1
    54.         NewTokenStuff.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
    55.         NewTokenStuffLen = Len(NewTokenStuff)
    56.         pSize = Len(NewTokenStuff)
    57.         'Enable shutdown-privilege
    58.         If AdjustTokenPrivileges(hProc, False, NewTokenStuff, NewTokenStuffLen, OldTokenStuff, OldTokenStuffLen) = 0 Then
    59.             MsgBox "AdjustTokenPrivileges Error: " & GetLastError()
    60.             Exit Function
    61.         End If
    62.         'initiate the system shutdown
    63.         If InitiateSystemShutdown("\\" & Machine, Message, Delay, Force, Restart) = 0 Then
    64.             Exit Function
    65.         End If
    66.         NewTokenStuff.Privileges(0).Attributes = 0
    67.         'Disable shutdown-privilege
    68.         If AdjustTokenPrivileges(hProc, False, NewTokenStuff, Len(NewTokenStuff), OldTokenStuff, Len(OldTokenStuff)) = 0 Then
    69.             Exit Function
    70.         End If
    71.     Else
    72.         'initiate the system shutdown
    73.         If InitiateSystemShutdown("\\" & Machine, Message, Delay, Force, Restart) = 0 Then
    74.             Exit Function
    75.         End If
    76.     End If
    77.     InitiateShutdownMachine = True
    78. End Function
    79. Function GetMyMachineName() As String
    80.     Dim sLen As Long
    81.     'create a buffer
    82.     GetMyMachineName = Space(100)
    83.     sLen = 100
    84.     'retrieve the computer name
    85.     If GetComputerName(GetMyMachineName, sLen) Then
    86.         GetMyMachineName = Left(GetMyMachineName, sLen)
    87.     End If
    88. End Function
    89.  
    90. Private Sub Command1_Click()
    91.  InitiateShutdownMachine GetMyMachineName, True, True, True, 60, "You initiated a system shutdown..."
    92. End Sub
    93.  
    94. Private Sub Form_Load()
    95.  
    96.    
    97. End Sub
    come back and mark your original post as resoved if your problem is fixed

    Jamie Garland

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How do i write a code to.....

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
    4.  
    5. Const EWX_LOGOFF = 0
    6. Const EWX_SHUTDOWN = 1
    7. Const EWX_REBOOT = 2
    8. Const EWX_FORCE = 4
    9.  
    10. Private Sub Command1_Click()
    11.     ExitWindowsEx EWX_FORCE Or EWX_SHUTDOWN , 0
    12. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    Member
    Join Date
    Oct 2006
    Location
    Australia
    Posts
    56

    Re: How do i write a code to.....

    shell shutdown.exe -s

    simple?
    my questions will be in vb6 or visual studios 2005

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How do i write a code to.....

    shutdown.exe is OS version specific.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: How do i write a code to.....

    Whilst ExitWindowsEx is supported by every post Win3.1 OS the following needs to be taken into consideration:
    from MDSN:

    Windows Me/98/95: Because of the design of the shell, calling ExitWindowsEx with EWX_FORCE fails to completely log off the user (the system terminates the applications and displays the Enter Windows Password dialog box, however, the user's desktop remains.) To log off the user forcibly, terminate the Explorer process before calling ExitWindowsEx with EWX_LOGOFF and EWX_FORCE.
    ...
    Console processes receive a separate notification message, CTRL_SHUTDOWN_EVENT or CTRL_LOGOFF_EVENT, as the situation warrants. A console process routes these messages to its HandlerRoutine function. ExitWindowsEx sends these notification messages asynchronously; thus, an application cannot assume that the console notification messages have been handled when a call to ExitWindowsEx returns.
    ...
    Windows Me/98/95: ExitWindowsEx does not work from a console application.

  8. #8
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    958

    Re: How do i write a code to.....

    Jamie_Garland, your code has a problem. You start with 'End Type' and you use 'pLuid As LARGE_INTEGER'
    What is 'LARGE_INTEGER' ?
    I would like to know how to shut down a PC...saves on all PC hardware and protects it too.

  9. #9
    Hyperactive Member Jenova's Avatar
    Join Date
    Feb 2006
    Location
    Googleplex
    Posts
    413

    Re: How do i write a code to.....

    LARGE_INTEGER is a structure used to represent a 64 bit integer. Each element in the structure represents a 32 bit integer.

    VB Code:
    1. Private Type LARGE_INTEGER
    2.     Lowpart As Long    ' Low order 32 bits
    3.     Highpart As Long    ' High order 32 bits
    4. End Type

  10. #10
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    958

    Re: How do i write a code to.....

    The shut-Down code is becoming closer to running...
    Code:
    Private Type TOKEN_PRIVILEGES
        PrivilegeCount As Long
        Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES
    End Type
    What about 'ANYSIZE_ARRAY' ?
    Incert any whole number?
    Thanks

  11. #11
    Hyperactive Member Jenova's Avatar
    Join Date
    Feb 2006
    Location
    Googleplex
    Posts
    413

    Re: How do i write a code to.....

    ANYSIZE_ARRAY is a constant with the value of 1

    VB Code:
    1. Private Const ANYSIZE_ARRAY As Long = 1

    Copy and Paste the constant above the TOKEN_PRIVILEGES structure.

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How do i write a code to.....

    You like doing things the hard way? My code does work and is very simple and easy.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    958

    Re: How do i write a code to.....

    Sir Rob, I know you are a very bright person, and your code did not work on my XP home computer, but I will try again on a different computer that has VB6 pro loaded.
    Also, over half the code I use, find, locate I really don't understand, yet I normally know how to use it. Thanks for being here.

  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How do i write a code to.....

    Did you place the code from #4 behind a form in a new project, run, and still nothing?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  15. #15
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    958

    Re: How do i write a code to.....

    OpenProcessToken Error: 5
    Nope, still not working....maybe I am doing something wrong?

    Code:
    Private Const ANYSIZE_ARRAY As Long = 1
    
    Private Type LARGE_INTEGER
        Lowpart As Long    ' Low order 32 bits
        Highpart As Long    ' High order 32 bits
    End Type
    
    Private Type Luid
        Lowpart As Long
        Highpart As Long
    End Type
    
    Private Type LUID_AND_ATTRIBUTES
        'pLuid As Luid
        pLuid As LARGE_INTEGER
        Attributes As Long
    End Type
    
    Private Type TOKEN_PRIVILEGES
        PrivilegeCount As Long
        Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES
    End Type
    
    Private Declare Function InitiateSystemShutdown Lib "advapi32.dll" Alias "InitiateSystemShutdownA" (ByVal lpMachineName As String, ByVal lpMessage As String, ByVal dwTimeout As Long, ByVal bForceAppsClosed As Long, ByVal bRebootAfterShutdown As Long) 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 GetCurrentProcess Lib "kernel32" () As Long
    Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LARGE_INTEGER) 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 GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    Private Declare Function GetLastError Lib "kernel32" () As Long
    Public Function InitiateShutdownMachine(ByVal Machine As String, Optional Force As Variant, Optional Restart As Variant, Optional AllowLocalShutdown As Variant, Optional Delay As Variant, Optional Message As Variant) As Boolean
    
        Dim hProc As Long
        Dim OldTokenStuff As TOKEN_PRIVILEGES
        Dim OldTokenStuffLen As Long
        Dim NewTokenStuff As TOKEN_PRIVILEGES
        Dim NewTokenStuffLen As Long
        Dim pSize As Long
    
        If IsMissing(Force) Then Force = False
        If IsMissing(Restart) Then Restart = True
        If IsMissing(AllowLocalShutdown) Then AllowLocalShutdown = False
        If IsMissing(Delay) Then Delay = 0
        If IsMissing(Message) Then Message = ""
        'Make sure the Machine-name doesn't start with '\'
        If InStr(Machine, "\\") = 1 Then
            Machine = Right(Machine, Len(Machine) - 2)
        End If
    
        'check if it's the local machine that's going to be shutdown
        If (LCase(GetMyMachineName) = LCase(Machine)) Then
            'may we shut this computer down?
            If AllowLocalShutdown = False Then Exit Function
            'open access token
            If OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hProc) = 0 Then
                MsgBox "OpenProcessToken Error: " & GetLastError()
                Exit Function
            End If
    
            'retrieve the locally unique identifier to represent the Shutdown-privilege name
            If LookupPrivilegeValue(vbNullString, SE_SHUTDOWN_NAME, OldTokenStuff.Privileges(0).pLuid) = 0 Then
                MsgBox "LookupPrivilegeValue Error: " & GetLastError()
                Exit Function
            End If
    
            NewTokenStuff = OldTokenStuff
            NewTokenStuff.PrivilegeCount = 1
            NewTokenStuff.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
            NewTokenStuffLen = Len(NewTokenStuff)
            pSize = Len(NewTokenStuff)
            'Enable shutdown-privilege
            If AdjustTokenPrivileges(hProc, False, NewTokenStuff, NewTokenStuffLen, OldTokenStuff, OldTokenStuffLen) = 0 Then
                MsgBox "AdjustTokenPrivileges Error: " & GetLastError()
                Exit Function
            End If
    
            'initiate the system shutdown
            If InitiateSystemShutdown("\\" & Machine, Message, Delay, Force, Restart) = 0 Then
                Exit Function
            End If
            NewTokenStuff.Privileges(0).Attributes = 0
            'Disable shutdown-privilege
            If AdjustTokenPrivileges(hProc, False, NewTokenStuff, Len(NewTokenStuff), OldTokenStuff, Len(OldTokenStuff)) = 0 Then
                Exit Function
            End If
        Else
            'initiate the system shutdown
            If InitiateSystemShutdown("\\" & Machine, Message, Delay, Force, Restart) = 0 Then
                Exit Function
            End If
        End If
        InitiateShutdownMachine = True
    End Function
    
    Function GetMyMachineName() As String
        Dim sLen As Long
        'create a buffer
        GetMyMachineName = Space(100)
        sLen = 100
        'retrieve the computer name
        If GetComputerName(GetMyMachineName, sLen) Then
            GetMyMachineName = Left(GetMyMachineName, sLen)
        End If
    End Function
    
    Private Sub Command1_Click()
     InitiateShutdownMachine GetMyMachineName, True, True, True, 60, "You initiated a system shutdown..."
    End Sub

  16. #16
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How do i write a code to.....

    Weird, on my XP system it doesnt work but the logof forced does.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
    4.  
    5. Private Const EWX_LOGOFF As Long = 0
    6. Private Const EWX_SHUTDOWN As Long = 1
    7. Private Const EWX_REBOOT As Long = 2
    8. Private Const EWX_FORCE As Long = 4
    9. Private Const EWX_POWEROFF As Long = &H8
    10. Private Const EWX_FORCEIFHUNG As Long = &H10
    11.  
    12. Private Sub Command1_Click()
    13.     ExitWindowsEx EWX_FORCE Or EWX_LOGOFF, 0
    14. End Sub
    Maybe its a SP-2 security thing?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  17. #17
    Hyperactive Member Jenova's Avatar
    Join Date
    Feb 2006
    Location
    Googleplex
    Posts
    413

    Re: How do i write a code to.....

    VB Code:
    1. ' Shutdown Flags
    2. Const EWX_LOGOFF = 0
    3. Const EWX_SHUTDOWN = 1
    4. Const EWX_REBOOT = 2
    5. Const EWX_FORCE = 4
    6. Const SE_PRIVILEGE_ENABLED = &H2
    7. Const TokenPrivileges = 3
    8. Const TOKEN_ASSIGN_PRIMARY = &H1
    9. Const TOKEN_DUPLICATE = &H2
    10. Const TOKEN_IMPERSONATE = &H4
    11. Const TOKEN_QUERY = &H8
    12. Const TOKEN_QUERY_SOURCE = &H10
    13. Const TOKEN_ADJUST_PRIVILEGES = &H20
    14. Const TOKEN_ADJUST_GROUPS = &H40
    15. Const TOKEN_ADJUST_DEFAULT = &H80
    16. Const SE_SHUTDOWN_NAME = "SeShutdownPrivilege"
    17. Const ANYSIZE_ARRAY = 1
    18. Private Type LARGE_INTEGER
    19.     lowpart As Long
    20.     highpart As Long
    21. End Type
    22. Private Type Luid
    23.     lowpart As Long
    24.     highpart As Long
    25. End Type
    26. Private Type LUID_AND_ATTRIBUTES
    27.     'pLuid As Luid
    28.     pLuid As LARGE_INTEGER
    29.     Attributes As Long
    30. End Type
    31. Private Type TOKEN_PRIVILEGES
    32.     PrivilegeCount As Long
    33.     Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES
    34. End Type
    35. Private Declare Function InitiateSystemShutdown Lib "advapi32.dll" Alias "InitiateSystemShutdownA" (ByVal lpMachineName As String, ByVal lpMessage As String, ByVal dwTimeout As Long, ByVal bForceAppsClosed As Long, ByVal bRebootAfterShutdown As Long) As Long
    36. Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
    37. Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
    38. Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LARGE_INTEGER) As Long
    39. 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
    40. Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    41. Private Declare Function GetLastError Lib "kernel32" () As Long
    42. Public Function InitiateShutdownMachine(ByVal Machine As String, Optional Force As Variant, Optional Restart As Variant, Optional AllowLocalShutdown As Variant, Optional Delay As Variant, Optional Message As Variant) As Boolean
    43.     Dim hProc As Long
    44.     Dim OldTokenStuff As TOKEN_PRIVILEGES
    45.     Dim OldTokenStuffLen As Long
    46.     Dim NewTokenStuff As TOKEN_PRIVILEGES
    47.     Dim NewTokenStuffLen As Long
    48.     Dim pSize As Long
    49.     If IsMissing(Force) Then Force = False
    50.     If IsMissing(Restart) Then Restart = True
    51.     If IsMissing(AllowLocalShutdown) Then AllowLocalShutdown = False
    52.     If IsMissing(Delay) Then Delay = 0
    53.     If IsMissing(Message) Then Message = ""
    54.     'Make sure the Machine-name doesn't start with '\\'
    55.     If InStr(Machine, "\\") = 1 Then
    56.         Machine = Right(Machine, Len(Machine) - 2)
    57.     End If
    58.     'check if it's the local machine that's going to be shutdown
    59.     If (LCase(GetMyMachineName) = LCase(Machine)) Then
    60.         'may we shut this computer down?
    61.         If AllowLocalShutdown = False Then Exit Function
    62.         'open access token
    63.         If OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hProc) = 0 Then
    64.             MsgBox "OpenProcessToken Error: " & GetLastError()
    65.             Exit Function
    66.         End If
    67.         'retrieve the locally unique identifier to represent the Shutdown-privilege name
    68.         If LookupPrivilegeValue(vbNullString, SE_SHUTDOWN_NAME, OldTokenStuff.Privileges(0).pLuid) = 0 Then
    69.             MsgBox "LookupPrivilegeValue Error: " & GetLastError()
    70.             Exit Function
    71.         End If
    72.         NewTokenStuff = OldTokenStuff
    73.         NewTokenStuff.PrivilegeCount = 1
    74.         NewTokenStuff.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
    75.         NewTokenStuffLen = Len(NewTokenStuff)
    76.         pSize = Len(NewTokenStuff)
    77.         'Enable shutdown-privilege
    78.         If AdjustTokenPrivileges(hProc, False, NewTokenStuff, NewTokenStuffLen, OldTokenStuff, OldTokenStuffLen) = 0 Then
    79.             MsgBox "AdjustTokenPrivileges Error: " & GetLastError()
    80.             Exit Function
    81.         End If
    82.         'initiate the system shutdown
    83.         If InitiateSystemShutdown("\\" & Machine, Message, Delay, Force, Restart) = 0 Then
    84.             Exit Function
    85.         End If
    86.         NewTokenStuff.Privileges(0).Attributes = 0
    87.         'Disable shutdown-privilege
    88.         If AdjustTokenPrivileges(hProc, False, NewTokenStuff, Len(NewTokenStuff), OldTokenStuff, Len(OldTokenStuff)) = 0 Then
    89.             Exit Function
    90.         End If
    91.     Else
    92.         'initiate the system shutdown
    93.         If InitiateSystemShutdown("\\" & Machine, Message, Delay, Force, Restart) = 0 Then
    94.             Exit Function
    95.         End If
    96.     End If
    97.     InitiateShutdownMachine = True
    98. End Function
    99. Function GetMyMachineName() As String
    100.     Dim sLen As Long
    101.     'create a buffer
    102.     GetMyMachineName = Space(100)
    103.     sLen = 100
    104.     'retrieve the computer name
    105.     If GetComputerName(GetMyMachineName, sLen) Then
    106.         GetMyMachineName = Left(GetMyMachineName, sLen)
    107.     End If
    108. End Function
    109. Private Sub Form_Load()
    110.     'KPD-Team 2000
    111.     'URL: [url]http://www.allapi.net/[/url]
    112.     'E-Mail: [email][email protected][/email]
    113.     InitiateShutdownMachine GetMyMachineName, True, True, True, 60, "You initiated a system shutdown..."
    114. End Sub

    Here is the code you need, this reboots the computer.
    Last edited by Jenova; Dec 27th, 2006 at 04:30 PM.

  18. #18
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    958

    Re: How do i write a code to.....

    Jenova, the code is working. This will allow me to turn off all PC at night.
    The Task Manager will run it. Once I make some small...very small changes I will summit for your approval for the Forumn.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width