예제 #1
0
파일: Processes.cs 프로젝트: huizh/xenadmin
        /*
         * Based upon
         * http://blogs.msdn.com/jasonz/archive/2007/05/11/code-sample-is-your-process-using-the-silverlight-clr.aspx
         * and http://www.csharpfriends.com/Forums/ShowPost.aspx?PostID=27395.
         */
        /// <summary>
        /// Returns the parent Process of the given one, or null on failure.
        /// </summary>
        /// <returns></returns>
        public static Process FindParent(Process process)
        {
            int procid = process.Id;

            Win32.PROCESSENTRY32 pe32 = new Win32.PROCESSENTRY32();
            pe32.dwSize = (uint)Marshal.SizeOf(pe32);

            Win32.ToolHelpHandle thh = Win32.CreateToolhelp32Snapshot(Win32.SnapshotFlags.Process, 0);
            try
            {
                if (thh.IsInvalid)
                {
                    log.Error("CreateToolhelp32Snapshot failed");
                    return null;
                }

                if (Win32.Process32First(thh, ref pe32))
                {
                    do
                    {
                        if (pe32.th32ProcessID == procid)
                        {
                            return Process.GetProcessById(Convert.ToInt32(pe32.th32ParentProcessID));
                        }
                    }
                    while (Win32.Process32Next(thh, ref pe32));

                    // Maybe the given process has gone away?  I don't know if it's possible for a process
                    // to have no parent.
                    return null;
                }
                else
                {
                    log.Error("Process32First failed");
                    return null;
                }
            }
            finally
            {
                thh.Close();
            }
        }
예제 #2
0
        /*
         * Based upon
         * http://blogs.msdn.com/jasonz/archive/2007/05/11/code-sample-is-your-process-using-the-silverlight-clr.aspx
         * and http://www.csharpfriends.com/Forums/ShowPost.aspx?PostID=27395.
         */
        /// <summary>
        /// Returns the parent Process of the given one, or null on failure.
        /// </summary>
        /// <returns></returns>
        public static Process FindParent(Process process)
        {
            int procid = process.Id;

            Win32.PROCESSENTRY32 pe32 = new Win32.PROCESSENTRY32();
            pe32.dwSize = (uint)Marshal.SizeOf(pe32);

            Win32.ToolHelpHandle thh = Win32.CreateToolhelp32Snapshot(Win32.SnapshotFlags.Process, 0);
            try
            {
                if (thh.IsInvalid)
                {
                    log.Error("CreateToolhelp32Snapshot failed");
                    return(null);
                }

                if (Win32.Process32First(thh, ref pe32))
                {
                    do
                    {
                        if (pe32.th32ProcessID == procid)
                        {
                            return(Process.GetProcessById(Convert.ToInt32(pe32.th32ParentProcessID)));
                        }
                    }while (Win32.Process32Next(thh, ref pe32));

                    // Maybe the given process has gone away?  I don't know if it's possible for a process
                    // to have no parent.
                    return(null);
                }
                else
                {
                    log.Error("Process32First failed");
                    return(null);
                }
            }
            finally
            {
                thh.Close();
            }
        }