Ejemplo n.º 1
0
        /// <summary>
        /// Returns an array with information about running processes.
        /// </summary>
        ///<exception cref="Win32Exception">Thrown when enumerating the processes fails.</exception>
        public static ProcessInfo[] GetProcesses()
        {
            List <ProcessInfo> procList = new List <ProcessInfo>();

            IntPtr handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS | TH32CS_SNAPNOHEAPS, 0);

            if ((Int32)handle == INVALID_HANDLE_VALUE)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(), "CreateToolhelp32Snapshot error.");
            }

            try
            {
                PROCESSENTRY processentry = new PROCESSENTRY();
                processentry.dwSize = (uint)Marshal.SizeOf(processentry);

                //Get the first process
                int retval = Process32First(handle, ref processentry);

                while (retval == 1)
                {
                    procList.Add(new ProcessInfo(new IntPtr((int)processentry.th32ProcessID), (int)processentry.cntThreads, (int)processentry.th32MemoryBase, (int)processentry.th32ParentProcessID));
                    retval = Process32Next(handle, ref processentry);
                }
            }
            finally
            {
                CloseToolhelp32Snapshot(handle);
            }

            return(procList.ToArray());
        }
Ejemplo n.º 2
0
        private static bool Enumerate(ref List <ProcEntry> list)
        {
            try
            {
                if (list == null)
                {
                    return(false);
                }
                list.Clear();

                //IntPtr snapshot_ = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
                IntPtr snapshot_ = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS | TH32CS_SNAPNOHEAPS, 0);

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

                PROCESSENTRY entry_ = new PROCESSENTRY();
                entry_.dwSize = (uint)Marshal.SizeOf(entry_);
                if (Process32First(snapshot_, ref entry_) == 0)
                {
                    CloseToolhelp32Snapshot(snapshot_);
                    return(false);
                }

                do
                {
                    ProcEntry procEntry = new ProcEntry();
                    procEntry.ExeName = entry_.szExeFile;
                    procEntry.ID      = entry_.th32ProcessID;
                    list.Add(procEntry);
                    entry_.dwSize = (uint)Marshal.SizeOf(entry_);
                }while (Process32Next(snapshot_, ref entry_) != 0);

                CloseToolhelp32Snapshot(snapshot_);

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Finds the Process Id of the specified .EXE file.
        /// </summary>
        /// <param name="fullpath">The full path to an .EXE file.</param>
        /// <returns>The Process Id to the process found. Return IntPtr.Zero if the process is not running.</returns>
        ///<exception cref="Win32Exception">Thrown when taking a system snapshot fails.</exception>
        public static IntPtr FindProcessPID(string fullpath)
        {
            fullpath = fullpath.ToLower();

            IntPtr snapshot_handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS | TH32CS_SNAPNOHEAPS, 0);

            if ((Int32)snapshot_handle == INVALID_HANDLE_VALUE)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(), "CreateToolhelp32Snapshot failed.");
            }

            try
            {
                PROCESSENTRY processentry = new PROCESSENTRY();
                processentry.dwSize = (uint)Marshal.SizeOf(processentry);
                StringBuilder fullexepath = new StringBuilder(1024);

                int retval = Process32First(snapshot_handle, ref processentry);

                while (retval == 1)
                {
                    IntPtr pid = new IntPtr((int)processentry.th32ProcessID);

                    // Writes the full path to the process into a StringBuilder object.
                    // Note: If first parameter is IntPtr.Zero it returns the path to the current process.
                    GetModuleFileName(pid, fullexepath, fullexepath.Capacity);

                    if (fullexepath.ToString().ToLower() == fullpath)
                    {
                        return(pid);
                    }

                    retval = Process32Next(snapshot_handle, ref processentry);
                }
            }
            finally
            {
                CloseToolhelp32Snapshot(snapshot_handle);
            }

            return(IntPtr.Zero);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Finds the Process Id of the specified .EXE file.
        /// </summary>
        /// <param name="fullpath">The full path to an .EXE file.</param>
        /// <returns>The Process Id to the process found. Return IntPtr.Zero if the process is not running.</returns>
        ///<exception cref="Win32Exception">Thrown when taking a system snapshot fails.</exception>
        public static IntPtr FindProcessPID(string fullpath)
        {
            fullpath = fullpath.ToLower();

            IntPtr snapshot_handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS | TH32CS_SNAPNOHEAPS, 0);

            if ((Int32)snapshot_handle == INVALID_HANDLE_VALUE)
                throw new Win32Exception(Marshal.GetLastWin32Error(), "CreateToolhelp32Snapshot failed.");

            try
                {
                PROCESSENTRY processentry = new PROCESSENTRY();
                processentry.dwSize = (uint)Marshal.SizeOf(processentry);
                StringBuilder fullexepath = new StringBuilder(1024);

                int retval = Process32First(snapshot_handle, ref processentry);

                while (retval == 1)
                    {
                    IntPtr pid = new IntPtr((int)processentry.th32ProcessID);

                    // Writes the full path to the process into a StringBuilder object.
                    // Note: If first parameter is IntPtr.Zero it returns the path to the current process.
                    GetModuleFileName(pid, fullexepath, fullexepath.Capacity);

                    if (fullexepath.ToString().ToLower() == fullpath)
                        return pid;

                    retval = Process32Next(snapshot_handle, ref processentry);
                    }
                }
            finally
                {
                CloseToolhelp32Snapshot(snapshot_handle);
                }

            return IntPtr.Zero;
        }
Ejemplo n.º 5
0
 private static extern int Process32Next(IntPtr snapshot, ref PROCESSENTRY processEntry);
Ejemplo n.º 6
0
        /// <summary>
        /// Returns an array with information about running processes.
        /// </summary>
        ///<exception cref="Win32Exception">Thrown when enumerating the processes fails.</exception>
        public static ProcessInfo[] GetProcesses()
        {
            List<ProcessInfo> procList = new List<ProcessInfo>();

            IntPtr handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS | TH32CS_SNAPNOHEAPS, 0);

            if ((Int32)handle == INVALID_HANDLE_VALUE)
                throw new Win32Exception(Marshal.GetLastWin32Error(), "CreateToolhelp32Snapshot error.");

            try
            {
                PROCESSENTRY processentry = new PROCESSENTRY();
                processentry.dwSize = (uint)Marshal.SizeOf(processentry);

                //Get the first process
                int retval = Process32First(handle, ref processentry);

                while (retval == 1)
                {
                    procList.Add(new ProcessInfo(new IntPtr((int)processentry.th32ProcessID), (int)processentry.cntThreads, (int)processentry.th32MemoryBase, (int)processentry.th32ParentProcessID));
                    retval = Process32Next(handle, ref processentry);
                }
            }
            finally
            {
                CloseToolhelp32Snapshot(handle);
            }

            return procList.ToArray();
        }
Ejemplo n.º 7
0
 private static extern int Process32Next(IntPtr snapshot, ref PROCESSENTRY processEntry);