export prototype BOOL LounchNestedInstallationMSI(string); 
export prototype BOOL LounchNestedRepairMSI(string);
export prototype BOOL LounchNestedUninstaledMSI(string);
prototype BOOL IsDriverInstaled(string);
prototype BOOL IsOldDriverInstaled(string);
   
prototype SetInstallKey(string,BOOL,BOOL);  
prototype string WM_GetGuidForMsi(string);
prototype string WM_GetVersionForMsi(string);

export prototype string WM_GetGuidForMsiByProduct(string) ;
export prototype RemoveFromAddRemoveMSI(string) ;  
//export prototype CheckRequirements(BOOL);  


// Function that lounch a MSI  installation in silent mode
 function BOOL LounchNestedInstallationMSI(szCmdLine) 
 	string msg,LogFileName,MsiName,szProgram,TimerStr;  
 	number ret;
 begin  
       ret = ParsePath(  MsiName,szCmdLine,  FILENAME_ONLY);  
   	   LogFileName = FOLDER_TEMP ^ "Install." + MsiName + ".LOG";    
       WriteToLogFile("LounchNestedInstallationMSI Start:",0,2);
       szProgram ="msiexec";  
       //LAAW_PARAMETERS.nCallbackInterval  = 1;  
       INSTALLER_TIMER_GLOBAL_VAR =0;   
       ret = LaunchAppAndWait ( szProgram,"/i "+ szCmdLine+" ALLUSERS=1 REBOOT=REALLYSUPPRESS  /L*v \"" + LogFileName +"\"  /q ",LAAW_OPTION_WAIT|LAAW_OPTION_HIDDEN); 
       ret =  LAAW_PARAMETERS.nLaunchResult  ;   
       Sprintf(msg,"%s - %d =  LAAW_PARAMETERS.nLaunchResult  ;  - %s",FormatMessage(ret) ,ret, szCmdLine);
       //ret = LaunchAppAndWait ( szProgram,"/i "+ szCmdLine+" ALLUSERS=1  REBOOT=REALLYSUPPRESS",LAAW_OPTION_WAIT|LAAW_OPTION_USE_CALLBACK); 
       //MessageBox(msg,1);
       WriteToLogFile("LaunchAppAndWait ("+szProgram+"/i "+ szCmdLine+" ALLUSERS=1  /L*v \"" + LogFileName +"\"  /q ,LAAW_OPTION_WAIT|LAAW_OPTION_HIDDEN)",0,1);   
       
            
       
      //Return Value
      if(ret=0 || ret = 3010) then 
            NumToStr(TimerStr,INSTALLER_TIMER_GLOBAL_VAR);   
            //WriteToLogFile("LounchNestedInstallationMSI :szCmdLine = " +szCmdLine +" instaled succsessfully after :" +TimerStr+" seconds");
         	return(TRUE);
      else
          	return(FALSE);
      endif;    
      WriteToLogFile("LounchNestedInstallationMSI End:",0,2);
 end; 

 // Function that lounch the MSI  installation  repair in silent mode 
 function BOOL LounchNestedRepairMSI(szCmdLine) 
 string LogFileName,MsiName, szProgram,TimerStr;  
 number ret;
 begin  
       ret = ParsePath(  MsiName,szCmdLine,  FILENAME_ONLY);  
  		LogFileName = FOLDER_TEMP ^ "Install." + MsiName + ".LOG";  
  
       WriteToLogFile("LounchNestedRepairMSI Start:",0,2);
       //LAAW_PARAMETERS.nCallbackInterval  = 1;  
       INSTALLER_TIMER_GLOBAL_VAR =0;
       szProgram ="msiexec";
       ret = LaunchAppAndWait (  szProgram,"/f "+ szCmdLine+" ALLUSERS=1  /L*v \"" + LogFileName +"\"  /q ",LAAW_OPTION_WAIT|LAAW_OPTION_HIDDEN); 
       ret =  LAAW_PARAMETERS.nLaunchResult  ; 
       WriteToLogFile("LaunchAppAndWait ("+szProgram+"/f "+ szCmdLine+" ALLUSERS=1 /L*v \"" + LogFileName +"\"  /q ,LAAW_OPTION_WAIT|LAAW_OPTION_HIDDEN)",0,1);   
      
      //Return Value
      if(ret =0) then  
          NumToStr(TimerStr,INSTALLER_TIMER_GLOBAL_VAR);     
      //   WriteToLogFile("LounchNestedInstallationMSI :szCmdLine =" +szCmdLine +"install Successfully after"+TimerStr +"seconds");  
         return(TRUE);
      else
          return(FALSE);
      endif;  
       WriteToLogFile("LounchNestedRepairMSI End:",0,2);  
 end; 
 
 
 function BOOL LounchNestedUninstaledMSI(szCmdLine) 
 string LogFileName,MsiName, szProgram,TimerStr;  
 number ret;
 begin  
    
       ret = ParsePath(  MsiName,szCmdLine,  FILENAME_ONLY);  
       LogFileName = FOLDER_TEMP ^ "Uninstall." + MsiName + ".LOG";      
 
       WriteToLogFile("LounchNestedUninstaledMSI Start:",0,2);
       //LAAW_PARAMETERS.nCallbackInterval  = 1;  
       INSTALLER_TIMER_GLOBAL_VAR =0;
       szProgram ="msiexec";     
      // ChangeDirectory (szCmdLine);   
       
       ret = LaunchAppAndWait (szProgram,"/x"+ szCmdLine + " REBOOT=REALLYSUPPRESS /L*v \"" + LogFileName +"\" /qn  "  ,LAAW_OPTION_WAIT|LAAW_OPTION_HIDDEN); 
       ret =  LAAW_PARAMETERS.nLaunchResult  ; 
       WriteToLogFile("LaunchAppAndWait ("+szProgram+"/x "+ szCmdLine+" /qn,LAAW_OPTION_WAIT|LAAW_OPTION_HIDDEN)",ret,1);   
      //Return Value
      if(ret =0) then  
          NumToStr(TimerStr,INSTALLER_TIMER_GLOBAL_VAR);     
      
         return(TRUE);
      else
          return(FALSE);
      endif;  
        WriteToLogFile("LounchNestedUninstaledMSI End:",0,2);  
 end; 
	

  
