コード例 #1
0
ファイル: ThreadEntry.cs プロジェクト: zhangli0092/opennetcf
        public static ThreadEntry[] GetThreads(uint processID)
        {
            ArrayList threadList = new ArrayList();

            IntPtr handle = ToolhelpAPI.CreateToolhelp32Snapshot(ToolhelpAPI.TH32CS_SNAPTHREAD, processID);

            if ((int)handle > 0)
            {
                try
                {
                    THREADENTRY32 teCurrent;
                    THREADENTRY32 te32 = new THREADENTRY32();

                    //Get byte array to pass to the API calls
                    byte[] teBytes = te32.ToByteArray();

                    //Get the first process
                    int retval = ToolhelpAPI.Thread32First(handle, teBytes);

                    while (retval == 1)
                    {
                        //Convert bytes to the class
                        teCurrent = new THREADENTRY32(teBytes);

                        if ((processID == 0) || (teCurrent.th32OwnerProcessID == processID))
                        {
                            //New instance
                            ThreadEntry tentry = new ThreadEntry(teCurrent);

                            threadList.Add(tentry);
                        }

                        retval = ToolhelpAPI.Thread32Next(handle, teBytes);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("Exception: " + ex.Message);
                }

                //Close handle
                ToolhelpAPI.CloseToolhelp32Snapshot(handle);

                return((ThreadEntry[])threadList.ToArray(typeof(ThreadEntry)));
            }
            else
            {
                throw new Exception("Unable to create snapshot");
            }
        }
コード例 #2
0
ファイル: ProcessEntry.cs プロジェクト: zhangli0092/opennetcf
        /// <summary>
        /// Rerieves an array of all running processes
        /// </summary>
        /// <returns></returns>
        public static ProcessEntry[] GetProcesses()
        {
            ArrayList procList = new ArrayList();

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

            if ((int)handle > 0)
            {
                try
                {
                    PROCESSENTRY32 peCurrent;
                    PROCESSENTRY32 pe32 = new PROCESSENTRY32();

                    //Get byte array to pass to the API calls
                    byte[] peBytes = pe32.ToByteArray();
                    byte[] empty   = new byte[peBytes.Length];
                    peBytes.CopyTo(empty, 0);

                    //Get the first process
                    int retval = ToolhelpAPI.Process32First(handle, peBytes);

                    while (retval == 1)
                    {
                        //Convert bytes to the class
                        peCurrent = new PROCESSENTRY32(peBytes);
                        //New instance
                        ProcessEntry proc = new ProcessEntry(peCurrent);

                        procList.Add(proc);

                        retval = ToolhelpAPI.Process32Next(handle, peBytes);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("Exception: " + ex.Message);
                }
                finally
                {
                    //Close handle
                    ToolhelpAPI.CloseToolhelp32Snapshot(handle);
                }

                return((ProcessEntry[])procList.ToArray(typeof(ProcessEntry)));
            }
            else
            {
                throw new Exception("Unable to create snapshot");
            }
        }
コード例 #3
0
ファイル: ModuleEntry.cs プロジェクト: zhangli0092/opennetcf
        public static ModuleEntry[] GetModules(uint processID)
        {
            List <ModuleEntry> moduleList = new List <ModuleEntry>();

            IntPtr handle = ToolhelpAPI.CreateToolhelp32Snapshot(ToolhelpAPI.TH32CS_SNAPMODULE, processID);

            if ((int)handle > 0)
            {
                try
                {
                    MODULEENTRY32 meCurrent;
                    MODULEENTRY32 me32 = new MODULEENTRY32();

                    //Get byte array to pass to the API calls
                    byte[] meBytes = me32.ToByteArray();

                    //Get the first process
                    int retval = ToolhelpAPI.Module32First(handle, meBytes);

                    while (retval == 1)
                    {
                        //Convert bytes to the class
                        meCurrent = new MODULEENTRY32(meBytes);
                        //New instance
                        ModuleEntry module = new ModuleEntry(meCurrent);

                        moduleList.Add(module);

                        retval = ToolhelpAPI.Module32Next(handle, meBytes);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("Exception: " + ex.Message);
                }

                //Close handle
                ToolhelpAPI.CloseToolhelp32Snapshot(handle);

                return(moduleList.ToArray());
            }
            else
            {
                throw new Exception("Unable to create snapshot");
            }
        }