0

I am implementing an automatically updater which need to close the current running application and start the installer.

The code I used is :

if (ExecuteAsAdmin(m_filePath))
    PostQuitMessage(0);

BOOL ExecuteAsAdmin( LPCTSTR filePath )
{
    SHELLEXECUTEINFO shExecInfo = {0};

    shExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);

    shExecInfo.fMask = SEE_MASK_CLASSNAME;
    shExecInfo.lpClass = _T("exefile");
    shExecInfo.hwnd = NULL;
    shExecInfo.lpVerb = _T("runas");
    shExecInfo.lpFile = filePath;
    shExecInfo.lpParameters = NULL;
    shExecInfo.lpDirectory = NULL;
    shExecInfo.nShow = SW_NORMAL;
    shExecInfo.hInstApp = NULL;

    return ShellExecuteEx(&shExecInfo);
}

Is this good enough?

simon
  • 15,344
  • 5
  • 45
  • 67
Jichao
  • 40,341
  • 47
  • 125
  • 198
  • What if the installer tries to write the executable before the original process has quit? – David Heffernan May 21 '13 at 08:53
  • @DavidHeffernan: The installer would first check whether the application is running. if it is running, the installer will notify user to first close the application before continue. – Jichao May 21 '13 at 09:14
  • Yes, I want to give the user a chane to save their work. I think maybe it is good enough. – Jichao May 22 '13 at 06:32
  • PostQuitMessage won't let users save data. And you need to make sure that the user has done that before you fire off the updater. I'd say that your entire design is wrong. – David Heffernan May 22 '13 at 06:34
  • @David, But he can just handle the `WM_QUIT` message to check if saving is necessary, save it and pass the message along. Would that not be okay? – Anish Ramaswamy May 22 '13 at 06:42
  • 2
    @AnishRam Not really. Send `WM_CLOSE` to main window and ask the question in response to that. Give user opportunity to save. Only if user is happy to proceed do you then call `DestroyWindow`. `PostQuitMessage` is not the right approach. – David Heffernan May 22 '13 at 07:12
  • related link: http://stackoverflow.com/questions/7562335/what-is-the-correct-way-to-programmatically-quit-an-mfc-application – Jichao May 22 '13 at 07:44

1 Answers1

2

You don't necessarily have to quit your application.

In Windows, you can't overwrite DLLs and EXEs while they're in use. However, you can rename them.

So, your application can rename all it's binaries and run the installer. The installer can run simultaneously without affecting the application (except for other text files, but I'll get to that). This way, the installer can do whatever it wants (since all the current binaries are renamed).

After installation, your application can notify the user that changes will be displayed only upon a restart.

Before restarting, save your current settings (if any) to replace the stock files that would have been installed by the installer. Also, make sure that your application deletes the renamed files on restart.

Hope this helps!

Anish Ramaswamy
  • 2,326
  • 3
  • 32
  • 63
  • Good to hear this solution, but it is too much complicated for my application. – Jichao May 22 '13 at 06:34
  • @Jichao, You wouldn't have to write ALL that code by yourself though :). Codeproject has a bunch of decent articles which provide some decent code to do about 70-80% of the job. – Anish Ramaswamy May 22 '13 at 06:40