zoukankan      html  css  js  c++  java
  • 进程守护

    private class SetPID
            {
                public delegate void SETPID(uint iPID);
                [DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
                public static extern SETPID GetProcAddress(IntPtr hModule, string procName);
            }
    
            [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            public static extern uint GetCurrentProcessId();
    
            private delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
            [DllImport("kernel32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            private static extern HookProc GetProcAddress(IntPtr hModule, string procName);
    
            [DllImport("kernel32.dll", CallingConvention = CallingConvention.StdCall)]
            private static extern IntPtr LoadLibrary(string sComName);
            [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
            private static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr pInstance, int threadId);
            [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
            private static extern bool UnhookWindowsHookEx(IntPtr pHookHandle);
    
            private const string NKCore = "NKCore.dll";
            private const int WH_GETMESSAGE = 3;
    
            public static bool ProtectProcess(uint processID, out IntPtr iHookProcedure)
            {
                //创建VC++核心动态库
                string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, NKCore);
                if (!File.Exists(path))
                {
                    AppRuntime.CreateFileFromResource(false, "Rocky.Resources.NKCore.dll", path);
                }
                IntPtr pInstance = LoadLibrary(NKCore);
                SetPID.SETPID pGPA = SetPID.GetProcAddress(pInstance, "SetPID");
                if (pGPA == null)
                {
                    iHookProcedure = IntPtr.Zero;
                    return false;
                }
                pGPA(processID);
                HookProc HookProcedure = GetProcAddress(pInstance, "MsgProc");
                iHookProcedure = SetWindowsHookEx(WH_GETMESSAGE, HookProcedure, pInstance, 0);
                return iHookProcedure != IntPtr.Zero;
            }
    
            public static bool UnprotectProcess(ref IntPtr iHookProcedure)
            {
                return UnhookWindowsHookEx(iHookProcedure);
            }
  • 相关阅读:
    Tomcat支持多少并发
    Redis高可用架构—Keepalive+VIP
    MapReduce运行原理
    hihoCoder 1015 KMP算法(kmp)
    <html>
    UI--仿IOS控件之ActionSheet样式 and more..
    redis集群
    mongodb及mongoclient在win7下的编译和使用
    @property与@synthesize的差别
    传智播客c/c++公开课学习笔记--邮箱账户的破解与邮箱安全防控
  • 原文地址:https://www.cnblogs.com/Googler/p/2770697.html
Copyright © 2011-2022 走看看