Click here to Skip to main content
15,390,740 members
Articles / Desktop Programming / WTL
Posted 15 Aug 2006


122 bookmarked

How To Create a Self-Restartable Application

Rate me:
Please Sign up or sign in to vote.
4.71/5 (29 votes)
21 Aug 2006CPOL3 min read
This article describes the way to add restarting support to your Win32 applications.
Application Before Restart
Step 1. Application Before Restart
Application While Restarting
Step 2. Application While Restarting
Application After Restart
Step 3. Application After Restart


In some cases, your application must be restarted to apply some changes. For example, you might want to restart an application after downloading newer versions of files or changing the UI language of the application. For users, it is very convenient not to have to restart an application manually. The best way is to ask the user if the application has to be restarted then or later.

In this article, I describe an easy way to complete this task with a few lines of code.

Using the code

Using the restart code is very simple.

First, include "RestartAPI.h" in your "stdafx.h", and add "RestartAPI.cpp" in your VC project.

The second step is modifying your main or WinMain function like shown below. Another way is to add the restart initialization code to your application start point and add the restart finishing code to the exit point, such as PreMessageLoop/PostMessageLoop in a WTL application. Here is an example of the modified WinMain function.

int WINAPI _tWinMain(HINSTANCE hInstance, 
           HINSTANCE /*hPrevInstance*/, 
           LPTSTR lpstrCmdLine, int nCmdShow)
    // Initialize restart code
    // Check if this instance is restarted and 
    // wait while previos instance finish
    if (RA_CheckForRestartProcessStart())
    // Your WinMain Code here
    // Finish restarting process if needed
    return 0;

The third step is to determine the point where you want to initialize the restarting of an application. In the attached sample, this point is Click handler of the button "Restart Me!". For restarting the application, you must call the RA_ActivateRestartProcess function and terminate your application.

LRESULT CMainDlg::OnBnClickedRestart(WORD /*wNotifyCode*/, 
        WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
    // Initialize restart proceess
    if (!RA_ActivateRestartProcess())
        ::MessageBox(NULL, _T("Something Wrong"), 
                     _T("Restart App"), 
        return 0;
    // Terminate application.
    return 0;

Short functions reference


Command line switch for restarting the application:


You can redefine the command line switch for your purposes. Restarted application is started with this switch:


Mutex unique name

Named mutex is used for waiting termination of the first instance of a process. The mutex name must be unique. For that, you can use GUIDs to define mutex namea like this:


You can redefine a mutex name for your purposes.


BOOL RA_CheckProcessWasRestarted();

Returns TRUE if process was restarted.

BOOL RA_CheckForRestartProcessStart();

Checks the process command line for restart switch. Call this function to check that it is a restarted instance.

BOOL RA_WaitForPreviousProcessFinish();

Wait till the previous instance of the process is finished.

BOOL RA_DoRestartProcessFinish();

Call it when a process finishes.

BOOL RA_ActivateRestartProcess();

Call this function when you need to restart an application. Start another copy of the process with the command line RA_CMDLINE_RESTART_PROCESS. After calling RA_ActivateRestartProcess, you must close an active instance of your application.


Demo project

The demo project includes a sample WTL dialog application that demonstrates how to use the restarting API. From the screenshots, you can see some stages of the application at work.

The first screen shows the application starting normally. In the second screen, the user clicked the "Restart Me!" button and you see the pseudo-termination progress. If you run the Task Manager at that moment, you'll see two instances of RestartableApp.exe. In the last screen, the application is in the normal state after it was restarted.

Source files

Source files include "RestartAPI.h" and "RestartAPI.cpp" which can be used separately in your Win32 application.


  • 10th August 2006
    • Initial public release.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Alexander D. Alexeev
Software Developer (Senior)
Russian Federation Russian Federation
No Biography provided

Comments and Discussions

Questiona simpler solution for MFC users Pin
ckorda2-Jan-19 13:25
Memberckorda2-Jan-19 13:25 
QuestionThanks! Pin
Ivee17-Aug-17 23:43
MemberIvee17-Aug-17 23:43 
QuestionWould it work for Win64 application as well ? Pin
Member 1239835020-Mar-16 23:59
MemberMember 1239835020-Mar-16 23:59 
AnswerRe: Would it work for Win64 application as well ? Pin
GregDude11-Apr-16 19:44
MemberGregDude11-Apr-16 19:44 
QuestionMy vote of 5 Pin
alon13-Mar-16 18:52
Memberalon13-Mar-16 18:52 
GeneralMy vote of 5 Pin
Antoine Megens7-Sep-15 22:46
MemberAntoine Megens7-Sep-15 22:46 
GeneralMy vote of 5 Pin
ANewCoder8-Nov-12 4:55
MemberANewCoder8-Nov-12 4:55 
GeneralSuperb! Pin
Alexander Golde4-Sep-12 22:55
MemberAlexander Golde4-Sep-12 22:55 
QuestionA switch in the command line is really needed? Pin
WebMaster12-Jun-10 4:16
MemberWebMaster12-Jun-10 4:16 
GeneralATL Pin
jorgento200917-Feb-10 22:49
Memberjorgento200917-Feb-10 22:49 
GeneralRe: ATL Pin
Alexander D. Alexeev18-Feb-10 0:50
MemberAlexander D. Alexeev18-Feb-10 0:50 
QuestionRestart exe and Samba symlinks problem Pin
kintz21-Jan-07 22:26
Memberkintz21-Jan-07 22:26 
QuestionRestart Application after *.exe updated Pin
X-View2-Dec-06 19:53
MemberX-View2-Dec-06 19:53 
AnswerRe: Restart Application after *.exe updated Pin
Alexander D. Alexeev4-Dec-06 2:17
MemberAlexander D. Alexeev4-Dec-06 2:17 
QuestionWhat if something hangs? Pin
Hokei29-Aug-06 17:27
MemberHokei29-Aug-06 17:27 
AnswerRe: What if something hangs? Pin
Alexander D. Alexeev29-Aug-06 21:13
MemberAlexander D. Alexeev29-Aug-06 21:13 
GeneralRe: What if something hangs? [modified] Pin
Hokei31-Aug-06 10:52
MemberHokei31-Aug-06 10:52 
GeneralRA_WaitForPreviousProcessFinish function name Pin
_FRED_18-Aug-06 1:44
Member_FRED_18-Aug-06 1:44 
AnswerRe: RA_WaitForPreviousProcessFinish function name Pin
Alexander D. Alexeev21-Aug-06 4:18
MemberAlexander D. Alexeev21-Aug-06 4:18 
QuestionIs it possible to use the code in MFC? Pin
jalal_haddad16-Aug-06 21:12
Memberjalal_haddad16-Aug-06 21:12 
AnswerRe: Is it possible to use the code in MFC? [modified] Pin
Alexander D. Alexeev16-Aug-06 21:45
MemberAlexander D. Alexeev16-Aug-06 21:45 
GeneralRe: Is it possible to use the code in MFC? Pin
Piccinano17-Aug-06 9:28
MemberPiccinano17-Aug-06 9:28 
GeneralRe: Is it possible to use the code in MFC? [modified] Pin
Andrew Truckle14-Nov-18 10:18
professionalAndrew Truckle14-Nov-18 10:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.