NEWS

[C#] Hướng dẫn Protected Process trong lập trình csharp

[C#] Hướng dẫn Protected Process trong lập trình csharp
Đăng bởi: Thảo Meo - Lượt xem: 6070 08:23:13, 15/09/2018DEVEXPRESS   In bài viết

Trong bài viết hôm nay, mình sẽ hướng dẫn các bạn cách protected process trong lập trình C#, trong nhiều ứng dụng các bạn thường thấy, có một vài ứng dụng các bạn không thể tắt ứng dụng được từ Task Manager windows.

Các ứng dụng thường thấy, là các chương trình diệt virus. :)

Để ứng dụng chạy bạn cần chạy ứng dụng dưới quyền Administrator, cách cấu hình ứng dụng chạy run as Administrator, các bạn có thể tham khảo ở link bên dưới:

[C#] Hướng dẫn chạy ứng dụng dưới quyền Administrator trong lập trình csharp

Dưới đây là giao diện demo ứng dụng:

Protected process C#

Đầu tiên, các bạn tạo một class với tên ProcessProtection.cs với source code C# bên dưới:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ProtectedProcess
{
    class ProcessProtection
    {
        [DllImport("ntdll.dll", SetLastError = true)]
        private static extern void RtlSetProcessIsCritical(UInt32 v1, UInt32 v2, UInt32 v3);

        ///
        /// Flag for maintaining the state of protection.
        /// 
        private static volatile bool s_isProtected = false;

        ///
        /// For synchronizing our current state.
        /// 
        private static ReaderWriterLockSlim s_isProtectedLock = new ReaderWriterLockSlim();

        ///
        /// Gets whether or not the host process is currently protected.
        /// 
        public static bool IsProtected
        {
            get
            {
                try
                {
                    s_isProtectedLock.EnterReadLock();

                    return s_isProtected;
                }
                finally
                {
                    s_isProtectedLock.ExitReadLock();
                }
            }
        }

        ///
        /// If not alreay protected, will make the host process a system-critical process so it
        /// cannot be terminated without causing a shutdown of the entire system.
        /// 
        public static void Protect()
        {
            try
            {
                s_isProtectedLock.EnterWriteLock();

                if (!s_isProtected)
                {
                    System.Diagnostics.Process.EnterDebugMode();
                    RtlSetProcessIsCritical(1, 0, 0);
                    s_isProtected = true;
                }
            }
            finally
            {
                s_isProtectedLock.ExitWriteLock();
            }
        }

        ///
        /// If already protected, will remove protection from the host process, so that it will no
        /// longer be a system-critical process and thus will be able to shut down safely.
        /// 
        public static void Unprotect()
        {
            try
            {
                s_isProtectedLock.EnterWriteLock();

                if (s_isProtected)
                {
                    RtlSetProcessIsCritical(0, 0, 0);
                    s_isProtected = false;
                }
            }
            finally
            {
                s_isProtectedLock.ExitWriteLock();
            }
        }
    }
}

- Cách sử dụng, khi mở ứng dụng các bạn gọi hàm 

ProcessProtection.Protect();

- Và viết sự kiện khi đóng form 

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            ProcessProtection.Unprotect();
        }

- Lưu ý: trong ứng dụng này, nếu bạn cố gắng đóng ứng dụng từ taskmanager, hoặc tắt ứng dụng mà không có gọi hàm Unprotect, thì màn hình máy tính của bạn là hiển thị màu xanh chết chóc (BSOD).

Nên các bạn nhớ cẩn thận, lưu trữ dữ liệu các phần mềm đang chạy khi demo ứng dụng trên nhé!

màu xanh chết chóc windows

HAPPY CODING heart

DOWNLOAD SOURCE

 

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn Protected Process trong lập trình csharp
Đăng bởi: Thảo Meo - Lượt xem: 6070 08:23:13, 15/09/2018DEVEXPRESS   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.