// Function that returns True if Driver already install False otherwise 
 function BOOL IsDriverInstaled(InstallName)  
    string Guid;
 begin    
    Guid =  WM_GetGuidForMsi(InstallName); 
    
    if(Guid="") then 
      return(FALSE);
    else
      return(TRUE);  
    endif;  
      
 end;
         
         
 function SetInstallKey(ProductName,InstallationSuccess,finalstatus)     
 	NUMBER nType; 
 	STRING RegKey,version,InstallIntelRegkey;
 begin   
     WriteToLogFile("SetInstallKey ("+ProductName+") Start:",0,2);
     InstallIntelRegkey = INSTALL_INTEL_REGKEY;
     RegKey =  InstallIntelRegkey^ProductName;
     RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE ); 
  	 nType = REGDB_STRING;    
  	 RegDBDeleteValue ( RegKey, "Sync" );
  	 if(InstallationSuccess)  then 	 
	  	   if (finalstatus ) then
	  	   		RegDBSetKeyValueEx ( RegKey, "install", nType, "success", -1 ); 	
	            WriteToLogFile("RegDBSetKeyValueEx("+RegKey+",install,nType ,success,-1 )",0,1);    
	  	   endif; 	
  	 else
  	 	RegDBSetKeyValueEx ( RegKey, "install", nType, "failure", -1 );  
	    WriteToLogFile("RegDBSetKeyValueEx("+RegKey+",install,nType ,failure,-1 )",0,1);  
  	 
  	 endif;      	 
     WriteToLogFile("SetInstallKey ("+ProductName+") End:",0,2);
 end;  
 
   
function string WM_GetGuidForMsi(sMsi)  
object l_Installer, l_Products;
string suMsi,lu_sTemp,l_sTemp, l_sGUID;
int l_nCount;  
  number nReturn,ret,nResault ;
 	 LIST listID;  
     number Index;
     BOOL doskip;   
     string msg;
begin 
      Sprintf(msg,"msi name: %s ",sMsi);  
      WriteToLogFile(msg,0,2);
       doskip = FALSE;
      try    
	    set l_Installer = CoCreateObject("WindowsInstaller.Installer");
	    if(!IsObject(l_Installer)) then
	    	Err.Raise(Err.Number, "", "Cannot Get Object, l_Installer");
	    endif;
	      
	    set l_Products = l_Installer.Products;
	    if(!IsObject(l_Products)) then
	    	Err.Raise(Err.Number, "", "Cannot Get Object, l_sProducts");
	    endif;
	    for l_nCount = 0 to l_Products.Count - 1  
	    Sprintf(msg,"for %d = 0 to %d - 1",l_nCount,l_Products.Count);
	    WriteToLogFile(msg,0,2);
        	l_sGUID = l_Products.Item(l_nCount); 
        	Sprintf(msg,"%s = l_Products.Item(%d);",l_sGUID,l_nCount);
	        WriteToLogFile(msg,0,2);
     
        	try
        		l_sTemp = l_Installer.ProductInfo(l_sGUID, "PackageName");
        		Sprintf(msg,"try - %s = l_Installer.ProductInfo(%s, \"PackageName\");",l_sTemp,l_sGUID);
	            WriteToLogFile(msg,0,2);
        	 catch 
        	    doskip = TRUE;
        	endcatch; 
        	if !(doskip) then
        		
	           nReturn = StrToUpper (suMsi, sMsi);
	           Sprintf(msg,"%d = StrToUpper (%s, %s);",nReturn,suMsi, sMsi);
	           WriteToLogFile(msg,0,2);
	           nReturn = StrToUpper (lu_sTemp, l_sTemp); 
	           Sprintf(msg,"%d = StrToUpper (%s, %s);",nReturn,lu_sTemp, l_sTemp);
	           WriteToLogFile(msg,0,2);
	           // remove the .MSI so it will be valid for 64 too
	           StrSub(suMsi,suMsi,0,StrLength(suMsi)-4);
	           if (lu_sTemp % suMsi) then      // if PackageName is the same as MSI
	             Sprintf(msg,"if (%s % %s) then",lu_sTemp, suMsi);
	             WriteToLogFile(msg,0,2);
	             Sprintf(msg,"return   %s;",l_sGUID);
	             WriteToLogFile(msg,0,2);
	             return   l_sGUID;
	           endif;  
           endif;  
                doskip = FALSE;
    	endfor;   
    	
         catch
         
 
		   return("");
		   Sprintf(msg,"could not find any installer return   \"\";","");
	       WriteToLogFile(msg,0,2);
	   
  endcatch;	 
  
