Esempio n. 1
0
    public static ArrayList GetProcessesEx()
    {
        ArrayList ar        = new ArrayList();
        IntPtr    snapshot_ = NativeMethods.CreateToolhelp32Snapshot(NativeMethods.TH32CS_SNAPPROCESS, 0);

        if (snapshot_ == IntPtr.Zero)
        {
            return(null);
        }

        NativeMethods.PROCESSENTRY32 entry_ = new NativeMethods.PROCESSENTRY32();
        entry_.dwSize = (uint)Marshal.SizeOf(entry_);

        if (NativeMethods.Process32First(snapshot_, ref entry_) == 0)
        {
            NativeMethods.CloseHandle(snapshot_);
            return(null);
        }

        do
        {
            ExeData w = new ExeData();
            w.title = entry_.szExeFile;
            w.pid   = entry_.th32ProcessID;
            ar.Add(w);
        }while (NativeMethods.Process32Next(snapshot_, ref entry_) != 0);

        NativeMethods.CloseHandle(snapshot_);
        return(ar);
    }
Esempio n. 2
0
        /// <summary>
        /// Gets the parent process identifier.
        /// </summary>
        /// <exception cref="Win32Exception">
        ///     Thrown when a window 32 error condition occurs.
        /// </exception>
        /// <param name="process">
        ///     The process.
        /// </param>
        /// <returns>
        /// The parent process identifier.
        /// </returns>
        private static int GetParentProcessIdentifier(Process process)
        {
            // Based on an approach from http://blogs.msdn.com/b/toffer/archive/2005/07/21/441540.aspx
            var    processEntry     = new NativeMethods.PROCESSENTRY32();
            IntPtr handleToSnapshot = NativeMethods.CreateToolhelp32Snapshot(NativeMethods.TH32CS_SNAPPROCESS, 0);

            try
            {
                if (!NativeMethods.Process32First(handleToSnapshot, processEntry))
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }

                if (process.Id == processEntry.ProcessId)
                {
                    return(processEntry.ParentProcessId);
                }

                while (NativeMethods.Process32Next(handleToSnapshot, processEntry))
                {
                    if (process.Id == processEntry.ProcessId)
                    {
                        return(processEntry.ParentProcessId);
                    }
                }
            }
            catch (Win32Exception exception)
            {
                Logger.WriteWarning(exception);
            }
            finally
            {
                NativeMethods.CloseHandle(handleToSnapshot);
            }

            return(0);
        }
Esempio n. 3
0
        private List <uint> GetChildPids()
        {
            List <uint> lPids = new List <uint>();

            if (KeePassLib.Native.NativeLib.IsUnix())
            {
                return(lPids);
            }

            try
            {
                uint pidThis = (uint)Process.GetCurrentProcess().Id;

                uint uEntrySize = (uint)Marshal.SizeOf(typeof(
                                                           NativeMethods.PROCESSENTRY32));
                if (WinUtil.IsAtLeastWindows2000)                // Unicode
                {
                    int p = Marshal.SizeOf(typeof(IntPtr));
                    if (p == 4)
                    {
                        Debug.Assert(uEntrySize ==
                                     NativeMethods.PROCESSENTRY32SizeUni32);
                    }
                    else if (p == 8)
                    {
                        Debug.Assert(uEntrySize ==
                                     NativeMethods.PROCESSENTRY32SizeUni64);
                    }
                }

                IntPtr hSnap = NativeMethods.CreateToolhelp32Snapshot(
                    NativeMethods.ToolHelpFlags.SnapProcess, 0);
                if (NativeMethods.IsInvalidHandleValue(hSnap))
                {
                    Debug.Assert(false);
                    return(lPids);
                }

                for (int i = 0; i < int.MaxValue; ++i)
                {
                    NativeMethods.PROCESSENTRY32 pe = new NativeMethods.PROCESSENTRY32();
                    pe.dwSize = uEntrySize;

                    bool b;
                    if (i == 0)
                    {
                        b = NativeMethods.Process32First(hSnap, ref pe);
                    }
                    else
                    {
                        b = NativeMethods.Process32Next(hSnap, ref pe);
                    }
                    if (!b)
                    {
                        break;
                    }

                    if (pe.th32ProcessID == pidThis)
                    {
                        continue;
                    }
                    if (pe.th32ParentProcessID != pidThis)
                    {
                        continue;
                    }

                    if (!string.IsNullOrEmpty(m_strChildExeName))
                    {
                        if (pe.szExeFile == null)
                        {
                            Debug.Assert(false); continue;
                        }

                        string str = GetExeName(pe.szExeFile);
                        if (!str.Equals(m_strChildExeName, StrUtil.CaseIgnoreCmp))
                        {
                            continue;
                        }
                    }

                    lPids.Add(pe.th32ProcessID);
                }

                if (!NativeMethods.CloseHandle(hSnap))
                {
                    Debug.Assert(false);
                }
            }
            catch (Exception) { Debug.Assert(false); }

            return(lPids);
        }