선언:

C#

[DllImport("advapi32")]
public static extern Int32 AbortSystemShutdown(String lpMachineName);


VB.NET

<DllImport("advapi32")> _
Public Shared Function AbortSystemShutdown(lpMachineName As [String]) As Int32
End Function




사용 예제:

C#

using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;

namespace ApiReference {
    class ApiExample {
        
        [DllImport("advapi32")]
        public static extern Int32 AbortSystemShutdown(String lpMachineName);

        public static void Main(string[] args) {
            Console.WriteLine("진행 중인 시스템 종료를 취소합니다...");
            SetPrivilege("SeShutdownPrivilege"2);
            
            if ( AbortSystemShutdown(Environment.MachineName) == 0 )
                Console.WriteLine("실패!!! 진행 중인 시스템 종료가 있는게 맞나요??");
            else
                Console.WriteLine("진행 중인 시스템 종료를 취소했습니다.");

            Console.ReadKey(true);
        }
        
        private static void SetPrivilege(String privilegeName, Int32 state) {
            Assembly asm = Assembly.GetAssembly(typeof(System.Diagnostics.Process));
            if ( asm == null ) return;
            
            Type t = asm.GetType("System.Diagnostics.Process");
            if ( t == null ) return;
        
            MethodInfo mi = t.GetMethod("SetPrivilege", BindingFlags.Static | BindingFlags.NonPublic);
            if ( mi == null ) return;
            
            Object[] parameters = {privilegeName, state};
            mi.Invoke(null, parameters);
        }
    }
}


VB.NET

Imports System
Imports System.Diagnostics
Imports System.Reflection
Imports System.Runtime.InteropServices

Namespace ApiReference
    Class ApiExample

        <DllImport("advapi32")> _
        Public Shared Function AbortSystemShutdown(lpMachineName As [String]) As Int32
        End Function

        Public Shared Sub Main(args As String())
            Console.WriteLine("진행 중인 시스템 종료를 취소합니다...")
            SetPrivilege("SeShutdownPrivilege", 2)

            If AbortSystemShutdown(Environment.MachineName) = 0 Then
                Console.WriteLine("실패!!! 진행 중인 시스템 종료가 있는게 맞나요??")
            Else
                Console.WriteLine("진행 중인 시스템 종료를 취소했습니다.")
            End If

            Console.ReadKey(True)
        End Sub

        Private Shared Sub SetPrivilege(privilegeName As [String], state As Int32)
            Dim asm As Assembly = Assembly.GetAssembly(GetType(System.Diagnostics.Process))
            If asm Is Nothing Then
                Return
            End If

            Dim t As Type = asm.[GetType]("System.Diagnostics.Process")
            If t Is Nothing Then
                Return
            End If

            Dim mi As MethodInfo = t.GetMethod("SetPrivilege", BindingFlags.[StaticOr BindingFlags.NonPublic)
            If mi Is Nothing Then
                Return
            End If

            Dim parameters As [Object]() = {privilegeName, state}
            mi.Invoke(Nothing, parameters)
        End Sub
    End Class
End Namespace




예제 실행 결과:




매개 변수 설명:

lpMachineName - 시스템 종료가 진행 중인 머신의 이름을 입력합니다.




API 설명:

진행 중인 시스템 종료를 취소합니다.




참고:

AbortSystemShutdown (MSDN)




비고:

이 API 역시 마찬가지로 SE_SHUTDOWN_PRIVILEGE 특권 (시스템 종료 특권)을 필요로 하니, 특권 활성화 후에 API를 호출하도록 합시다. 이 API는 'shutdown -a' 와 동일한 작업을 합니다.



'API Reference' 카테고리의 다른 글

29. SetWindowTheme  (0) 2014.10.09
28. GetForegroundWindow  (0) 2014.10.09
26. InitiateSystemShutdown  (0) 2014.10.08
25. GetCurrentProcessId  (0) 2014.10.07
24. DebugActiveProcessStop  (0) 2014.10.06

+ Recent posts