⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 process.cs

📁 蓝牙通讯
💻 CS
📖 第 1 页 / 共 2 页
字号:
				//raise exited event
				OnExited();

				return true;
			}
			else
			{
				return false;
			}
		}
		#endregion


		#region Handle
		/// <summary>
		/// Returns the associated process's native handle.
		/// </summary>
		/// <value>The handle that the operating system assigned to the associated process when the process was started.
		/// The system uses this handle to keep track of process attributes.</value>
		public IntPtr Handle
		{
			get
			{
				//if not valid handle
				if(m_pinfo.hProcess == IntPtr.Zero)
				{
					if(m_pinfo.dwProcessID != 0)
					{
						

						//populate handle
						m_pinfo.hProcess = OpenProcess(0, false, (IntPtr)m_pinfo.dwProcessID);
					}
					else
					{
						//no handle or id available
						return IntPtr.Zero;
					}
				}

				return m_pinfo.hProcess;
			}
		}
		#endregion

		#region Id
		/// <summary>
		/// Gets the unique identifier for the associated process.
		/// </summary>
		/// <remarks>The system-generated unique identifier of the process that is referenced by this <see cref="Process"/> instance.</remarks>
		public int Id
		{
			get
			{
				return m_pinfo.dwProcessID;
			}
		}
		#endregion

		#region Has Exited
		/// <summary>
		/// Gets a value indicating whether the associated process has been terminated.
		/// </summary>
		/// <remarks>true if the operating system process referenced by the <see cref="Process"/> component has terminated; otherwise, false.</remarks>
		public bool HasExited
		{
			get
			{
				return m_exited;
			}
		}
		#endregion

		#region Exit Code
		/// <summary>
		/// Gets the value that the associated process specified when it terminated.
		/// </summary>
		/// <remarks>The code that the associated process specified when it terminated.</remarks>
		public int ExitCode
		{
			get
			{
				int exitcode = 0;
				if(GetExitCodeProcess((IntPtr)m_pinfo.hProcess, ref exitcode))
				{
					if(exitcode==StillActive)
					{
						throw new System.InvalidOperationException(" The process has not exited.");
					}
					else
					{
						//process is definately exited;
						m_exited = true;

						return exitcode;
					}
				}
				else
				{
					throw new System.InvalidOperationException("The process OpenNETCF.Diagnostics.Process.Handle is not valid.");
				}
			}
		}
		#endregion

		#region Start Info
		/// <summary>
		/// Gets or sets the properties to pass to the <see cref="Process.Start"/> method of the <see cref="Process"/>.
		/// </summary>
		/// <value>The <see cref="ProcessStartInfo"/> that represents the data with which to start the process.
		/// These arguments include the name of the executable file or document used to start the process.</value>
		/// <exception cref="T:System.ArgumentException">The value that specifies the <see cref="Process.StartInfo"/> is null.</exception>
		/// <remarks>StartInfo represents the set of parameters to use to start a process.
		/// When <see cref="Process.Start"/> is called, the <see cref="Process.StartInfo"/> is used to specify the process to start.
		/// The only necessary StartInfo member to set is the <see cref="ProcessStartInfo.FileName"/> property.
		/// The <see cref="ProcessStartInfo.FileName"/> property does not need to represent an executable file.
		/// It can be of any file type for which the extension has been associated with an application installed on the system.
		/// For example the FileName can have a .txt extension if you have associated text files with an editor, such as Notepad, or it can have a .pwd if you have associated .pwd files with a word processing tool, such as Microsoft Pocket Word.</remarks>
		public ProcessStartInfo StartInfo
		{
			get
			{
				return m_pstart;
			}
			set
			{
				if(value!=null)
				{
					m_pstart = value;
				}
				else
				{
					throw new ArgumentException("The value that specifies the OpenNETCF.Diagnostics.Process.StartInfo is null.");
				}
			}
		}
		#endregion


		#region Exited
		/// <summary>
		/// Occurs when a process exits.
		/// </summary>
		/// <remarks>Note that in this partial implementation the Exited event will only be raised when a <see cref="Process"/> is explictly Killed.
		/// The <see cref="Process"/> class will not watch created processes unless you use the <see cref="Process.WaitForExit"/> method.</remarks>
		public event System.EventHandler Exited;

		/// <summary>
		/// Raises the <see cref="E:OpenNETCF.Diagnostics.Process.Exited"/> event.
		/// </summary>
		protected void OnExited()
		{
			//set exited flag
			this.m_exited = true;

			if(Exited!=null)
			{
				//raise Exited event
				Exited(this, new EventArgs());
			}
		}
		#endregion

		private static uint GetPUserKData()
		{
			uint			PUserKData	= 0;
			Core.SystemInfo si			= Core.GetSystemInfo();
			
			switch(si.wProcessorArchitecture)
			{
				case Core.ProcessorArchitecture.ARM:
					PUserKData = PUserKDataARM;
					break;
				case Core.ProcessorArchitecture.Intel:
					PUserKData = PUserKDataX86;
					break;
				default:
					throw new NotSupportedException("Unsupported on current processor: " + EnumEx.GetName(typeof(Core.ProcessorArchitecture), si.wProcessorArchitecture));
			}

			return PUserKData;
		}

		/// <summary>
		/// This function returns the process identifier of the calling process.
		/// </summary>
		/// <returns>The return value is the process identifier of the calling process.</returns>
		public static int GetCurrentProcessID()
		{
			IntPtr pBase = new IntPtr((int) (GetPUserKData() + SYSHANDLE_OFFSET));
			return Marshal.ReadInt32(pBase, SH_CURTHREAD * 4);
		}

		/// <summary>
		/// This function returns the thread identifier, which is used as a handle of the calling thread. 
		/// </summary>
		/// <returns>The thread identifier of the calling thread indicates success.</returns>
		public static int GetCurrentThreadID()
		{
			IntPtr pBase = new IntPtr((int) (GetPUserKData() + SYSHANDLE_OFFSET));
			return Marshal.ReadInt32(pBase, SH_CURPROC * 4);
		}

		/// <summary>
		/// Gets a new <see cref="Process"/> component and associates it with the currently active process.
		/// <para><b>New in v1.1</b></para>
		/// </summary>
		/// <returns>A new <see cref="Process"/> component associated with the process resource that is running the calling application.</returns>
		/// <remarks>Use this method to create a new Process instance and associate it with the process resource on the local computer.</remarks>
		public static IntPtr GetCurrentProcess()
		{
			return new IntPtr(SH_CURPROC + SYS_HANDLE_BASE);
		}

		/// <summary>
		/// This function returns a pseudohandle for the current thread. 
		/// </summary>
		/// <returns>The return value is a pseudohandle for the current thread.</returns>
		/// <remarks>
		/// <p>A pseudohandle is a special constant that is interpreted as the current thread handle. </p>
		/// <p>The calling thread can use this handle to specify itself whenever a thread handle is required.This handle has the maximum possible access to the thread object.</p>
		/// <p>The function cannot be used by one thread to create a handle that can be used by other threads to refer to the first thread. The handle is always interpreted as referring to the thread that is using it.</p>
		/// <p>The pseudohandle need not be closed when it is no longer needed. Calling the CloseHandle function with this handle has no effect.</p>
		/// </remarks>
		public static IntPtr GetCurrentThread()
		{
			return new IntPtr(SH_CURTHREAD + SYS_HANDLE_BASE);
		}
		#region Process P/Invokes

		[DllImport("coredll", EntryPoint="ShellExecuteEx", SetLastError=true)]
		private extern static bool ShellExecuteEx( ShellExecuteInfo ex );

		[DllImport("coredll", EntryPoint="CreateProcess", SetLastError=true)]
		private extern static bool CreateProcess(string pszImageName, string pszCmdLine, IntPtr psaProcess, IntPtr psaThread, int fInheritHandles, int fdwCreate, IntPtr pvEnvironment, IntPtr pszCurDir, IntPtr psiStartInfo, ProcessInfo pi);

		[DllImport("coredll", EntryPoint="OpenProcess", SetLastError=true)]
		internal extern static IntPtr OpenProcess(uint fdwAccess, bool fInherit, IntPtr IDProcess);

		[DllImport("coredll.dll", EntryPoint="CloseHandle", SetLastError=true)] 
		internal static extern int CloseHandle(IntPtr hObject);

		[DllImport("coredll.dll", EntryPoint="TerminateProcess", SetLastError=true)]
		internal extern static bool TerminateProcess(IntPtr hProcess, int uExitCode);

		[DllImport("coredll.dll", EntryPoint="WaitForSingleObject", SetLastError = true)]
		private static extern int WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds); 

		[DllImport("coredll.dll", EntryPoint="GetExitCodeProcess", SetLastError=true)]
		private extern static bool GetExitCodeProcess(IntPtr hProcess, ref int lpExitCode);

