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

来源:互联网 发布:脸上红血丝 知乎 编辑:程序博客网 时间:2024/06/15 18:20

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;


卸载调用和上面相同。

阅读全文
1 0