Results 1 to 9 of 9

Thread: Exiting Windows

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2004
    Posts
    6

    Exiting Windows

    Hi,
    could anyone give me code for shutting down, restarting and logging off windows. This is the code I have at the moment but it only works when I load it from the project. It doesn't work as an application:

    Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
    Const EWX_LOGOFF = 0
    Const EWX_SHUTDOWN = 1
    Const EWX_REBOOT = 2
    Const EWX_FORCE = 4
    Const EWX_POWEROFF = 8
    Const EWX_RESET = EWX_LOGOFF + EWX_FORCE + EWX_REBOOT

    Private Sub Command1_Click()
    Dim X As Long
    If Combo1.ListIndex = 0 Then ' If restart option is selected
    X = ExitWindowsEx(EWX_RESET, dwReserved)
    ElseIf Combo1.ListIndex = 1 Then 'If Log off option is selected
    X = ExitWindowsEx(EWX_LOGOFF, dwReserved)
    ElseIf Combo1.ListIndex = 2 Then 'If Shut down option is selected
    X = ExitWindowsEx(EWX_SHUTDOWN, dwReserved)
    Else 'If Else
    End If
    End Sub

    Thanks a million to anyone who replys

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Try this. I think your last parameter is not populated with a value, dwReserved.
    VB Code:
    1. 'In general section
    2. Const EWX_LOGOFF = 0
    3. Const EWX_SHUTDOWN = 1
    4. Const EWX_REBOOT = 2
    5. Const EWX_FORCE = 4
    6. Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
    7. Private Sub Form_Load()
    8.     'KPD-Team 1998
    9.     'URL: [url]http://www.allapi.net/[/url]
    10.     'E-Mail: [email][email protected][/email]
    11.     msg = MsgBox("This program is going to reboot your computer. Press OK to continue or Cancel to stop.", vbCritical + vbOKCancel + 256, App.Title)
    12.     If msg = vbCancel Then End
    13.     'reboot the computer
    14.     ret& = ExitWindowsEx(EWX_FORCE Or EWX_REBOOT, 0)
    15. 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

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2004
    Posts
    6
    Thanks RobDogg, but this only reboots. I want to have it so you have three options of logout, shutdown and restart from a combo box. This is what i have for the command button:

    Private Sub Command1_Click()
    Dim X As Long
    If Combo1.ListIndex = 0 Then ' If restart option is selected
    X = ExitWindowsEx(EWX_RESET, dwReserved)
    ElseIf Combo1.ListIndex = 1 Then 'If Log off option is selected
    X = ExitWindowsEx(EWX_LOGOFF, dwReserved)
    ElseIf Combo1.ListIndex = 2 Then 'If Shut down option is selected
    X = ExitWindowsEx(EWX_SHUTDOWN, dwReserved)
    Else 'If Else
    End If
    End Sub

    Should i have code for the combo box?
    This is the code i have by itself;its not attached to anything:

    Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
    Const EWX_LOGOFF = 0
    Const EWX_SHUTDOWN = 1
    Const EWX_REBOOT = 2
    Const EWX_FORCE = 4
    Const EWX_POWEROFF = 8
    Const EWX_RESET = EWX_LOGOFF + EWX_FORCE + EWX_REBOOT

    Im only new to VB and im only 14
    Thanks again

    Nide_Elves

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

    The problem is the last parameter dwReserved. It is not used or
    declared. Try this code. Works for me.
    VB Code:
    1. Option Explicit
    2. 'Change the combo1 style property to "2 - Dropdown list" from
    3. 'the properties window manually.
    4. Const EWX_LOGOFF = 0
    5. Const EWX_SHUTDOWN = 1
    6. Const EWX_REBOOT = 2
    7. Const EWX_FORCE = 4
    8. Const EWX_RESET = EWX_LOGOFF + EWX_FORCE + EWX_REBOOT
    9.  
    10. Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
    11.  
    12. Private Sub Command1_Click()
    13.     If Combo1.ListIndex = 0 Then ' If restart option is selected
    14.         ExitWindowsEx EWX_RESET, 0&
    15.     ElseIf Combo1.ListIndex = 1 Then 'If Log off option is selected
    16.         ExitWindowsEx EWX_LOGOFF, 0&
    17.     ElseIf Combo1.ListIndex = 2 Then 'If Shut down option is selected
    18.         ExitWindowsEx EWX_SHUTDOWN, 0&
    19.     End If
    20. End Sub
    21.  
    22. Private Sub Form_Load()
    23.     Combo1.AddItem "Restart Windows"
    24.     Combo1.AddItem "Log off Windows"
    25.     Combo1.AddItem "Shut down Windows"
    26.     Combo1.ListIndex = 0
    27. End Sub
    HTH
    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

    Thread Starter
    New Member
    Join Date
    May 2004
    Posts
    6
    Now, there's 2 problems:

    No.1: It doesn't shutdown
    No.2: It doesn't restart

    Could this have anything to do with what windows i'm using?

    This is making me REALLY angry

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Try clearing the combo first and check out the requirements for the API.
    allapi.net
    VB Code:
    1. Private Sub Form_Load()
    2.     Combo1.ListItems.Clear
    3.     Combo1.AddItem "Restart Windows"
    4.     Combo1.AddItem "Log off Windows"
    5.     Combo1.AddItem "Shut down Windows"
    6.     Combo1.ListIndex = 0
    7. 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

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Here is a better way.
    Show the actual Windows Shutdown dialog box and let the user
    make the choice.
    VB Code:
    1. Private Declare Function SHShutDownDialog Lib "shell32" Alias "#60" (ByVal YourGuess As Long) As Long
    2.  
    3. Private Sub Form_Load()
    4.     'KPD-Team 1999
    5.     'URL: [url]http://www.allapi.net/[/url]
    6.     'E-Mail: [email][email protected][/email]
    7.     SHShutDownDialog 0
    8. 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

  8. #8

    Thread Starter
    New Member
    Join Date
    May 2004
    Posts
    6
    Yes, yes, yes. Thanks, but I actually wanted to have the program customed, not the Shutdown dialog come up.

    And so that the user has the choice of command.

    Thanks for putting up with me

    Nide

  9. #9
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171


    Has someone helped you? Then you can Rate their helpful post.

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