System Shutdown 
Author Message
 System Shutdown

How can I shutdown a W2K or XP system from inside an application (using
VB.NET)?

Thanks for any help...

Nuno Fonseca



Fri, 13 May 2005 23:16:06 GMT  
 System Shutdown
Hi,

You need use P/Invoke to call API.

Public Module Sample

    Private Declare Function ExitWindowsEx Lib "user32" _

(<System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.Un
managedType.U4)> _
        ByVal uFlags As exitWindowsFlags, _

<System.Runtime.InteropServices.MarshalAs(Runtime.InteropServices.UnmanagedT
ype.U8)> _
        ByVal dwReason As ShutdownReasons) As Boolean

    <Flags()> Public Enum ShutdownReasons As Long
        SHTDN_REASON_FLAG_COMMENT_REQUIRED = &H1000000
        SHTDN_REASON_FLAG_DIRTY_PROBLEM_ID_REQUIRED = &H2000000
        SHTDN_REASON_FLAG_CLEAN_UI = &H4000000
        SHTDN_REASON_FLAG_DIRTY_UI = &H8000000
        SHTDN_REASON_FLAG_USER_DEFINED = &H40000000
        SHTDN_REASON_FLAG_PLANNED = &H80000000
        SHTDN_REASON_MAJOR_OTHER = &H0
        SHTDN_REASON_MAJOR_NONE = &H0
        SHTDN_REASON_MAJOR_HARDWARE = &H10000
        SHTDN_REASON_MAJOR_OPERATINGSYSTEM = &H20000
        SHTDN_REASON_MAJOR_SOFTWARE = &H30000
        SHTDN_REASON_MAJOR_APPLICATION = &H40000
        SHTDN_REASON_MAJOR_SYSTEM = &H50000
        SHTDN_REASON_MAJOR_POWER = &H60000
        SHTDN_REASON_MAJOR_LEGACY_API = &H70000
        SHTDN_REASON_MINOR_OTHER = &H0
        SHTDN_REASON_MINOR_NONE = &HFF
        SHTDN_REASON_MINOR_MAINTENANCE = &H1
        SHTDN_REASON_MINOR_INSTALLATION = &H2
        SHTDN_REASON_MINOR_UPGRADE = &H3
        SHTDN_REASON_MINOR_RECONFIG = &H4
        SHTDN_REASON_MINOR_HUNG = &H5
        SHTDN_REASON_MINOR_UNSTABLE = &H6
        SHTDN_REASON_MINOR_DISK = &H7
        SHTDN_REASON_MINOR_PROCESSOR = &H8
        SHTDN_REASON_MINOR_NETWORKCARD = &H9
        SHTDN_REASON_MINOR_POWER_SUPPLY = &HA
        SHTDN_REASON_MINOR_CORDUNPLUGGED = &HB
        SHTDN_REASON_MINOR_ENVIRONMENT = &HC
        SHTDN_REASON_MINOR_HARDWARE_DRIVER = &HD
        SHTDN_REASON_MINOR_OTHERDRIVER = &HE
        SHTDN_REASON_MINOR_BLUESCREEN = &HF
        SHTDN_REASON_MINOR_SERVICEPACK = &H10
        SHTDN_REASON_MINOR_HOTFIX = &H11
        SHTDN_REASON_MINOR_SECURITYFIX = &H12
        SHTDN_REASON_MINOR_SECURITY = &H13
        SHTDN_REASON_MINOR_NETWORK_CONNECTIVITY = &H14
        SHTDN_REASON_MINOR_WMI = &H15
        SHTDN_REASON_MINOR_SERVICEPACK_UNINSTALL = &H16
        SHTDN_REASON_MINOR_HOTFIX_UNINSTALL = &H17
        SHTDN_REASON_MINOR_SECURITYFIX_UNINSTALL = &H18
        SHTDN_REASON_MINOR_MMC = &H19
        SHTDN_REASON_MINOR_TERMSRV = &H20
        SHTDN_REASON_UNKNOWN = SHTDN_REASON_MINOR_NONE
        SHTDN_REASON_LEGACY_API = SHTDN_REASON_MAJOR_LEGACY_API _
                Or SHTDN_REASON_FLAG_PLANNED
    End Enum

    <Flags()> Public Enum exitWindowsFlags As Integer
        EWX_LOGOFF = 0
        EWX_SHUTDOWN = 1&
        EWX_REBOOT = 2&
        EWX_FORCE = 4&
        EWX_POWEROFF = 8&
        EWX_FORCEIFHUNG = 10&
        EWX_RESET = EWX_LOGOFF Or EWX_FORCE Or EWX_REBOOT
    End Enum

    Sub ShutdownSystem()
        Dim result As Boolean
        result = ExitWindowsEx(exitWindowsFlags.EWX_LOGOFF, _
                  ShutdownReasons.SHTDN_REASON_FLAG_PLANNED)
        If Not result Then
            MsgBox("Shutdown Failed")
        End If
    End Sub

    Sub Main()
        ShutdownSystem()
    End Sub
End Module

I hope this helps. If you have any questions, please reply to this post.

Best Regards,

Jun Su
Microsoft Support

---
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------

| Subject: System Shutdown
| Date: Mon, 25 Nov 2002 15:16:06 -0000
|
| How can I shutdown a W2K or XP system from inside an application (using
| VB.NET)?
|
| Thanks for any help...
|
| Nuno Fonseca
|
|
|



Mon, 16 May 2005 16:06:25 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Which Events on a System Shutdown or Forced End

2. Detecting a system shutdown

3. System shutdown by means of a VB program

4. Oracle obstructs system shutdown!!

5. HELP: Sleep API call and W95 system shutdown

6. WIN32 API : SYSTEM SHUTDOWN

7. How to run defrag at system shutdown through vbscript on windows2000

8. Can I vbscript a system shutdown/restart?

9. System shutdown

10. Off-Topic: Outlook Express causes system shutdown hang

11. Receiving a system shutdown event in vb?

12. detecting and aborting the system shutdown

 

 
Powered by phpBB® Forum Software