zoukankan      html  css  js  c++  java
  • 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?

    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;
    shareimprove this answer
     
        
    That works really well. The only thing is that I get the Executable path in the following format: "PROGRA~1243~1.106prog.exe". Can I do something to have the full path, like displayed in the task manager, instead of the compact path with the ~1 ? Strangely, for the second folder, the ~1 is in the middle of the folder name, probably because of the dots in the name... – Amaranth Jan 28 '14 at 20:31
        
    That looks like a 8.3 filename. Try to call the ExpandFileName function to expand it to full path. – TLamaJan 28 '14 at 20:39
        
    It did not work, but no worries, I found a way that worked for me (it's a bit more complex though). vincenzo.net/isxkb/index.php?title=GetLongPathName_() Thank you for your great help :) – Amaranth Jan 28 '14 at 20:50 
    1  
    You're welcome! Hm, the ExpandFileName function internally calls GetFullPathName rather than GetLongPathName. It makes me wonder what's the difference between them... – TLama Jan 28 '14 at 21:07
        
    I think this only works in Unicode Inno Setup. I use the non-Unicode version and this will compile for me but always errors out. I think Unicode Inno Setup has more support for Variant casting etc... This place I get stuck at on this script is where WbemObjectSet.Count gets used first. – Arvo Bowen Feb 3 at 0:29
  • 相关阅读:
    虚拟主机导入MySQL出现Unknown character set: ‘utf8mb4’
    解决导入MySQL数据库提示"Unknown character set: 'utf8mb4'"错误
    织梦dedecms如何去除版权中的Power by DedeCms
    发送邮件常见出错代码及简单解决方法
    Hadoop集群配置(最全面总结)
    大数据下的数据分析平台架构
    跟上节奏 大数据时代十大必备IT技能
    Hive深入浅出
    深入解析:分布式系统的事务处理经典问题及模型(转载分享)
    数据分析≠Hadoop+NoSQL
  • 原文地址:https://www.cnblogs.com/micro-chen/p/6485067.html
Copyright © 2011-2022 走看看