コード例 #1
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");
            }
        }
コード例 #2
0
		/// <summary>
		/// Rerieves an array of all running processes
		/// </summary>
		/// <returns></returns>
		public static ProcessEntry[] GetProcesses()
		{
			ArrayList procList = new ArrayList();

			IntPtr handle = Util.CreateToolhelp32Snapshot(Util.TH32CS_SNAPPROCESS, 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();
					//Get the first process
					int retval = Util.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 = Util.Process32Next(handle, peBytes);
					}
				}
				catch(Exception ex)
				{
					throw new Exception("Exception: " + ex.Message);
				}
				
				//Close handle
				Util.CloseToolhelp32Snapshot(handle); 
				
				return (ProcessEntry[])procList.ToArray(typeof(ProcessEntry));

			}
			else
			{
				throw new Exception("Unable to create snapshot");
			}


		}