How to Stop an Application from Opening

How Do I Stop An Application From Opening

Rather than trying to kill the process when it runs, how about stopping it from running in the first place?

Changing what happens when the shell tries to launch an application is simple - add a new registry key to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options

To test this I added a registry key called notepad.exe and within this the string value Debugger with the value calc.exe. Now whenever I try and run notepad calc opens. The following is the exported registry key.

REGEDIT4

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\notepad.exe]
"Debugger"="calc.exe"

Having made this change I've not yet managed to open notepad, not bad for a no code solution. If you need to be 100% certain that the application never runs you could always add a "kill" solution too as detailed by others.

Way to stop a program from starting up using c#?

Well, you can definitely determine which programs are running by looking for the process names you want (GetProcessesByName()) and killing them.

Process[] processes = Process.GetProcessesByName(processName);
foreach(Process process in processes)
{
process.Kill();
}

You could just have a list of them you didn't want to run, do the time check (or whatever criteria was to be met) and then walk the list. I did something like this once for a test and it works well enough.

Stop user from opening an already program using PowerShell

Since Windows doesn't have such a feature that prevents launching multiple copies of an executable, you have two options:

  1. poll process list every now and then. Terminate extra instances of the application
  2. create a wrapper to the application and use a mutex to prevent multiple copies

The first option has its caveats. If additional copies are launched, it takes on the average half the polling interval to detect those. What's more, which of the processes are to be terminated? The eldest? The youngest? Some other criteria?

The second one can be circumvented easily by just launching the application itself.

The only real solution is to implement a single-instance feature in the application itself. Games often do this. For business software, be wary that the users will hate you, if there is a single reason why running multiple instances would be of use. Yes, especially if that use case would be absurd.

As an example of a mutex-based launcher, consider the following function

function Test-Mutex {
$mtx = new-object System.Threading.Mutex($false, "SingleInstanceAppLauncher")

if(-not $mtx.WaitOne(0, $false)) {
write-host "Mutex already acquired, will not launch second instance!"
read-host "Any key"
return
}
write-host "Running instance #1"
read-host "Any key"
# Do stuff
}

As like the solution 2 caveat, any user can work around the limit by just executing the do suff part. Remember, the wrapper prevents launching multiple instances of the wrapper, not about the do stuff.

How can I prevent launching my app multiple times?

At program startup check if same process is already running:

using System.Diagnostics;

static void Main(string[] args)
{
String thisprocessname = Process.GetCurrentProcess().ProcessName;

if (Process.GetProcesses().Count(p => p.ProcessName == thisprocessname) > 1)
return;
}

How to close particular aplication after opening it in c++?

You can use ShellExecuteEx with SEE_MASK_NOCLOSEPROCESS, then you can pass it as a parameter it to the TerminateProcess function.

Here is a sample:

#include <windows.h>

int main()
{
SHELLEXECUTEINFO lpExecInfo{};
lpExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
lpExecInfo.lpFile = L"notepad.exe";
lpExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
lpExecInfo.hwnd = NULL;
lpExecInfo.lpVerb = NULL;
lpExecInfo.lpParameters = NULL;
lpExecInfo.lpDirectory = NULL;
lpExecInfo.nShow = SW_SHOWNORMAL;
ShellExecuteEx(&lpExecInfo);

Sleep(3000);
if (lpExecInfo.hProcess)
{
TerminateProcess(lpExecInfo.hProcess, 0);
CloseHandle(lpExecInfo.hProcess);
}
return 0;
}


Related Topics



Leave a reply



Submit