Friday, June 18, 2010

Check Multiple instance of setup to run at one time

Hello Friends, today I want to share a script which will help you in preventing multiple instances of an installer simultaneously. For this you need to declare below two prototypes in your setup.rul file. 


      prototype Kernel32.CreateMutex(byval NUMBER, BOOL, STRING);
      prototype Kernel32.ReleaseMutex(HWND);


Below I had used a function named 'CheckMultipleInstance' which will help in preventing multiple instances of an installshield installer to run at a time.


//***************************************************************************
//FUNCTION NAME : CheckMultipleInstance
//Purpose : To prevent multiple instances of setup to run at one time.
//***************************************************************************
function CheckMultipleInstance(hMSI)
     STRING strMutex;
     HWND hMutex;
     NUMBER nError;
begin
     strMutex = "MY_SETUPMUTEX" ;
     hMutex = Kernel32.CreateMutex(NULL, TRUE, strMutex);
     nError = Err.LastDllError();
     if (hMutex != 0 && nError == 183) then
            MessageBoxEx("Another instance of this setup is already
            running","",SEVERE);

            ReleaseMutex(hMutex);


            if(MsiSetProperty(ISMSI_HANDLE,"ANOTHERINSTANCE","1") ==
                ERROR_SUCCESS) then

                SprintfMsiLog("CUSTOM LOG: %s","Version is not Valid.");
            endif;


            abort;
      else
            if(MsiSetProperty(ISMSI_HANDLE,"ANOTHERINSTANCE","0") ==
                ERROR_SUCCESS) then

                SprintfMsiLog("CUSTOM LOG: %s","Version is not Valid.");
            endif;
      endif; 
end;

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hi Amit,
    It's a very good solution to restrict multiple instances. But where we can call this function in our execution sequence ? Because we have to call this function before extraction of MSI from EXE. So where do we need to call this function ?

    ReplyDelete