如何使用installscript检测Excel.exe运行?

问题描述 投票:0回答:3

我一直试图在我的installshield安装程序中检测excel进程。我有一个在appsearch之后运行的自定义操作,如果找到该进程并弹出一个窗口并向用户显示警告,则会弹出一个窗口。

我尝试使用我在installsite.org上找到并使用findWindow()调用的一些旧例子。似乎都没有在进程列表中找到excel.exe。

这是我在尝试查找窗口时使用的一段代码

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;

请注意,无论应用程序是打开还是关闭,只有else块似乎会触发。

我尝试了几种不同的变体,主要是用不同的大写,扩展和版本替换“excel”。似乎什么都没有检测到窗户。我使用Spy ++并且它报告窗口以当前打开的笔记本的名称命名,这使得事情变得复杂,因为我无法知道用户可以打开什么。

我愿意接受这方面的建议。此解决方案的唯一要求是它必须能够在Installshield中作为自定义操作或部分安装条件运行。

excel installshield custom-action installscript installshield-2012
3个回答
1
投票

您可以使用vbscript自定义操作。您可以在UISequence或ExecuteSequence(或两者)的开头运行此CA如果您希望它作为Install条件的一部分。如果要停止安装过程,请在vbscript函数中添加代码,并将自定义操作的“返回处理”选项配置为“Synchonous(检查退出代码)”。

这是我的脚本:

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

0
投票

显然,通过查找关联的窗口来确定进程是否正在运行有其缺陷。

我的建议是检测Excel.exe的进程是否正在运行。它将涉及枚举系统上的进程。相应地修改您的代码。使用C ++更容易实现它,但有许多示例可以向您展示如何实现我刚才所说的。

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

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

采取


0
投票

我们也可以编写一个InstallScript代码来实现这一目标。请参考以下代码:

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;
© www.soinside.com 2019 - 2024. All rights reserved.