C#关机的代码怎样?

解决方案 »

  1.   

    Option Explicit On 
    Option Strict OnImports System
    Imports System.Text
    Imports System.Diagnostics
    Imports System.Runtime.InteropServicesNamespace TimeShutDown
        '/// <summary>
        '/// Specifies the type of restart options that an application can use.
        '/// </summary>
        Public Enum RestartOptions
            '/// <summary>
            '/// 注销
            '/// </summary>
            LogOff = 0
            '/// <summary>
            '/// 关机
            '/// </summary>
            PowerOff = 8
            '/// <summary>
            '/// 重起
            '/// </summary>
            Reboot = 2
            '/// <summary>
            '/// 安全关机模式
            '/// </summary>
            ShutDown = 1
            '/// <summary>
            '/// Suspends the system.   休眠
            '/// </summary>
            Suspend = -1
            '/// <summary>
            '/// Hibernates the system. 待机
            '/// </summary>
            Hibernate = -2
        End Enum
        '/// <summary>
        '/// An LUID is a 64-bit value guaranteed to be unique only on the system on which it was generated. The uniqueness of a locally unique identifier (LUID) is guaranteed only until the system is restarted.
        '/// </summary>
        <StructLayout(LayoutKind.Sequential, Pack:=1)> _
        Friend Structure LUID
            '/// <summary>
            '/// The low order part of the 64 bit value.
            '/// </summary>
            Public LowPart As Integer
            '/// <summary>
            '/// The high order part of the 64 bit value.
            '/// </summary>
            Public HighPart As Integer
        End Structure
        '/// <summary>
        '/// The LUID_AND_ATTRIBUTES structure represents a locally unique identifier (LUID) and its attributes.
        '/// </summary>
        <StructLayout(LayoutKind.Sequential, Pack:=1)> _
        Friend Structure LUID_AND_ATTRIBUTES
            '/// <summary>
            '/// Specifies an LUID value.
            '/// </summary>
            Public pLuid As LUID
            '/// <summary>
            '/// Specifies attributes of the LUID. This value contains up to 32 one-bit flags. Its meaning is dependent on the definition and use of the LUID.
            '/// </summary>
            Public Attributes As Integer
        End Structure
        '/// <summary>
        '/// The TOKEN_PRIVILEGES structure contains information about a set of privileges for an access token.
        '/// </summary>
      

  2.   

    <StructLayout(LayoutKind.Sequential, Pack:=1)> _
        Friend Structure TOKEN_PRIVILEGES
            '/// <summary>
            '/// Specifies the number of entries in the Privileges array.
            '/// </summary>
            Public PrivilegeCount As Integer
            '/// <summary>
            '/// Specifies an array of LUID_AND_ATTRIBUTES structures. Each structure contains the LUID and attributes of a privilege.
            '/// </summary>
            Public Privileges As LUID_AND_ATTRIBUTES
        End Structure
        '/// <summary>
        '/// Implements methods to exit Windows.
        '/// </summary>
        Public Class WindowsController
            '/// <summary>Required to enable or disable the privileges in an access token.</summary>
            Private Const TOKEN_ADJUST_PRIVILEGES As Integer = &H20
            '/// <summary>Required to query an access token.</summary>
            Private Const TOKEN_QUERY As Integer = &H8
            '/// <summary>The privilege is enabled.</summary>
            Private Const SE_PRIVILEGE_ENABLED As Integer = &H2
            '/// <summary>Specifies that the function should search the system message-table resource(s) for the requested message.</summary>
            Private Const FORMAT_MESSAGE_FROM_SYSTEM As Integer = &H1000
            '/// <summary>Forces processes to terminate. When this flag is set, the system does not send the WM_QUERYENDSESSION and WM_ENDSESSION messages. This can cause the applications to lose data. Therefore, you should only use this flag in an emergency.</summary>
            Private Const EWX_FORCE As Integer = 4
            '/// <summary>
            '/// The LoadLibrary function maps the specified executable module into the address space of the calling process.
            '/// </summary>
            '/// <param name="lpLibFileName">Pointer to a null-terminated string that names the executable module (either a .dll or .exe file). The name specified is the file name of the module and is not related to the name stored in the library module itself, as specified by the LIBRARY keyword in the module-definition (.def) file.</param>
            '/// <returns>If the function succeeds, the return value is a handle to the module.<br></br><br>If the function fails, the return value is NULL. To get extended error information, call Marshal.GetLastWin32Error.</br></returns>
            Private Declare Ansi Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As IntPtr
            '/// <summary>
            '/// The FreeLibrary function decrements the reference count of the loaded dynamic-link library (DLL). When the reference count reaches zero, the module is unmapped from the address space of the calling process and the handle is no longer valid.
            '/// </summary>
            '/// <param name="hLibModule">Handle to the loaded DLL module. The LoadLibrary or GetModuleHandle function returns this handle.</param>
            '/// <returns>If the function succeeds, the return value is nonzero.<br></br><br>If the function fails, the return value is zero. To get extended error information, call Marshal.GetLastWin32Error.</br></returns>
            Private Declare Ansi Function FreeLibrary Lib "kernel32" (ByVal hLibModule As IntPtr) As Integer
            '/// <summary>
            '/// The GetProcAddress function retrieves the address of an exported function or variable from the specified dynamic-link library (DLL).
            '/// </summary>
            '/// <param name="hModule">Handle to the DLL module that contains the function or variable. The LoadLibrary or GetModuleHandle function returns this handle.</param>
            '/// <param name="lpProcName">Pointer to a null-terminated string containing the function or variable name, or the function's ordinal value. If this parameter is an ordinal value, it must be in the low-order word; the high-order word must be zero.</param>
            '/// <returns>If the function succeeds, the return value is the address of the exported function or variable.<br></br><br>If the function fails, the return value is NULL. To get extended error information, call Marshal.GetLastWin32Error.</br></returns>
            Private Declare Ansi Function GetProcAddress Lib "kernel32" (ByVal hModule As IntPtr, ByVal lpProcName As String) As IntPtr
            '/// <summary>
            '/// The SetSuspendState function suspends the system by shutting power down. Depending on the Hibernate parameter, the system either enters a suspend (sleep) state or hibernation (S4). If the ForceFlag parameter is TRUE, the system suspends operation immediately; if it is FALSE, the system requests permission from all applications and device drivers before doing so.
            '/// </summary>
            '/// <param name="Hibernate">Specifies the state of the system. If TRUE, the system hibernates. If FALSE, the system is suspended.</param>
            '/// <param name="ForceCritical">Forced suspension. If TRUE, the function broadcasts a PBT_APMSUSPEND event to each application and driver, then immediately suspends operation. If FALSE, the function broadcasts a PBT_APMQUERYSUSPEND event to each application to request permission to suspend operation.</param>
            '/// <param name="DisableWakeEvent">If TRUE, the system disables all wake events. If FALSE, any system wake events remain enabled.</param>
            '/// <returns>If the function succeeds, the return value is nonzero.<br></br><br>If the function fails, the return value is zero. To get extended error information, call Marshal.GetLastWin32Error.</br></returns>
            Private Declare Ansi Function SetSuspendState Lib "powrprof" (ByVal Hibernate As Integer, ByVal ForceCritical As Integer, ByVal DisableWakeEvent As Integer) As Integer
            '/// <summary>
            '/// The OpenProcessToken function opens the access token associated with a process.
            '/// </summary>
            '/// <param name="ProcessHandle">Handle to the process whose access token is opened.</param>
            '/// <param name="DesiredAccess">Specifies an access mask that specifies the requested types of access to the access token. These requested access types are compared with the token's discretionary access-control list (DACL) to determine which accesses are granted or denied.</param>
            '/// <param name="TokenHandle">Pointer to a handle identifying the newly-opened access token when the function returns.</param>
            '/// <returns>If the function succeeds, the return value is nonzero.<br></br><br>If the function fails, the return value is zero. To get extended error information, call Marshal.GetLastWin32Error.</br></returns>
            Private Declare Ansi Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As IntPtr, ByVal DesiredAccess As Integer, ByRef TokenHandle As IntPtr) As Integer
            '/// <summary>
            '/// The LookupPrivilegeValue function retrieves the locally unique identifier (LUID) used on a specified system to locally represent the specified privilege name.
            '/// </summary>
            '/// <param name="lpSystemName">Pointer to a null-terminated string specifying the name of the system on which the privilege name is looked up. If a null string is specified, the function attempts to find the privilege name on the local system.</param>
            '/// <param name="lpName">Pointer to a null-terminated string that specifies the name of the privilege, as defined in the Winnt.h header file. For example, this parameter could specify the constant SE_SECURITY_NAME, or its corresponding string, "SeSecurityPrivilege".</param>
            '/// <param name="lpLuid">Pointer to a variable that receives the locally unique identifier by which the privilege is known on the system, specified by the lpSystemName parameter.</param>
            '/// <returns>If the function succeeds, the return value is nonzero.<br></br><br>If the function fails, the return value is zero. To get extended error information, call Marshal.GetLastWin32Error.</br></returns>
      

  3.   

    有没有C#版本的啊?[email protected]?subject=CJL(email主题需以CJL开始才能收到。)
      

  4.   

    也就是调用几个API.网上大把VB的.楼主下一个回来转一下格式吧.
      

  5.   

    这位兄弟,我也要,[email protected],发个过来吧,谢谢