How to Silently Check for Updates

This article will teach you how to use wyUpdate in "standalone mode" to silently check for updates and, if updates are found, launch wyUpdate to begin updating. This is just a part of the larger step-by-step walkthrough. So if you haven't read the walkthrough you should start there.

To silently check for updates all you need to do is run wyUpdate with the "/quickcheck" switch:

wyUpdate.exe /quickcheck

wyUpdate will start hidden, and only show itself if there are updates available. Otherwise it will silently close itself and return 0.

Errors

By default, if an error occurs wyUpdate shows itself. However, if you would rather wyUpdate just returns '1' on an error rather than showing itself, then pass the "/noerr" commandline switch:

wyUpdate.exe /quickcheck /noerr

Silently return on new updates

You can also choose to keep wyUpdate completely silent even if new updates are found:

wyUpdate.exe /quickcheck /justcheck /noerr

wyUpdate will return 0 if no updates are found, 1 if an error occurred, and 2 if there are updates available.

Skipping the update info screen

If you want to silently check for updates then jump right into installing the update, first run wyUpdate in silent mode and check if the return code is "2":

wyUpdate.exe /quickcheck /justcheck /noerr

If the return code is "2" this means there is an update. From there you can prompt the user within your app if they want to update. If they agree, then launch wyUpdate with the "/skipinfo" commandline argument:

wyUpdate.exe /skipinfo

This "/skipinfo" argument will tell wyUpdate to skip the "update information" page and instead jump straight into downloading and installing the update.

Silently updating a service

If your app is a Windows Service, and you want wyUpdate to silently update it, then read the "How to Silently update a Windows Service" article instead.

Example C++ code

Below is a C++ sample to that launches wyUpdate to silently check for updates. Then, if updates are found, the app closes itself and relaunches wyUpdate to install the new update.

#include "Shlwapi.h"
#pragma comment (lib, "Shlwapi.lib")

void GetModuleDirectory(LPTSTR szPath)
{
    GetModuleFileName(NULL, szPath, MAX_PATH);

    LPTSTR pSlash = _tcsrchr(szPath, '\\');

    if (pSlash == 0)
        szPath[2] = 0;
    else
        *pSlash = 0;
}

bool UpdateAvailable()
{
    // Get the real path to wyUpdate.exe
    // (assumes it's in the same directory as this app)
    TCHAR szPath[MAX_PATH];
    szPath[0] = '\"';
    GetModuleDirectory(&szPath[1]);
    PathAppend(szPath, _T("wyUpdate.exe\" /quickcheck /justcheck"));


    STARTUPINFO si = {0}; si.cb = sizeof(si);
    PROCESS_INFORMATION pi = {0};

    // start wyUpdate
    if (!CreateProcess(NULL, szPath, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
    {
        MessageBox(0, _T("Failed to launch wyUpdate."), _T("Error..."), MB_OK);
        return false;
    }

    // Wait until child process exits.
    WaitForSingleObject(pi.hProcess, INFINITE);

    // Get the exit code
    DWORD exitcode = 0;
    GetExitCodeProcess(pi.hProcess, &exitcode);

    // Close process and thread handles.
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);

    // exitcode of 2 means update available
    return exitcode == 2;
}

Updating in a GUI app

To check for updates in a GUI app you can use it like this:

if (UpdateAvailable())
{
    //TODO: subtly notify the user that there's updates available
    //TODO: don't use a message box in production - it's far too abrasive.
    if (MessageBox(0, _T("Update Available. Click OK start updating."),
        _T("Update available"), MB_OKCANCEL|MB_ICONINFORMATION|MB_TOPMOST) == IDOK)
    {
        TCHAR szPath[MAX_PATH];

        GetModuleDirectory(szPath);
        PathAppend(szPath, _T("wyUpdate.exe"));

        // Start the wyUpdate and Quit
        ShellExecute(NULL, NULL, szPath, NULL, NULL, SW_SHOWNORMAL);

        exit(1);
    }
}