private static IntPtr GetKillHandle(uint a_pid)
        {
            var hProc = IntPtr.Zero;

            var accessFlags = (uint)(ProcessFlags.QueryInformation | ProcessFlags.Terminate);

            var info = new Processentry32();

            info.dwSize = (uint)Marshal.SizeOf(typeof(Processentry32));

            if (!Process32First(s_hSnapShot, ref info))
            {
                throw new Exception("Cannot find first Process");
            }

            do
            {
                if (info.th32ProcessID == a_pid)
                {
                    hProc = OpenProcess(accessFlags, false, info.th32ProcessID);

                    break;
                }
            }while (Process32Next(s_hSnapShot, ref info));

            return(hProc);
        }
Esempio n. 2
0
        public static List <Process> GetChildProcesses(int processId)
        {
            var childProcesses = new List <Process>();
            var snapshotHandle = IntPtr.Zero;

            try
            {
                var targetSize   = (uint)Marshal.SizeOf(typeof(Processentry32));
                var processEntry = new Processentry32 {
                    dwSize = targetSize
                };
                snapshotHandle = CreateToolhelp32Snapshot((uint)SnapshotFlags.Process, 0);

                if (Process32First(snapshotHandle, ref processEntry))
                {
                    do
                    {
                        if (processId == processEntry.th32ParentProcessID)
                        {
                            childProcesses.Add(Process.GetProcessById((int)processEntry.th32ProcessID));
                        }
                    }while (Process32Next(snapshotHandle, ref processEntry));
                }
            }
            catch (Exception)
            {
                return(new List <Process>());
            }
            finally
            {
                CloseHandle(snapshotHandle);
            }

            return(childProcesses);
        }
        private static bool AddChildrenToStack(Processentry32 a_info, object a_data)
        {
            var killStack = (Stack <uint>)a_data;

            if (a_info.th32ParentProcessID == s_findPid)
            {
                killStack.Push(a_info.th32ProcessID);
            }

            return(true);
        }
Esempio n. 4
0
        /// <summary>
        /// Get the parent process PID
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        private static uint GetParentProcess(uint pid)
        {
            //entry of process information
            var pe = new Processentry32
                          {
                              dwSize = ((uint)Marshal.SizeOf(typeof(Processentry32)))
                          };

            //Handle to get process information
            var handleProcess = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

            uint parentPid = 0;

            try
            {
                //Get the process entry for the first Handle
                var rv = Process32First(handleProcess, ref pe);

                if (!rv)
                {
                    CloseHandle(handleProcess);
                    throw new SecurityException("Cannot iterate on process list");
                }

                while (rv)
                {
                    if (pid == pe.th32ProcessID) //is it the current process?
                    {
                        parentPid = pe.th32ParentProcessID;
                        break;
                    }

                    //Get the process entry for the next Handle
                    rv = Process32Next(handleProcess, ref pe);
                }
            }
            finally
            {
                CloseHandle(handleProcess);
            }

            return parentPid;
        }
        private static void VisitEachProcess(ProcVisitor a_vistor, object a_data)
        {
            var info = new Processentry32 {
                dwSize = (uint)Marshal.SizeOf(typeof(Processentry32))
            };

            if (!Process32First(s_hSnapShot, ref info))
            {
                throw new Exception("Cannot find first Process");
            }

            do
            {
                // callback if there is one
                if (a_vistor != null && a_vistor(info, a_data) == false)
                {
                    break;
                }
            }while (Process32Next(s_hSnapShot, ref info));
        }
Esempio n. 6
0
 private static extern bool Process32Next(IntPtr hSnapshot, ref Processentry32 lppe);
 public static extern bool Process32First(IntPtr hSnapshot, ref Processentry32 lppe);
Esempio n. 8
0
 internal static extern bool Process32Next([In] IntPtr hSnapshot, ref Processentry32 lppe);
Esempio n. 9
0
 public static extern bool Process32Next(IntPtr hSnapshot, ref Processentry32 lppe);
 static extern bool Process32Next(IntPtr a_hSnapshot, ref Processentry32 a_lppe);