zoukankan      html  css  js  c++  java
  • inno setup 安装前判断进程是否存在,以及停止相应进程

    要引入psvince.dll这个dll,放在inno setup的安装目录下(与其他dll相同目录),代码如下: 

    [Files]

    Source: compiler:psvince.dll;Flags: dontcopy noencryption

    [code]
    // function IsModuleLoaded to call at install time
    // added also setuponly flag
    function IsModuleLoaded(modulename: String ):  Boolean;
    external 'IsModuleLoaded@files:psvince.dll stdcall setuponly';

    // function IsModuleLoadedU to call at uninstall time
    // added also uninstallonly flag
    function IsModuleLoadedU(modulename: String ):  Boolean;
    external 'IsModuleLoaded@{app}psvince.dll stdcall uninstallonly' ;

    //;判断进程是否存在
    function IsAppRunning(const FileName : string): Boolean;
    var
        FSWbemLocator: Variant;
        FWMIService   : Variant;
        FWbemObjectSet: Variant;
    begin
        Result := false;
        try
          FSWbemLocator := CreateOleObject('WBEMScripting.SWBEMLocator');
          FWMIService := FSWbemLocator.ConnectServer('', 'rootCIMV2', '', '');
          FWbemObjectSet := FWMIService.ExecQuery(Format('SELECT Name FROM Win32_Process Where Name="%s"',[FileName]));
          Result := (FWbemObjectSet.Count > 0);
          FWbemObjectSet := Unassigned;
          FWMIService := Unassigned;
          FSWbemLocator := Unassigned;
        except
          if (IsModuleLoaded(FileName)) then
            begin
              Result := false;
            end
          else
            begin
              Result := true;
            end
          end;
    end;

    //;通过名称终结进程
    procedure TaskKillProcessByName(AppName: String);
    var
      WbemLocator : Variant;
      WMIService   : Variant;
      WbemObjectSet: Variant;
      WbemObject   : Variant;
    begin;
      WbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
      WMIService := WbemLocator.ConnectServer('localhost', 'rootCIMV2');
      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;

    //;安装的时候判断进程是否存在,存在则先提示是否结束进程
    function InitializeSetup(): Boolean;
    begin
      Result := true;
      if  IsAppRunning('{#MyAppExeName}') then
      begin
        if MsgBox('安装程序检测到 {#MyAppName} 正在运行!'#13''#13'单击“是”按钮关闭程序并继续安装;'#13''#13'单击“否”按钮退出安装!', mbConfirmation, MB_YESNO) = IDYES then
        begin
          TaskKillProcessByName('{#MyAppExeName}');
          TaskKillProcessByName('{#MyAppExeName}');
          Result:= true;
        end
        else
          Result:= false;
      end;
    end;

    //;卸载的时候判断进程是否存在,存在则先提示是否结束进程
    function InitializeUninstall(): Boolean;
    begin
        Result:= true;
        if  IsAppRunning('{#MyAppExeName}') then
        begin
          if MsgBox('卸载程序检测到 {#MyAppName} 正在运行!'#13''#13'单击“是”按钮关闭程序并继续卸载;'#13''#13'单击“否”按钮退出卸载!', mbConfirmation, MB_YESNO) = IDYES then
          begin
            TaskKillProcessByName('{#MyAppExeName}');
            TaskKillProcessByName('{#MyAppExeName}');
            Result:= true;
          end
          else
            Result:= false;
        end;
        // Unload the DLL, otherwise the dll psvince is not deleted
        UnloadDLL(ExpandConstant('{app}psvince.dll'));
        DelTree(ExpandConstant('{app}'), True, True, True);
    end;

  • 相关阅读:
    痞子衡嵌入式:并行NAND接口标准(ONFI)及SLC Raw NAND简介
    痞子衡嵌入式:恩智浦i.MX RT1xxx系列MCU启动那些事(6)- Bootable image格式与加载(elftosb/.bd)
    痞子衡嵌入式:恩智浦i.MX RT1xxx系列MCU启动那些事(5)- 再聊eFUSE及其烧写方法
    痞子衡嵌入式:恩智浦i.MX RT1xxx系列MCU启动那些事(4)- Flashloader初体验(blhost)
    Hystrix完整配置列表
    使用Redis实现UA池
    使用Redis实现延时任务(二)
    使用Redis实现延时任务(一)
    一文彻底理解Redis序列化协议,你也可以编写Redis客户端
    一个低级错误引发Netty编码解码中文异常
  • 原文地址:https://www.cnblogs.com/w-pound/p/13158219.html
Copyright © 2011-2022 走看看