InnoSetup 安装和卸载时判断程序是否运行

InnoSetup安装和卸载时判断程序是否运行,有两种方法:psvince和WMI。

1.psvince

[Files]

Source: "D:\SDK\psvince\1.1.1.0\psvince.dll"; Flags: dontcopy noencryption //安装时使用 注意此项不能给卸载使用
Source: "D:\SDK\psvince\1.1.1.0\psvince.dll"; DestDir:"{app}"; Flags:ignoreversion //卸载时使用 注意同样他也不能给安装时候使用否则出错


[Code]

function IsModuleLoaded(modulename: String): Boolean; external 'IsModuleLoaded@files:psvince.dll stdcall setuponly'; //安装时候
function IsModuleLoadedU(modulename: String): Boolean; external 'IsModuleLoaded@{app}\psvince.dll stdcall uninstallonly'; //卸载时候使用


注意不要好似用反了,否则执行的时候会出现错误。


function InitializeSetup() : boolean;
  var
    tempstr : string;
  begin
    tempstr := ExpandConstant('{#MyAppExeName}');
    if IsModuleLoaded(tempstr) then
    begin
      MsgBox('要安装的软件正在运行,请首先关闭!',mbInformation, MB_OK);
      result := false;
    end
    else
    begin
      result := true;
    end;
  end;

  function InitializeUninstall() : Boolean;
  var
    tempstr : string;
    load : Boolean;
  begin
    result := true;
    tempstr := ExpandConstant('{#MyAppExeName}');
    load := IsModuleLoadedU(tempstr);
    if load then
    begin
      MsgBox('软件正在运行,请首先退出后重新卸载!',mbInformation, MB_OK);
      result := false;
    end;
  end;


2. WMI

function IsAppRunning(const FileName: string): Boolean;
var
  FWMIService: Variant;
  FSWbemLocator: Variant;
  FWbemObjectSet: Variant;
begin
  Result := false;
  FSWbemLocator := CreateOleObject('WBEMScripting.SWBEMLocator');
  FWMIService := FSWbemLocator.ConnectServer('', 'root\CIMV2', '', '');
  FWbemObjectSet := FWMIService.ExecQuery(Format('SELECT Name FROM Win32_Process Where Name="%s"',[FileName]));
  Result := (FWbemObjectSet.Count > 0);
  FWbemObjectSet := Unassigned;
  FWMIService := Unassigned;
  FSWbemLocator := Unassigned;
end;


function InitializeSetup(): Boolean;
begin
 Result := IsAppRunning('notepad.exe');
  if Result then
      MsgBox('notepad.exe is running. Please close the application before running the installer ', mbError, MB_OK); 
end;


卸载调用和上面相同。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值