我在Form添加一个关机按钮,如果SHUTDOWN字段定义等于0,功能实现,如果定义为1,抛出异常。
请高手解决。代码如下:
        [DllImport("user32")]
        public static extern long ExitWindowsEx(long uFlag, long dwReserved);        long dwReserved;
        const int SHUTDOWN=1;
        long sh;
        private void ButClose_Click(object sender, EventArgs e)
        {
            sh = ExitWindowsEx(SHUTDOWN, dwReserved);
        }

解决方案 »

  1.   

    报什么错,什么系统EWX_SHUTDOWN
    EWX_SHUTDOWN = 1;
    关闭系统,安全地关闭电源。所有文件缓冲区已经刷新到磁盘上,所有正在运行的进程已经停止。
    Windows要求:
    Windows NT中:调用进程必须有SE_SHUTDOWN_NAME特权。
    Windows 9X中:可以直接调用。
    编辑本段
    参数dwReserved
      

  2.   

    win7系统
    在2010版本上“报PInvoke 签名与非托管的目标签名不匹配”错。
    在08版本,就没有反应。
    如果定义SHUTDOWN=0,会注销系统。
      

  3.   

    能说说都是那些权限的问题吗?
    开发过程中用是的admin权限。
      

  4.   


    Option Compare Binary
    Option Explicit On
    Option Strict On
    Public Class clShutdown
        Public Declare Function ExitWindowsEx Lib "user32.dll" (ByVal uFlags As Integer, ByVal dwReserved As Integer) As Boolean
        Public Declare Function NtShutdownSystem Lib "ntdll" (ByVal ShutdownAction As enumntshutdownsystem) As Integer
        Public Declare Function RtlAdjustPrivilege Lib "ntdll" (ByVal Privilege As Integer, ByVal NewValue As Integer, ByVal NewThread As Integer, ByRef OldValue As Integer) As Integer
        Public Const SE_SHUTDOWN_PRIVILEGE As Integer = 19
        Enum enumntshutdownsystem As Integer
            shutdown = 0
            RESTART = 1
            POWEROFF = 2
        End Enum
        Enum enumexitwindowsflags As Integer
            EWX_LOGOFF = 0
            EWX_SHUTDOWN = 1
            EWX_REBOOT = 2
            EWX_FORCE = 4
            EWX_POWEROFF = 8
            EWX_FORCEIFHUNG = 10
        End Enum
        Public Sub TurboShutdown(ByVal turbo As enumntshutdownsystem)
            Try
                RtlAdjustPrivilege(SE_SHUTDOWN_PRIVILEGE, 1, 0, 0)
                NtShutdownSystem(turbo) '关机
                'NtShutdownSystem restart '重启动
                'NtShutdownSystem poweroff '关机
            Catch ex As Exception
            End Try
        End Sub
        Public Sub simshutdown(ByVal sim As enumexitwindowsflags)
            Try
                Dim b As Boolean
                RtlAdjustPrivilege(SE_SHUTDOWN_PRIVILEGE, 1, 0, 0)
                b = ExitWindowsEx(sim, 0)
            Catch ex As Exception
            End Try
        End Sub
    End Class很久之前用vb.net写的一个。你找个在线翻译的。翻译成c#吧。
      

  5.   

    回6楼,
    主要是想学习调用API函数方法及在调用过程中出现的这些异常的处理方式。
      

  6.   

    其实没有 那么复杂吧,直接用 Process CMD 就可以了,Shutdown 0 -t