|
-
May 25th, 2004, 07:03 AM
#1
Thread Starter
New Member
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
-
May 25th, 2004, 10:58 AM
#2
Try this. I think your last parameter is not populated with a value, dwReserved.
VB Code:
'In general section
Const EWX_LOGOFF = 0
Const EWX_SHUTDOWN = 1
Const EWX_REBOOT = 2
Const EWX_FORCE = 4
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Private Sub Form_Load()
'KPD-Team 1998
'URL: [url]http://www.allapi.net/[/url]
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
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 26th, 2004, 02:01 AM
#3
Thread Starter
New Member
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
-
May 26th, 2004, 10:29 AM
#4
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:
Option Explicit
'Change the combo1 style property to "2 - Dropdown list" from
'the properties window manually.
Const EWX_LOGOFF = 0
Const EWX_SHUTDOWN = 1
Const EWX_REBOOT = 2
Const EWX_FORCE = 4
Const EWX_RESET = EWX_LOGOFF + EWX_FORCE + EWX_REBOOT
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Private Sub Command1_Click()
If Combo1.ListIndex = 0 Then ' If restart option is selected
ExitWindowsEx EWX_RESET, 0&
ElseIf Combo1.ListIndex = 1 Then 'If Log off option is selected
ExitWindowsEx EWX_LOGOFF, 0&
ElseIf Combo1.ListIndex = 2 Then 'If Shut down option is selected
ExitWindowsEx EWX_SHUTDOWN, 0&
End If
End Sub
Private Sub Form_Load()
Combo1.AddItem "Restart Windows"
Combo1.AddItem "Log off Windows"
Combo1.AddItem "Shut down Windows"
Combo1.ListIndex = 0
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 28th, 2004, 02:16 AM
#5
Thread Starter
New Member
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
-
May 28th, 2004, 12:04 PM
#6
Try clearing the combo first and check out the requirements for the API.
allapi.net
VB Code:
Private Sub Form_Load()
Combo1.ListItems.Clear
Combo1.AddItem "Restart Windows"
Combo1.AddItem "Log off Windows"
Combo1.AddItem "Shut down Windows"
Combo1.ListIndex = 0
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 28th, 2004, 12:29 PM
#7
Here is a better way.
Show the actual Windows Shutdown dialog box and let the user
make the choice.
VB Code:
Private Declare Function SHShutDownDialog Lib "shell32" Alias "#60" (ByVal YourGuess As Long) As Long
Private Sub Form_Load()
'KPD-Team 1999
'URL: [url]http://www.allapi.net/[/url]
SHShutDownDialog 0
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 28th, 2004, 06:05 PM
#8
Thread Starter
New Member
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
-
May 30th, 2004, 03:07 AM
#9
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|