views:

93

answers:

3

Hi all,

I'm creating a setup project using Visual Studio 2008 and I was wondering if there is a standard way to check if a certain program is running during the setup? In this case I want to check for any instances of Microsoft Excel.

thanks in advance!

+1  A: 

You can use the Process class to get the currently running processes

Process[] runningProcesses = Process.GetProcesses();

You can then loop over these and check for a known process name.

Simon P Stevens
+1  A: 

Well I am not sure if it is 'standard' per se but I use EnumProcesses to check for running apps. e.g. something loosely like this (not compiled)

if ( EnumProcesses( processID, sizeof(processID), &needed ) )
{
  processes = needed / sizeof(DWORD);

  for ( int i = 0; i < processes; i++ )
  {
    HANDLE hProcess = OpenProcess( 
      PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID[i] );

   if ( hProcess )
   {
     HMODULE hMod;

     if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), &cbNeeded) )
     {
       GetModuleBaseName( hProcess, hMod, processName, sizeof(processName) );
       //  compare with processName, the sought after process
       if ( !_tcscmp(processName, _tcsupr(processName)) )
       {
         result = processID[i];
         break; // break at first instance
       }
     }
   }
   CloseHandle( hProcess );
 }
Anders K.
A: 

To do this during setup you will likely have to define a custom action. A custom action is an install step which allows you to run arbitrary code. It's too big of a topic to cover in a SO post, but the following article will help you get started on custom actions

In this action you could use the process class to determine if a particular process is running.

public static bool IsExcelRunning() {
  return Process.GetProcesses().Where(x => x.ProcessName == "excel");
}

Can't remember off the top of my head if it's excel or msexcel but you can adjust appropriately ;).

JaredPar