end;


function BOOL IsOldDriverInstaled(InstallName)  
    string Guid;
 begin    
    Guid =  WM_GetGuidForMsi(InstallName); 
    
    if(Guid="") then 
      return(FALSE);
    else
      return(TRUE);  
    endif;  
      
 end;
 

function  RemoveFromAddRemoveMSI(ProductName)  
 string InstallRegKey,Guid,UNINSTALLREGPLACE;  
 number ret,nType;
 begin      
 
 Guid =  WM_GetGuidForMsiByProduct(ProductName); 
 UNINSTALLREGPLACE = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\";
 //"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall";//SOFTWARE\\Classes\\Installer\\Products\\21E33AB224D6C4B45B2EAEB1FFF2C647

 InstallRegKey = UNINSTALLREGPLACE^Guid;
         
      
 RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE ); 
 nType = REGDB_NUMBER;
 ret = RegDBSetKeyValueEx ( InstallRegKey, "SystemComponent", nType, "1", -1 ); 
 ret = RegDBDeleteKey(InstallRegKey);
 
 end;     
 
 
 
 
function string WM_GetGuidForMsiByProduct(sMsi)  

object l_Installer, l_Products;
string suMsi,lu_sTemp,l_sTemp, l_sGUID;
int l_nCount;  
  number nReturn,ret,nResault ;
 	 LIST listID;  
     number Index;
     BOOL doskip;
begin 
       doskip = FALSE;
      try    
	    set l_Installer = CoCreateObject("WindowsInstaller.Installer");
	    if(!IsObject(l_Installer)) then
	    	Err.Raise(Err.Number, "", "Cannot Get Object, l_Installer");
	    endif;
	      
	    set l_Products = l_Installer.Products;
	    if(!IsObject(l_Products)) then
	    	Err.Raise(Err.Number, "", "Cannot Get Object, l_sProducts");
	    endif;
	    for l_nCount = 0 to l_Products.Count - 1  
	    
        	l_sGUID = l_Products.Item(l_nCount); 
        	try
        		l_sTemp = l_Installer.ProductInfo(l_sGUID, "ProductName");
        	 catch 
        	    doskip = TRUE;
        	endcatch; 
        	if !(doskip) then
        		
	           nReturn = StrToUpper (suMsi, sMsi);
	   
	           nReturn = StrToUpper (lu_sTemp, l_sTemp);  
	           if (lu_sTemp =  suMsi) then      // if PackageName is the same as MSI
	             return   l_sGUID;
	           endif;  
           endif;  
                doskip = FALSE;
    	endfor;   
    	
         catch
         
 
		   return("");
		  
	   
  endcatch;	
	
end;

function string WM_GetVersionForMsi(sMsi)  
object l_Installer, l_Products;
string suMsi,lu_sTemp,l_sTemp, l_sGUID,l_sVer;
int l_nCount;  
  number nReturn,ret,nResault ;
 	 LIST listID;  
     number Index;
     BOOL doskip;
begin 
       doskip = FALSE;
      try    
	    set l_Installer = CoCreateObject("WindowsInstaller.Installer");
	    if(!IsObject(l_Installer)) then
	    	Err.Raise(Err.Number, "", "Cannot Get Object, l_Installer");
	    endif;
	   
	    set l_Products = l_Installer.Products;
	    if(!IsObject(l_Products)) then
	    	Err.Raise(Err.Number, "", "Cannot Get Object, l_sProducts");
	    endif;
	    for l_nCount = 0 to l_Products.Count - 1  
	    
        	l_sGUID = l_Products.Item(l_nCount); 
        	try
        		l_sTemp = l_Installer.ProductInfo(l_sGUID, "PackageName");
        	 catch 
        	    doskip = TRUE;
        	endcatch; 
        	if !(doskip) then
        		
	           nReturn = StrToUpper (suMsi, sMsi);
	   
	           nReturn = StrToUpper (lu_sTemp, l_sTemp);  
	           if (lu_sTemp % suMsi) then      // if PackageName is the same as MSI
	             
	             l_sVer = l_Installer.ProductInfo(l_sGUID, "VersionString");
	             return   l_sVer;
	           endif;  
           endif;  
                doskip = FALSE;
    	endfor;   
    	
         catch
         
 
		  return("");
		  
	   
  endcatch;	 
  
end;   


