How can I use installscript to detect Excel.exe running?

2.1k views Asked by At

Ive been trying to detect the excel process in my installshield installer. I have a custom action that runs after appsearch and pops a window if it finds the process and displays a warning to the user.

I have tried using some old examples I found on installsite.org and using the findWindow() call. Neither seems to find excel.exe in the process list.

Here is a snippet of code I was using when trying the findwindow

export prototype MyTestFunction(HWND);

function MyTestFunction(hMSI)
HWND nHwnd;
begin   
    nHwnd = FindWindow("EXCEL", "");
    if (nHwnd != 0) then
    MessageBox("found excel", WARNING);
    SendMessage(nHwnd, WM_CLOSE, 0, 0);
    else
    MessageBox("cant find excel", WARNING);
    endif;

end;

Note that only the else block ever seems to fire regardless of the application being open or closed.

I have tried several different variants of this mostly just replacing the "excel" with different capitalization, extensions and versions. Nothing seems to detect the window. I used Spy++ and it reported that the window is named after the name of the currently opened notebook which complicates things since I have no way of knowing what a user could have opened.

I am open to suggestions here. The only requirement for this solution is that it has to be able to run as either a custom action or part of an install condition from within Installshield.

3

There are 3 answers

0
Kiran Hegde On

Obviously trying to figure out if a process is running by finding the associated window has its pitfalls.

My suggestion is to detect if the process for Excel.exe is running. It would involve enumerating the processes on the system. Modify your code accordingly. Its easier to do it using C++ but there are numerous examples available which show you how to achieve what i have just stated.

https://community.flexerasoftware.com/archive/index.php?t-162141.html

https://community.flexerasoftware.com/archive/index.php?t-188807.html

Take

0
Ajit Medhekar On

We can write a InstallScript code as well to achieve this. Please refer the code below :

function CheckRunningProcessAndTerminate(hMSI)
// To Do:  Declare local variables.
OBJECT wmi,objProc;
STRING szProcessName;

begin
// To Do:  Write script that will be executed when MyFunction is called.
szProcessName = "Excel.exe";

set wmi = CoGetObject("winmgmts://./root/cimv2", "");
set objProc = wmi.ExecQuery("Select * from Win32_Process where Name = '" + szProcessName + "'");

if (objProc.count > 0) then
    MessageBox("Process is running.", INFORMATION);
    //kill proces
    TerminateProcesses(szProcessName); 
    //objProc.Terminate(); //I tried this, but it didn't worked.
else
    MessageBox("Process is not running.", INFORMATION);
endif;

 end;
1
Foebius On

You could use a vbscript Custom Action. You can run this CA at the begining of UISequence or ExecuteSequence (or both) If you want it as a part of the Install condition. Add the code in a vbscript function and configure "Return Processing" Option for the Custom Action to "Synchonous (Check exit code)" if you want to stop the installation process.

Here is my script:

Public Function StopProcess

Dim objWMIService, objProcess, colProcess
Dim strComputer, executableFileName
Const IDABORT = 3    

strComputer = "."
executableFileName = "excel.exe"

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Set colProcess = objWMIService.ExecQuery("Select * from Win32_Process Where Name = '" & executableFileName & "'")

For Each  objProcess in colProcess
   objProcess.Terminate()
   ' OR
   StopProcess = IDABORT
   Exit for
Next
End function