views:

267

answers:

1

During my application setup, the user has the option of installing a new SQL Server Express 2005 instance in the local machine if they don't want to use an already existing SQL Server 2005 in their network for whatever reason.

As the SQL Server installation is optional, I don't have it as a prerequisite of my installer. Instead, I just bundle the sqlexpr32.exe setup in my install media and programatically launch it with the appropriate command line arguments in order to perform an automatic install. (Note: I'm using the /qb command line flag so the install is not silent, it shows the user interface, but does not ask for any user input). And in case anyone wants to know, I'm following this Microsoft article on how to launch the SQL Server Express setup.

This is what I'm doing in my custom install action:

// All this runs on a background thread so the user
// can cancel my app's setup at any time

// Launch the installer
Process setupProcess = new Process();
setupProcess.StartInfo.FileName = "sqlexpr32.exe";
setupProcess.StartInfo.Arguments = " a bunch of command line args here";
setupProcess.StartInfo.UseShellExecute = false; // to avoid a shell window
setupProcess.Start();

// At this point the SQL Server installer is running

// Monitor the process on 2-second intervals:
while (!setupProcess.WaitForExit(2000))
{
    if(WasCancelled) // flag that is set when the user cancels my app's setup
    {
        // This following line is my problem. Sending CloseMainWindow does not 
        // seem to work. The SQL Server installer just keeps running.
        setupProcess.CloseMainWindow();
        setupProcess.WaitForExit();
        break;
    }
} 

// After this point I build a results report for the user. 
// My app's installer does not yet quit even if it was canceled.

So my question is: How could I 'signal' the SQL Server installer process to cancel and exit?

This line does not seem to do anything:

setupProcess.CloseMainWindow();

This also does not work:

setupProcess.Close(); // This closes my handle. Not the target process.

And I obviously wouldn't want to just kill the process as I could be leaving the user's machine in a not-so-desirable state, in the best case with a lot of garbage files or worse, with a corrupt install.

Any ideas? Sending keys or simulating user clicks? Or hopefully something less hacky?

EDIT:

I think I have found out why CloseMainWindow does not work:

  • The process I start (sqlexpr32.exe) is not really the one that shows the UI for the SQLServer Installer, but a self-extracting exe that in turn launches the sql server real setup.exe as a child process. So this issue gets even harder. =(
+2  A: 

What if you wait until the installer finishes, and after that - if the user has cancelled the main process - you uninstall it immediately?

I know it's a more time consuming process but it's clear and easy.

boj