Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
995 views
in Technique[技术] by (71.8m points)

windows - Inno Setup Pascal Script to search for running process

I am currently trying to do a validation at the uninstall moment. In a Pascal script function, in Inno Setup, I want to search for a specific processes, with a wild card if possible. Then, loop through all find results, get the Image Name and Image Path Name, in order to check if the program that is about to be uninstalled is the same as the one running.

Is there a way to do that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is an exemplary task for WMI and its WQL language. Getting list of running processes through WMI is even more reliable than Windows API. The below example shows how to query the Win32_Process class with the 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', 'rootCIMV2');

  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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...