Вопрос How to get the uninstaller fail if the application is running?

  • Автор темы Автор темы nizcoz
  • Дата начала Дата начала
nizcoz

Код:
#define NameProc "Setup"

[Setup]
AppName={#NameProc}
AppVersion={#NameProc}
DefaultDirName={sd}\{#NameProc}
AppMutex={#NameProc}

[Code]
var
  Mutex: THandle;

function CreateMutex(lpMutexAttributes: Longint; bInitialOwner: BOOL; lpName: AnsiString): THandle; external 'CreateMutexA@kernel32.dll stdcall';
function ReleaseMutex(hMutex: THandle): BOOL; external 'ReleaseMutex@kernel32.dll stdcall';
function CloseHandle(hObject: THandle): BOOL; external 'CloseHandle@kernel32.dll stdcall';

procedure SMutex;
begin
  Mutex:= CreateMutex(0, False, ExpandConstant('{#SetupSetting("AppMutex")}'));
end;

function InitializeSetup(): Boolean;
begin
  Result:= not CheckForMutexes(ExpandConstant('{#SetupSetting("AppMutex")}'));
  if not Result then
  MsgBox('One version of the installer is already running. Simultaneous startup is prohibited.', mbError,mb_Ok);
  CloseHandle(Mutex);
end;

procedure InitializeWizard();
begin
  SMutex;
end;
 
In theory, like that, but the problem is that unins000.dat can be read once and an error is displayed (unins000.dat the file is being occupied by another program). Sorry, I do not know how to get around this mistake.


Код:
#define NameProc "MySetup"

[Setup]
AppName={#NameProc}
AppVersion={#NameProc}
DefaultDirName={sd}\{#NameProc}
AppMutex={#NameProc}

[Code]
var
  Mutex: THandle;

function CreateMutex(lpMutexAttributes: Longint; bInitialOwner: Boolean; lpName: AnsiString): THandle; external 'CreateMutexA@kernel32.dll stdcall';
function ReleaseMutex(hMutex: THandle): Boolean; external 'ReleaseMutex@kernel32.dll stdcall';
function CloseHandle(hObject: THandle): Boolean; external 'CloseHandle@kernel32.dll stdcall';

function InitializeUninstall(): Boolean;
begin
Result:= not CheckForMutexes(ExpandConstant('{#SetupSetting("AppMutex")}'));
if not Result then
  MsgBox('One version of the installer is already running. Simultaneous startup is prohibited.', mbError,mb_Ok);
  CloseHandle(Mutex);
end;

procedure InitializeUninstallProgressForm();
begin
  Mutex:=CreateMutex(0, False, ExpandConstant('{#SetupSetting("AppMutex")}'));
end;
 
Sorry, i misunderstood you, so here's the "crutch". "nil" file - empty file, any ..

Код:
#define NameProc "MySetup"

[Setup]
AppName={#NameProc}
AppVersion={#NameProc}
DefaultDirName={sd}\{#NameProc}

[Files]
Source: "nil"; DestDir: "{tmp}";

[Code]
procedure ProcIndif(Status: Boolean);
begin
case Status of
  True: begin
    ExtractTemporaryFile('nil');
    FileCopy(ExpandConstant('{tmp}\nil'), ExpandConstant('{sd}\nil'), True);
end else DeleteFile(ExpandConstant('{sd}\nil'));
end;
end;

function InitializeUninstall: Boolean;
begin
if not FileExists(ExpandConstant('{sd}\nil')) then begin Result:=True;
end else begin
  MsgBox('The installer is started. You can not delete.', mbError,mb_Ok);
  Result:=False;
end;
end;

procedure InitializeWizard;
begin
  ProcIndif(True);
end;

procedure DeinitializeSetup;
begin
  ProcIndif(False);
end;
Ammm... Try it, it can work out.
 

Вложения

  • Exp.7z
    Exp.7z
    597 байт · Просмотры: 7
Последнее редактирование:
nizcoz, Why do you want mutex? You can simply try to create a file stream with Share none property, if it's successfully created, that means the program is not running, if not, then program is running and cancel wizard
 
Назад
Сверху