Setup终止正在运行的进程

Setup终止正在运行的进程

本文介绍了Inno Setup终止正在运行的进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了一种方法来查找某个进程("iexplore.exe")是否正在运行,现在我需要找到一种方法来从Inno Setup中关闭它(终止该进程).

I have already implemented a way to find whether a process ("iexplore.exe") is running, I now need to find a way to close it (terminate the process) from Inno Setup.

strProg := 'iexplore.exe';
winHwnd := FindWindowByWindowName(strProg);
MsgBox('winHwnd: ' + inttostr(winHwnd),  mbInformation, MB_OK );
if winHwnd <> 0 then
  retVal:=postmessage(winHwnd,WM_CLOSE,0,0);

以上示例中的消息框将始终返回0,因此永远不会获得句柄.(示例中的WM_CLOSE常量已正确初始化)我需要另一种方法来执行此操作,希望该方法不涉及编写执行此操作的C ++ DLL(我不精通C ++,也许可以用C#编写DLL,但是我不知道Inno Setup是否将与之交互.)

The message box in the example above will always return 0 therefore no handle is ever gotten.(the WM_CLOSE constant in the example is properly initialized)I need another way of doing this, and hopefully one that does not involve writing a C++ DLL that does this (I am not proficient in C++, I might be able to write a DLL in C#, however I don't know whether Inno Setup will interop with that).

此C#DLL将获得进程列表,遍历进程名称,找到匹配项(=="iexplorer"),然后杀死具有该名称的进程...但是我仍然希望找到一个更简单的解决方案这样我就不必与Pascal脚本进行互操作了.

This C# DLL would get the process list, iterate thru the names of processes, find a match (=="iexplorer") and then kill the processes with that name...however I am still hoping to find an easier solution so that I wouldnt have to interop it with Pascal script.

提前谢谢!

推荐答案

使用Win32_Process的版本,则使用"notepad.exe"调用该过程:

Version with use of Win32_Process, you call the procedure with i.e. 'notepad.exe':

const wbemFlagForwardOnly = $00000020;

procedure CloseApp(AppName: String);
var
  WbemLocator : Variant;
  WMIService   : Variant;
  WbemObjectSet: Variant;
  WbemObject   : Variant;
begin;
  WbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  WMIService := WbemLocator.ConnectServer('localhost', 'root\CIMV2');
  WbemObjectSet := WMIService.ExecQuery('SELECT * FROM Win32_Process Where Name="' + AppName + '"');
  if not VarIsNull(WbemObjectSet) and (WbemObjectSet.Count > 0) then
  begin
    WbemObject := WbemObjectSet.ItemIndex(0);
    if not VarIsNull(WbemObject) then
    begin
      WbemObject.Terminate();
      WbemObject := Unassigned;
    end;
  end;
end;

这篇关于Inno Setup终止正在运行的进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 13:06