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

不羁岁月 提交于 2019-12-12 03:35:02

问题


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.


回答1:


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



回答2:


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




回答3:


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;


来源:https://stackoverflow.com/questions/31059238/how-can-i-use-installscript-to-detect-excel-exe-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!