//		[DllImport("coredll.dll", EntryPoint="GetCurrentProcessId", SetLastError=true)]
//		private extern static int GetCurrentProcessId();

		[DllImport("coredll.dll", EntryPoint="GetWindowThreadProcessId", SetLastError=true)]
		private extern static int GetWindowThreadProcessId(IntPtr hWnd, ref int lpdwProcessId);

		#endregion
		

		#region Process Info
		private sealed class ProcessInfo
		{
			public IntPtr hProcess	= IntPtr.Zero;
			public IntPtr hThread	= IntPtr.Zero;
			public int dwProcessID	= 0;
			public int dwThreadID	= 0;
		}
		#endregion

		#region Shell Execute Info
		private sealed class ShellExecuteInfo
		{

			public UInt32 cbSize		= 60; 
			public UInt32 fMask			= 0x00000040; 
			public IntPtr hwnd			= IntPtr.Zero; 
			public IntPtr lpVerb		= IntPtr.Zero; 
			public IntPtr lpFile		= IntPtr.Zero; 
			public IntPtr lpParameters	= IntPtr.Zero; 
			public IntPtr lpDirectory	= IntPtr.Zero; 
			public int nShow			= 0; 
			public IntPtr hInstApp		= IntPtr.Zero; 
			public IntPtr lpIDList		= IntPtr.Zero; 
			public IntPtr lpClass		= IntPtr.Zero; 
			public IntPtr hkeyClass		= IntPtr.Zero; 
			public UInt32 dwHotKey		= 0; 
			public IntPtr hIcon			= IntPtr.Zero; 
			public IntPtr hProcess		= IntPtr.Zero; 
		}
		#endregion

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -