Inno Setup检查运行过程

时间:2010-03-02 12:28:42

标签: inno-setup

我有一个Inno安装项目,我想在卸载之前检查应用程序是否实际运行。我尝试了很多方法但是在Windows 7中运行时它都无声地失败。例如,使用notepad.exe检查psvince.dll进程的以下脚本始终返回false,无论记事本是否正在运行。

我在C#应用中使用psvince.dll进行检查,如果它在Windows 7下工作,它可以正常运行。所以我最好的猜测是安装程序无法在启用UAC的情况下正确运行。

[Code]
function IsModuleLoaded(modulename: String): Boolean;
external 'IsModuleLoaded@files:psvince.dll stdcall';

function InitializeSetup(): Boolean;
begin
   if(Not IsModuleLoaded('ePub.exe')) then
   begin
       MsgBox('Application is not running.', mbInformation, MB_OK);
       Result := true;
   end
   else
   begin
       MsgBox('Application is already running. Close it before uninstalling.', mbInformation, MB_OK);
       Result := false;
   end
end;

4 个答案:

答案 0 :(得分:7)

您使用的是Unicode Inno Setup吗?如果你是,那就应该说

function IsModuleLoaded(modulename: AnsiString): Boolean;

因为psvince.dll不是Unicode dll。

此示例还检查epub.exe,而不是notepad.exe。

答案 1 :(得分:5)

您也可以尝试使用WMIService:

procedure FindApp(const AppName: String);
var
  WMIService: Variant;
  WbemLocator: Variant;
  WbemObjectSet: Variant;
begin
  WbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  WMIService := WbemLocator.ConnectServer('localhost', 'root\CIMV2');
  WbemObjectSet := WMIService.ExecQuery('SELECT * FROM Win32_Process Where Name="' + AppName + '"');
  if not VarIsNull(WbemObjectSet) and (WbemObjectSet.Count > 0) then
  begin
    Log(AppName + ' is up and running');
  end;
end;

答案 2 :(得分:4)

Inno Setup实际上有一个AppMutex指令,该指令在帮助中有记录。实现它需要2行代码。

在您的iss文件的[Setup]部分中添加:

AppMutex=MyProgramsMutexName

然后在您的应用程序启动过程中添加以下代码:

CreateMutex(NULL, FALSE, "MyProgramsMutexName");

答案 3 :(得分:1)

您可以使用此代码检查notepad.exe是否正在运行。

[Code]
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 := not IsAppRunning('notepad.exe');
  if not Result then
  MsgBox('notepad.exe is running. Please close the application before running the installer ', mbError, MB_OK);
end;