Retrieve a complete processes list using C# - c#

I am trying to write a C# program to retrieve a complete process list. However I find that an application open a window but I don't see it in the process tab of Windows task manager, I see it in task tab. In addition, I also cannot get its information using my C# code.
static void showProcesses()
{
Process[] procs = Process.GetProcesses();
foreach (Process proc in procs)
{
Console.WriteLine(proc.ProcessName);
}
}
I browsed many forums but I can only find the methods to hide a process, and I don't find any method for showing hidden processes. Do anyone have idea how to retrieve hidden process information?

There are no hidden processes on Windows. Only processes you do not have (security) rights to see.
A process running as an administrator (in Vista/Win7/Win2k8 would need to be elevated) will always be able to see all processes.
However, a console application that lists the processes may well exit before Task Manager's display refreshes, and thus won't be seen. This is likely with a simple program even with update speed set to "high".
You need to keep your process around until Task manager has updated its display. The simplest way would be do add the following statements to the end of your Main method:
Console.Write("Press ENTER to exit");
Console.ReadLine();

I'm not sure what you mean. The code above lists the same number of processes as pslist. When you talk about methods to hide a process are you talking about root kits? If so they usually work by changing how the list commands work. I.e. the processes are in fact being enumerated, but the info is not displayed to the user.

it works just fine all you need is add :
Console.Write("Press ENTER to exit");
Console.ReadLine();
at the end or start project with ctrl + F5

Related

Kill a specific event handle of a running process?

I am trying to close a specific singleton handle associated with a process. The windows application "Process Explorer" allows you to do this but you need to select the process, then right click on the handle and select close. I am after a solution that will allow me to auto close the specific handle when the application is running.
I know I can get all processes and even kill a process by simply doing:
foreach (var p in Process.GetProcessesByName("your.exe"))
{
p.Kill();
}
But how would I go about closing a handle attached the process and not the process itself?
I ended up using NtDuplicateObject as Michael suggested in the comments. It was a bit of a pain to implement but everything works as expected - thanks Michael!

Detect a process exit in C#

I've an application which does
Process.Start()
to start another application 'firefox.exe'. I want to wait till that application ends (process dies) and continue my execution (example: Show a messagebox). There may be multiple instances of the application 'firefox.exe' running at the same time.
I have try WaitForExit and HasExited, but it return true right after firefox process start, so that the Messagebox show immediately.
How can i Show the messagebox in this situtation.
Update 1:
I tried this:
Process browser = new Process();
browser= Process.Start(#"dist\bin\firefox.exe");
browser.StartInfo.UseShellExecute = false;
browser.WaitForExit();
MessageBox.Show("AAAA!!!");
and HasExited similarly with EventRaising.
Update 2:
I have tried with many Simple Program like Notepad.exe, cmd.exe... All of them worked well. I think that Firefox call another process before running the main process and closing the original process. How can i bypass this problem.
If you can be certain that no firefox instance is already running you can simply use Process.GetProcessesByName("firefox"); and use WaitForExit on these processes.
If you want to handle multiple concurrent firefox instances you have a bit of a problem since firefox manages multiple processes in its own special ways. When you start firefox it might just ask the existing firefox process to create a new window and then quit. So you would need to detect new windows and monitor when this window is closed. I do not think there is any way to accomplish what you want simply by waiting on processes, at least not without cooperation by the program.
In the end I would try to do whatever you are trying to accomplish some other way. Perhaps you can host a webcontrol inside the application? Or perhaps embed a copy of Firefox Portable or some other stand alone browser in your application and start that instead?

.NET How to check if a Windows process is running as an "App" or as a "Background application"

On Windows 8.1 you go into the task manager and check the list of processes, there are two lists:
- One for "Apps", which are visible foreground apps
- One for "Background processes", which are processes running in the background
My end goal is to time how long it takes an application to load. When the application is still loading, it appears in "Background processes". However, once loaded, it appears in "Apps". This is going to be my criteria on what constitutes an app finishing loading.
I am using a System.Diagnostics.Process object to try to accomplish this. However, I am struggling to come up with a way to distinguish between a Process under "Background processes" and a Process under "Apps".
Does anyone have an idea on how to make this distinction? I looked through MSDN and tried different methods, none of which have been successful.
The property System.Diagnostics.Process.MainWindowHandle is zero when process has not UI (i.e. is background process).
Normally, if a process is an "App", it should have its own window's name, otherwise, it is a "Background application". Thus the code should be as follow:
Process[] arrProcess = Process.GetProcesses();
foreach (Process process in arrProcess)
{
if (!string.IsNullOrEmpty(process.MainWindowTitle))
{
//Do something with your App
}
else
{
//Do something with your Background process
}
}
Services are also usually created by SYSTEM user - the column "User Name" in task manager.

How can I obtain a list of currently running applications?

I want to programmatically obtain a list of running (desktop) applications, and then I want to display this list to the user. It should be something similar to the application list displayed in the Windows Task Manager.
How can I create this in C#? Specifically, I need a way to obtain that list of currently running applications.
You can use the Process.GetProcesses method to provide information about all of the processes that are currently running on your computer.
However, this shows all running processes, including ones that are not necessarily shown on the taskbar. So what you'll need to do is filter out those processes that have an empty MainWindowTitle.The above-linked documentation explains why this works:
A process has a main window associated
with it only if the process has a
graphical interface. If the associated
process does not have a main window
(so that MainWindowHandle is zero),
MainWindowTitle is an empty string
("").
So, you could use something like the following code, which will print out (to a console window) a list of all currently running applications that are visible on your taskbar:
Process[] processes = Process.GetProcesses();
foreach (var proc in processes)
{
if (!string.IsNullOrEmpty(proc.MainWindowTitle))
Console.WriteLine(proc.MainWindowTitle);
}

C# Detecting Spawned Processes

I'm writing a piece of c# code that launches an installer and waits for it to return before continuing with other stuff.
I'm having trouble with certain installers that spawn other processes with the original process returning before the install has actual finished. Is there some way that I can wait until all the processes have finished?
To clarify here's the scenario I'm having trouble with:
Launch Installer1
Installer1 spawns/launches another installer (Installer2)
Installer 1 returns
Application thinks install has finished but Installer2 is still running. This causes issues with workflow in the app.
Here's the code I'm using at the moment:
// launch installer
Process process = windowsApplicationLauncher.LaunchApplication(_localFilePath);
// wait for process to return
do
{
if (!process.HasExited)
{
}
}
while (!process.WaitForExit(1000));
if (process.ExitCode == 0)
{
_fileService.MoveFile(_localFilePath, _postInstallFilePath);
_notification.SetComplete(false);
return true;
}
return false;
Have you thought about using WMI to solve this problem?
You can use WMI to listen for process creation and deletion events. Question 967668 has a good example.
When you receive a process creation event, you could issue a WMI query to determine if the process is a child (or a child of a child etc) of your root installer with something like the following:
"SELECT * FROM Win32_Process WHERE ParentProcessId=".
It might be better to do it this way inside the do / while loop:
System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcessesByName(proc.ProcessName, Environment.MachineName);
Then iterate through the procs to find out which is still running...by using the HasExited property...
The logic being that the process's subprocesses are owned by your code, so you could check first if they have exited or not, if not, keep looping...
Hope this helps,
Best regards,
Tom.

Categories

Resources