Displaying a Splash Screen with C++ (Part III)

This is Part III of a series on creating a splash screen application in native code. For more information, see the Introduction and the Code License.

Part III: Launching the Application

Now that the splash screen is displayed, we need to launch the actual WPF application. This part is fairly straightforward: we just need to build the path to the executable file, then call CreateProcess to execute it.

HANDLE LaunchWpfApplication()
{
    // get folder of the current process
    TCHAR szCurrentFolder[MAX_PATH] = { 0 };
    GetModuleFileName(NULL, szCurrentFolder, MAX_PATH);
    PathRemoveFileSpec(szCurrentFolder);

    // add the application name to the path
    TCHAR szApplicationPath[MAX_PATH];
    PathCombine(szApplicationPath, szCurrentFolder, _T("App.exe"));

    // start the application
    STARTUPINFO si = { 0 };
    si.cb = sizeof(si);
    PROCESS_INFORMATION pi = { 0 };
    CreateProcess(szApplicationPath, NULL, NULL, NULL, FALSE, 0, NULL, szCurrentFolder, &si, &pi);
    return pi.hProcess;
}

This method returns a handle to the launched process; we will use this in the next installment to quit the splash screen application if the WPF application exits early (presumably due to an unexpected failure). I’ll also show how to create an event that the WPF application can use to signal to the splash screen application that the WPF UI has been displayed and that the splash screen should be closed.

The CreateProcess documentation says that the handles in the PROCESS_INFORMATION structure must be closed when they are no longer needed, but the PROCESS_INFORMATION documentation lets us know that they will automatically be closed when the splash screen process exits; thus, there is no need to specially track the thread handle just so that it can be closed.

Posted by Bradley Grainger on September 26, 2008