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

    • Anon
    • October 28th, 2011

    Hi. Thanks for the updated code for psvince.dll. unfortunately it seems that using the IsModuleLoaded2 function on XP SP3 32bit results in this:

    Runtime Error (at -1:0)
    Cannot Import dll:C:\DOCUME~1\MAIN_U~1\LOCALS~1\Temp\is-20PSQ.tmp\psvince.dll.

  1. I didn’t have that problem for now. Have you exported function IsModuleLoaded2 – if not, add it under exports in file psvince.def.

    • Anon
    • October 28th, 2011

    I did that. But I used:

    function IsModuleLoaded(modulename : AnsiString): Boolean;

    Maybe that’s the cause? The weird thing is that it worked fine on >=Vista and I don’t have an XP box to test it again.

  2. Now that you mentioned declaration, I remember I was also having trouble with it, but I don’t remember exactly. Declaration with PAnsiChar works for me also on Win XP machines.

    As for Win XP box: try with virtualization?

    • Anon
    • October 30th, 2011

    Can you share you psvince.dll build? I only have MSVC2010 so I cannot target Win2K.

    Thanks in advance.

    PS. I’m thinking of creating a googlecode project for psvince and I want to add your changes, with all the credits going to you of course.

  3. Sure, just let me know where to put it.

  1. No trackbacks yet.

Leave a reply to jonz Cancel reply