How To Create a Self-Restartable Application, from CodeProject.

This article describes the way to add restarting support to your Win32 applications. Key pointer is as followings:

Old Instance Create a Mutex and create a new process with command retrieved by GetModuleFileName().

g_RA_hMutexOtherRestarting = ::CreateMutex( NULL, TRUE, RA_MUTEX_OTHER_RESTARTING);
TCHAR szAppPath[MAX_PATH] = {0};
::GetModuleFileName(NULL, szAppPath, MAX_PATH);
CreateProcess(NULL, szAppPath, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);

New Instance wait for this mutex for release which means old instance has been dead.

// Releasing mutex signal that process finished
DWORD dwWaitResult = WaitForSingleObject(g_RA_hMutexOtherRestarting, 0);
if (dwWaitResult == WAIT_TIMEOUT)
::ReleaseMutex(g_RA_hMutexOtherRestarting);
::CloseHandle(g_RA_hMutexOtherRestarting);

Popularity: 31% [?]