/// <summary> /// Acquires the full path to the process /// </summary> private static void GetProcessFullPath(ProcessInformation processInfo) { processInfo.FullPath = null; processInfo.IsService = false; bool modifiable; using (var processHandle = OpenProcess(processInfo.ID, out modifiable)) { processInfo.Modifiable = modifiable; if (null != processHandle) { IntPtr[] moduleHandles = Interop.EnumProcessModules(processHandle.Value, 1); // Enumerate the modules in the process--the first one is the application which we can get the path from if (moduleHandles != null && moduleHandles.Length > 0) { // Uses size in bytes string moduleFilename; if (Interop.GetModuleFileNameEx(processHandle.Value, moduleHandles[0], out moduleFilename)) { processInfo.FullPath = moduleFilename; } } } } }
public bool Configure(ProcessInformation pinfo) { Text = System.String.Format(Resources.MemoryHackerFormatString, pinfo.ID, pinfo.Name, pinfo.FullPath); m_pm = ProcessModifier.Open((uint) pinfo.ID); if(m_pm == null) return false; UpdateList(); UpdateList(); // The second call is for debugging any problems in this function. updList = new UpdateListDelegate(UpdateList); m_timer = new System.Threading.Timer(m_cb, this, 2000, 2000); return true; }
/// <summary> /// Acquires the name, path, and other details for the process /// </summary> private static ProcessInformation GetProcessInfo(int processID, string idleProcessName, string systemName) { // Get a handle to the process and set some values for the process string processName = (processID == 0) ? idleProcessName : ((processID == 4) ? systemName : null); string fullProcessPath = null; bool modifiable; using (var processHandle = OpenProcess(processID, out modifiable)) { // Get the process name. if (null != processHandle) { IntPtr[] moduleHandles = Interop.EnumProcessModules(processHandle.Value, 1); // The first module is (typically) the application itself, so we can get the information we need from there if (moduleHandles != null && moduleHandles.Length > 0) { string moduleProcessName; if (Interop.GetModuleBaseName(processHandle.Value, moduleHandles[0], out moduleProcessName)) { processName = moduleProcessName; } string moduleFilename; if (Interop.GetModuleFileNameEx(processHandle.Value, moduleHandles[0], out moduleFilename)) { fullProcessPath = moduleFilename; } } } } ProcessInformation result = new ProcessInformation(processID, processName); result.Modifiable = modifiable; result.FullPath = fullProcessPath; return result; }
public bool EnumDesktopWindowsDelegate(IntPtr hwnd, IntPtr lParam) { uint processId = 0; uint threadId = Interop.GetWindowThreadProcessId(hwnd, out processId); bool visible; DateTime windowThreadDate = DateTime.MinValue; string windowTitle; if (threadId == 0 || processId == 0) // We somehow mystically failed to get one of the thread or process ids { return true; } // Get window information var maybeWindowInfo = Interop.GetWindowInfo(hwnd); if (!maybeWindowInfo.HasValue) { return true; } var windowInfo = maybeWindowInfo.Value; // There are some classes of windows that I don't want to display so skip them if (windowInfo.dwExStyle.HasFlag(WindowExtendedStyle.ToolWindow) || 0 != (windowInfo.dwStyle & (WindowStyle.Popup | WindowStyle.Child | WindowStyle.Disabled))) { return true; } // Get the text of the window windowTitle = Interop.GetWindowText(hwnd); if (string.IsNullOrEmpty(windowTitle)) { return true; } // Flag whether the window is visible. There are some that I still want to display unfortunately // which means I'll also have a bunch of others that I don't want to display in the list visible = (windowInfo.dwStyle & (WindowStyle.Minimize | WindowStyle.Visible)) != 0; DateTime threadExitedTime, threadKernelTime, threadUserTime; // I use the thread information to determine when the window is created. Typically the first window // created is really the interesting one so I sort them based on date created using (var threadHandle = Interop.OpenThreadHandle(ThreadAccess.QueryInformation, false, threadId)) { if (threadHandle != null) { Interop.GetThreadTimes(threadHandle.Value, out windowThreadDate, out threadExitedTime, out threadKernelTime, out threadUserTime); } } ProcessFriendlyName windowFriendlyName = new ProcessFriendlyName(windowTitle, windowThreadDate, visible); ProcessInformation processInfo = null; if (!mProcesses.TryGetValue((int)processId, out processInfo)) { processInfo = new ProcessInformation((int)processId, string.Empty); mProcesses[(int)processId] = processInfo; } processInfo.Add(windowFriendlyName); return true; }
private static void UpdateProcessInformation(SortedList<int, ProcessInformation> processes, ENUM_SERVICE_STATUS_PROCESS serviceStatus) { uint processId = serviceStatus.ServiceStatus.processId; if (processId == 0) { return; } ProcessInformation processInfo = null; if (!processes.TryGetValue((int)processId, out processInfo)) { processInfo = new ProcessInformation((int)processId, string.Empty); processes[(int)processId] = processInfo; } processInfo.Add(new ProcessFriendlyName(serviceStatus.pDisplayName)); processInfo.IsService = true; }
private static SortedList<int, ProcessInformation> QueryToolHelp(string idleProcessName, string systemName) { SortedList<int, ProcessInformation> result = new SortedList<int, ProcessInformation>(); foreach (var processEntry in Interop.SnapProcesses()) { // Set up the structures ProcessInformation processInfo = new ProcessInformation((int) processEntry.th32ProcessID, processEntry.szExeFile); if (processInfo.ID == 0) // The idle process has the zero id, so save myself some work by special casing it { processInfo.Add(new ProcessFriendlyName(idleProcessName)); } else if (processInfo.ID == 4) { processInfo.Add(new ProcessFriendlyName(systemName)); } GetProcessFullPath(processInfo); result[processInfo.ID] = processInfo; } return result; }