Posts Tagged ‘ Inno Setup ’

InnoSetup: Check if Revit is running

When installing Revit (or AutoCAD) addins, you have to check if process is running otherways files are not copied correctly.

I am using Inno Setup for my setup projects, since I found it extremely powerful and easy to use. Previously I used Visual Studio Installer, you can read Kean’s¬†tutorial how to implement it. The solution was great since you got the MSI file, which you could include in other installers, but there was to much mantainance and then came the news about¬†retirement.

The code to check in Inno Setup if Revit is running:

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

function InitializeSetup(): Boolean;
var
    Guid : string;
    ErrorCode : integer;
    nMsgBoxResult: Integer;
  foo : string;
  dir: string;
begin
  Result := True;
  while IsModuleLoaded2('Revit.exe') and (nMsgBoxResult <> IDCANCEL) do
  begin
      nMsgBoxResult := MsgBox(CustomMessage('RevitRunning'), mbConfirmation, MB_RETRYCANCEL)
  end;
  // if Cancel is pressed
  if nMsgBoxResult = IDCANCEL then
  begin
    Result := False;
  end;
end;

You need to include psvince.dll file in the [Files] section of the script, for the setup to run properly.

[Files]
Source: {#PATH}\psvince\x86\psvince.dll; Flags: dontcopy

You can download source for psvince.dll. When tested on 64 bit machines, I noticed, that 64 bit processes are not found, so I came up with this:

– add definition for new function at the top of psvince.cpp

BOOL WINAPI EnumProcs2(char* procname);

– copy code bellow in psvince.cpp

int APIENTRY IsModuleLoaded2( char *lpModule )
{
    return EnumProcs2( lpModule );
}

BOOL WINAPI EnumProcs2(char* procname)
{
    //MessageBox(NULL, procname, "msg", MB_OK);
    HANDLE handleToSnapshot;
    PROCESSENTRY32 procEntry;
    procEntry.dwSize = sizeof(PROCESSENTRY32);
    handleToSnapshot = CreateToolhelp32Snapshot(2, 0);
    if (Process32First(handleToSnapshot, &procEntry))
    {
        do
        {
            //MessageBox(NULL, procEntry.szExeFile, "msg",MB_OK);
            if (strcmp(procname, procEntry.szExeFile) == 0)
            {
                //delete handleToSnapshot;
                return TRUE;
            }
        } while (Process32Next(handleToSnapshot, &procEntry));
    }
    return FALSE;
}

– update exports in psvince.def

Advertisements