preventing a program from starting when another prog is running (Windows) - c#

I have 2 programs. I want to build an external tool, which prevents one of the programs from starting, when the other program is already running, and vice versa.
I can't touch the code of either of the two programs.
I want to do it preferably with C#, but a scripting language will also be ok.
Can anyone help me with the concept how to implement it?
Thanks in advance.

Write your tool as a windows service that keeps running in background and kills the second program if it starts. It can find out when a new process starts by listening to WMI events see .NET Events for Process executable start

There are couple of ways to do that. If you are using C# you should be getting the process name using the GetProcessesByName(). It would look something like this
Process[] processes = Process.GetProcessesByName(processName);
If you know the process name you can just kill the process
Or else use the [Semaphore Class] (http://msdn.microsoft.com/en-us/library/system.threading.semaphore.aspx).

Related

Check if Windows Application is running (not process)

I am hoping to check at the beginning of an automated test if an application is open. I can check if the process is running by doing the following
foreach (Process proc in Process.GetProcesses())
{
if (proc.ProcessName.Contains(name))
{
return true;
}
}
However, the process I want to find starts up about a minute before the application actually opens and is ready to be used by the test methods (its a very slow starting application). The above code sample looks at all windows processes running, but I am wondering, is there a way to do a similar method but to look at windows applications running?
There is a method already in class Process that you can use to check if an app with a UI has fully started:
Process.WaitForInputIdle(int milliseconds)
This will wait up to milliseconds ms for the message loop to become idle (and returns a bool to indicate success status). Depending on the application you're waiting for, you might want to allow 30 seconds or longer.
This might work for you, but be aware that in my experience for some applications it is not totally reliable!
The Windows API documentation has more details about the Windows API function that WaitForInputIdle() calls behind the scenes.
When a process is started, you can say application has started.
What you want is to wait until application startup progress has completed or not.
This means, when process is started, application startup begins. When application startup is completed, is becomes ready for user input. So I think you should have a look at following question and its answers.
Programmatically, how does this application detect that a program is ready for input
Apllication is proces.
If you can modify app, at app start you can create file and at end delete it. So you can chceck file existance. If file exist app starting/started.
If you need info when main form is created use:
WINFORMS
Form.Shown event.
WPF Loaded Event
uITestControl.Exists did the trick for me.
This method will return a boolean value corresponding to the existence of the application window being open. This allows an if statement to be created that can open the application if not already open, or do nothing if its already open.

Kill Control Pannel Applet (appwiz.cpl) from Vb.net Application

I am building a vb.net application which monitors external process and kills the process based on certain conditions. The application works great with monitoring .exe process but i m unable to use it with control pannel items.
Suppose a user launches programs and features , I want my vb.net app to detect it and kill it. Under task manager the process is shown as explorer.exe.
I can successfully launch programs and features using Process.Start(System.Environment.SystemDirectory + "\appwiz.cpl")
but i cannot kill it this way, can anyone tell me how i could kill this process??
Thanks!
You can use the following:
Process.Start("taskkill.exe /im explorer.exe")
Just replace explorer.exe with the process you wanna stop/kill...
Also works with the PID (Process ID):
Process.Start("taskkill.exe /pid /*yourPID*/")
Do not kill applications until there is other way to exit them.
It is like finishing your day by instantly turning off your computer.
First, at least ask applications to close themselves:
Please see Microsoft article How to use Visual Basic .NET or Visual Basic 2005 to close another application how to implement closing request.
Also please have a look at this Q&A how to prevent application to be uninstalled by a user (w/o admin rights)?.

Determine if program crashed [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Detecting process crash in .NET
I'm writing a c# program that have to determine if another C++ game program (let's call it Foobar) is crashed or not.
When the FooBar program crashes it's notifying the user about the crash with a MessageBox, if you OK that windows the program closes.
So I guess I could determine if the program is crashed if that messagebox is opened/active. Problem I dont know how to do that.
Or if there is any other better solution comes to your mind, please share it with me.
Edit:
I can not edit the C++ program, and it's always a possibility that it will crash. I just need to know if it did.
Duplicate of Detecting process crash in .NET.
The heartbeat is probably the way to go, but there is another way.
When a process crashes, Windows first checks to see if a Just-In-Time debugger is configured on your system. If so, you can have that debugger attach itself to the process right before it crashes. Usually you would use this functionality to generate a memory dump as a process crashes. Whatever debugger you attach gets to know the PID and name of the crashing process. You can either utilize features of existing debugging tools, such as ADPlus, or write your own program and tell Windows that that is your Just-In-Time debugger and should be run when a process crashes. I believe you can set up a JIT debugger specifically for any process name.
See http://msdn.microsoft.com/en-us/library/5hs4b7a6(v=VS.80).aspx
I think that if you set the HKLM\Software\Microsoft\Windows NT\Current Version\AeDebug\Debugger registry entry to '"DirectoryOfYourProgram\YourProgram.exe" -p %ld' where YourProgram.exe expects a PID passed in with the -p flag, your program will be called and passed the correct PID when a process crashes.
If you have access to the code for both apps, you could setup a heartbeat message from the c++ app to the c# app, and do whatever when the heartbeats stop. For the comms you would need something like named pipes or similar. Alternatively your c++ could write to a file regularly, which your c# could detect updates to, using file monitoring.

C# Process automation

I'm starting a external application with System.Diagnostics.Process, this external process at one moment opens up a dialog where user has type something and then click OK. What i need is to wait with my application(the one where i started the external process) until the user has inserted something and clicked OK. After that OK i have to do some more task on that external process and then close it.
Yes, it's possible. There are a number of ways to get window information starting with a process handle and/or ID. See this question and responses for getting started. You will most likely end up using P/Invoke to the Win32 API to get this accomplished but there are dozens of good examples for getting this done.
Once you have the window handle you can use a timer polling scheme to test for the presence, or in your case, presence and then the disappearance of a window.
This is possible but there are some work behind it. First you need to run your code as unmanaged code as you will need to hook on Windows OS events with the Win32 API.
So an option would be to have a loop looking for the dialog to open, pause what ever your code are doing and continue when the dialog are gone.
If the application you are starting exists after the user interacts with the dialog, then you can just call Process.WaitFroExit() and your code will not continue until the process you started has quit.
There are quite a few helpful functions for interacting with processes in the System.Diagnostics.Process class (that I assume you are using to start this external application)

C# Hook Windows Processes to Check for Debugging Processes

I was wondering if there is a way I could hook the windows processes to check if any suspicious programs are running like (Wireshark, Fiddler, OllyDBG, etc).
I want to hook the windows processes so it will close the client or pop-up a message in real time when it detects a unwanted process.
If you guys can provide me with any links to doing this that would be nice.
Thanks!
Process[] processlist = Process.GetProcesses();
Then walk the list and do as desired for your apps you do not want to run.
You can detect process creations by using WMI creation events for Win32_Process. An instance of Win32_Process is created with each process, so looking new instances will tell you about process creation in (near) real time.
To receive WMI creation events see this page: http://msdn.microsoft.com/en-us/library/system.management.eventquery.aspx (EDIT: different link, now to sample in C#).
EnumWindows enumerates all top level windows.
And you don't want to inject a C# dll into other processes. This requires the .net runtime to be loaded into that process. This wastes RAM, and if the process is a .net app using a different version of .net then there are versioning problems. Especially if your dll is injected before the process loads its own version of .net.
And what to you want to achieve by injecting into that process you can't achieve from the outside?
You might want to check out EasyHook on CodePlex (http://easyhook.codeplex.com). Here is some discussion where people reportedly have been able to hook into CreateProcess. If you manage to hook into that API function you know of the created process at creation time.

Categories