gpt4 book ai didi

windows - Inno Setup Pascal 脚本搜索正在运行的进程

转载 作者:可可西里 更新时间:2023-11-01 13:53:40 27 4
gpt4 key购买 nike

我目前正在尝试在卸载时进行验证。在 Pascal 脚本函数中,在 Inno Setup 中,我想搜索特定的进程,如果可能的话使用通配符。然后,循环所有的find结果,得到Image Name和Image Path Name,以检查即将卸载的程序是否与正在运行的程序相同。

有办法吗?

最佳答案

这是 WMI 及其 WQL 语言的典型任务。通过 WMI 获取正在运行的进程列表甚至比 Windows API 更可靠。下面的示例显示了如何查询 Win32_Process类与 LIKE运算符(operator):

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
DefaultGroupName=My Program
[Code]
type
TProcessEntry = record
PID: DWORD;
Name: string;
Description: string;
ExecutablePath: string;
end;
TProcessEntryList = array of TProcessEntry;

function GetProcessList(const Filter: string;
out List: TProcessEntryList): Integer;
var
I: Integer;
WQLQuery: string;
WbemLocator: Variant;
WbemServices: Variant;
WbemObject: Variant;
WbemObjectSet: Variant;
begin
Result := 0;

WbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
WbemServices := WbemLocator.ConnectServer('localhost', 'root\CIMV2');

WQLQuery :=
'SELECT ' +
'ProcessId, ' +
'Name, ' +
'Description, ' +
'ExecutablePath ' +
'FROM Win32_Process ' +
'WHERE ' +
'Name LIKE "%'+ Filter +'%"';

WbemObjectSet := WbemServices.ExecQuery(WQLQuery);
if not VarIsNull(WbemObjectSet) and (WbemObjectSet.Count > 0) then
begin
Result := WbemObjectSet.Count;
SetArrayLength(List, WbemObjectSet.Count);
for I := 0 to WbemObjectSet.Count - 1 do
begin
WbemObject := WbemObjectSet.ItemIndex(I);
if not VarIsNull(WbemObject) then
begin
List[I].PID := WbemObject.ProcessId;
List[I].Name := WbemObject.Name;
List[I].Description := WbemObject.Description;
List[I].ExecutablePath := WbemObject.ExecutablePath;
end;
end;
end;
end;

procedure InitializeWizard;
var
S: string;
I: Integer;
Filter: string;
ProcessList: TProcessEntryList;
begin
MsgBox('Now we try to list processes containing "sv" in their names...',
mbInformation, MB_OK);

Filter := 'sv';
if GetProcessList(Filter, ProcessList) > 0 then
for I := 0 to High(ProcessList) do
begin
S := Format(
'PID: %d' + #13#10 +
'Name: %s' + #13#10 +
'Description: %s' + #13#10 +
'ExecutablePath: %s', [
ProcessList[I].PID,
ProcessList[I].Name,
ProcessList[I].Description,
ProcessList[I].ExecutablePath]);
MsgBox(S, mbInformation, MB_OK);
end;
end;

关于windows - Inno Setup Pascal 脚本搜索正在运行的进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21390130/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com