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;

Check Free space on disk

function CheckFreeSpace(hMSI)
_DISK_INFO di;
NUMBER n; // to do: something with function return values
NUMBER nvSizeTargetHigh, nvSizeTargetLow, nUnitsTarget;
begin
// init. _DISK_INFO members: what drive, what info
di.szDiskPath = INSTALLDIR;
di.nInfoToQuery = DISK_INFO_QUERY_DISK_FREE_SPACE;

n = GetDiskInfo(&di);
// change to desired unit for free space
nUnitsTarget = MBYTES;

n = ConvertSizeToUnits(di.nFreeSpaceHigh, di.nFreeSpaceLow, BYTES,nvSizeTargetHigh, nvSizeTargetLow, nUnitsTarget);

//SprintfBox(INFORMATION, "Free Space", "Free space: %d MB", nvSizeTargetLow);
if(nvSizeTargetLow<500) then
MsiSetProperty(hMSI,"VALIDFREESPACE","0");
MessageBoxEx("Atleast 500 MB space is required on the destination drive to install Beyond TXT G4 Client. Please free the required space and try again..","",INFORMATION);
else
MsiSetProperty(hMSI,"VALIDFREESPACE","1");
endif;
end;

Monday, May 24, 2010

Calulate Disk Space

Hi, often we need in our installer projects to calculate the disk space on the target machine.

Below code can be edited for this purpose:


function DiskSpace(hMSI)
    NUMBER n;
    STRING szAvailableSpace;
    NUMBER nUnitUsed,nNum,nLocation;
    BOOL bReq;
    STRING sLoc;
begin
     nUnitUsed = GBYTES;
     ISRT._GetDiskSpaceExEx(INSTALLDIR,szAvailableSpace,nUnitUsed,TRUE,TRUE);

     StrToNum(nNum,szAvailableSpace);
     if(nNum<1) then
          nUnitUsed=MBYTES;  
          ISRT._GetDiskSpaceExEx(INSTALLDIR,szAvailableSpace,nUnitUsed,TRUE,TRUE);  
          StrToNum(nNum,szAvailableSpace);
       
         if(nNum<1024) then
              nUnitUsed=KBYTES;
              ISRT._GetDiskSpaceExEx(INSTALLDIR,szAvailableSpace,nUnitUsed,TRUE,TRUE);
              StrToNum(nNum,szAvailableSpace);
     
              if(nNum<1048576) then
                   nUnitUsed=BYTES;
                   ISRT._GetDiskSpaceExEx(INSTALLDIR,szAvailableSpace,nUnitUsed,TRUE,TRUE);
                   szAvailableSpace = szAvailableSpace + " " + StrConvertSizeUnit(BYTES ); //
                   MessageBox("Available space--> "+szAvailableSpace,TRUE);
                   MsiSetProperty(hMSI,"DISKSPACE",szAvailableSpace);
             endif;
             szAvailableSpace = szAvailableSpace + " " + StrConvertSizeUnit( KBYTES );
             MsiSetProperty(hMSI,"DISKSPACE",szAvailableSpace);
         else
             szAvailableSpace = szAvailableSpace + " " + StrConvertSizeUnit( MBYTES );
             MsiSetProperty(hMSI,"DISKSPACE",szAvailableSpace);
         endif;
    endif;

//////////////
nLocation = StrFind (szAvailableSpace, ".");
NumToStr(sLoc,nLocation);
StrSub(szAvailableSpace,szAvailableSpace,0,2);


szAvailableSpace = szAvailableSpace + " " + StrConvertSizeUnit( GBYTES );
//MessageBox("Available space--> "+szAvailableSpace,TRUE);
MsiSetProperty(hMSI,"DISKSPACE",szAvailableSpace);
end;