How to create a dummy windows exe runs in the background

One way to prevent a program from being launched is to delete the executable file and create a folder with the same name. In some cases, this works, in other cases instead of lauching the program, the explorer window of the folder is popped up repeatedly. This is still annoying.

The best solution will be to replace the executable file with a dummy windows program of the same name that will run in the background without anything noticeable in the front.

Here is a solution, build it with MingW with the build command line in the comment.

 
#include <windows.h>
 
// to build g++ -o dummywin.exe dummywin.c -mwindows
int APIENTRY WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPTSTR    lpCmdLine,
    int       nCmdShow
)
{
    MSG msg;
 
    HANDLE hSingleInstanceMutex = CreateMutex(NULL, TRUE, "SingleInstanceMutex");
    DWORD dwError = GetLastError();
    if (dwError == ERROR_ALREADY_EXISTS)
    {
      return 0;
    }
 
    while (GetMessage(&msg, NULL, 0, 0))
    return 0;
}
 
 

Sometimes the program will be repeately launched with a timer. To prevent it from launching a bunch of processes, we wish there is only one instance running.

On 16 bit system, we can check the previous instance to determine whether an instance is already running, but from win32, this parameter is always zero. To achieve the single instance we need a system wide indicator. A mutex is a good way to do it. The code above illustrates how to use CreateMutex to set up a global mutex to coordinate between different instances of the same executable.

With this tool, you can now completely mute any unwanted executable and get rid of annoying interruptions.

If there are second or third occasion where a dummy window executable is needed, we need to use a different mutex name and recompile the source code, otherwise, the second or third one will be unable to launch. A better way is to use the name of the executable file name as mutex name, so there is no conflict.

 
#include <stdio.h>
#include <windows.h>
 
// to build g++ -o dummywin.exe dummywin.c -mwindows
 
void get_filename(char *path, char *name)
{
    int i,j = 0;
    for(i = 0; path[i]; i ++)
        if(path[i] == '\\') j = i;
    strcpy(name, &path[j+1]);
}
 
 
int APIENTRY WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPTSTR    lpCmdLine,
    int       nCmdShow
)
{
    MSG msg;
 
    TCHAR lpFileName[MAX_PATH];
    GetModuleFileName(NULL, lpFileName, MAX_PATH);
 
    TCHAR lpExeName[MAX_PATH];
    get_filename(lpFileName, lpExeName);
 
    HANDLE hSingleInstanceMutex = CreateMutex(NULL, TRUE, strcat(lpExeName, "Mutex"));
    DWORD dwError = GetLastError();
    if (dwError == ERROR_ALREADY_EXISTS)
    {
      return 0;
    }
 
    while (GetMessage(&msg, NULL, 0, 0))
    return 0;
}
 
 

Build once, and change the executable file name after it's copied to destination, each one will has it's own mutex object with different name.