
////////////////////////////////////////////////////////////////////////////////
//                                                                            
//   File Name:   Setup.rul                                                   
//                                                                            
//   Description: InstallShield script                                        
//                                                                            
//   Comments:    This script was generated based on the selections you made in
//                the Project Wizard.  Refer to the help topic entitled "Modify      
//                the script that the Project Wizard generates" for information
//                on possible next steps.
//
////////////////////////////////////////////////////////////////////////////////


// Include header files
    
#include "ifx.h"
#include "utils.h" 
#include "winapi.h"
    
////////////////////// string defines ////////////////////////////

//////////////////// installation declarations ///////////////////

// ----- DLL function prototypes -----
  // PROCESSENTRY32
typedef PROCESSENTRY32
begin
	NUMBER dwSize; 
	NUMBER cntUsage; 
	NUMBER th32ProcessID; 
	NUMBER th32DefaultHeapID; 
	NUMBER th32ModuleID; 
	NUMBER cntThreads; 
	NUMBER th32ParentProcessID; 
	NUMBER  pcPriClassBase; 
	NUMBER dwFlags; 
	STRING szExeFile[MAX_PATH]; 
	NUMBER th32MemoryBase;
	NUMBER th32AccessKey;
end;            

#define REG_DWORD			4

#define KEY_ALL_ACCESS		0x000f003f
#define KEY_WOW64_64KEY		0x0100
#define KEY_WOW64_32KEY		0x0200

#define ERROR_SUCCESS		0
#define ERROR_NO_MORE_ITEMS	259

    // your DLL function prototypes
    prototype BOOL Setupapi.SetupCopyOEMInfA( BYREF STRING, POINTER, LONG, LONG, BYREF STRING, LONG, POINTER, POINTER );
    prototype LONG Kernel.OpenEvent( LONG, BOOL, BYREF STRING );
    prototype BOOL Kernel.SetEvent( LONG );
    prototype BOOL Kernel.MoveFileExA( BYREF STRING, POINTER, LONG ); //2004-04-08
    prototype NUMBER KERNEL32.CreateToolhelp32Snapshot(NUMBER, NUMBER);        
    prototype BOOL KERNEL32.Process32First(NUMBER, PROCESSENTRY32 POINTER);   
    prototype BOOL KERNEL32.Process32Next(NUMBER, PROCESSENTRY32 POINTER);   
    prototype NUMBER KERNEL32.OpenProcess(NUMBER, BOOL, NUMBER);   
    prototype BOOL KERNEL32.TerminateProcess(NUMBER, INT);
    prototype BOOL KERNEL32.IsWow64Process(NUMBER, POINTER);
    prototype BOOL KERNEL32.Wow64DisableWow64FsRedirection(POINTER);
    prototype BOOL KERNEL32.Wow64RevertWow64FsRedirection(NUMBER);
    prototype LONG Advapi32.RegCreateKeyExA(NUMBER, BYVAL STRING, NUMBER, POINTER, NUMBER, NUMBER, POINTER, POINTER, POINTER);
    prototype LONG Advapi32.RegSetValueExA(NUMBER, BYVAL STRING, NUMBER, NUMBER, POINTER, NUMBER);
    prototype LONG Advapi32.RegCloseKey(NUMBER);
    prototype LONG Advapi32.RegEnumKeyA(NUMBER, NUMBER, BYREF STRING, NUMBER);
    prototype LONG Advapi32.RegDeleteKeyA(NUMBER, BYVAL STRING);
    prototype LONG Advapi32.RegDeleteKeyExA(NUMBER, BYVAL STRING, NUMBER, NUMBER);
    prototype LONG Advapi32.RegFlushKey(NUMBER);
    prototype LONG Advapi32.RegDeleteValueA(NUMBER, BYVAL STRING);
// ---- script function prototypes -----


    // your script function prototypes

    // your global variables
    typedef CUSTOMINFO   
   begin
    	BOOL	bInitialized;
    	STRING	szTwainDir[MAX_PATH];
    	STRING	szDriverPackageDir[MAX_PATH];
    	STRING	szUninstParam[MAX_PATH];
    	STRING	szSoftRootKey[MAX_PATH];     
    	STRING	szSYRootKey[MAX_PATH];
    	STRING	szProductType[MAX_PATH];
    	STRING	szDriverService[MAX_PATH];
    	STRING	szDefInfPath[MAX_PATH];
    	STRING	szUninstToolPath[MAX_PATH];
    end;
    
    CUSTOMINFO	gCustomInfo;


//////////////////////////////////////////////////////////////////////////////////////////
//                                                                           
//  FUNCTION:   OnFirstUIBefore                                            
//                                                                           
//  EVENT:      The OnFirstUIBefore event is called by OnShowUI when the setup is
//              running in first install mode. By default this event displays UI allowing
//              the end user to specify installation parameters.
//              
//              Note: This event will not be called automatically in a
//              program...endprogram style setup.                                                             
//
//////////////////////////////////////////////////////////////////////////////////////////
function OnFirstUIBefore()
    number  nResult;
    number  nLevel;
    number  nvSize, nSetupType;
    number  nLoop;
    string  szTitle, szMsg;
    string  szTargetPath;
    string  svDir;
    string  szComponents, szTargetdir;
    string  szTargetDirAppendix;  
begin	
    nSetupType = COMPLETE;

	InitializeCustomTarget();
	
    if ( ALLUSERS ) then   
        TARGETDIR = PROGRAMFILES ^ IFX_COMPANY_NAME ^ IFX_PRODUCT_NAME;
    else
        TARGETDIR = FOLDER_APPDATA ^ IFX_COMPANY_NAME ^ IFX_PRODUCT_NAME; 
    endif;

    // Customize the default TARGETDIR for multi-instance application.
    // TODO: If you want something different customize the code below.	
    if( MAINT_OPTION = MAINT_OPTION_MULTI_INSTANCE  && MULTI_INSTANCE_COUNT > 0) then
        nLoop = 1;
        svDir = TARGETDIR;
        while(ExistsDir(TARGETDIR) = EXISTS)			
            NumToStr(szTargetDirAppendix,nLoop);
            TARGETDIR = svDir + szTargetDirAppendix;
            nLoop = nLoop + 1;
        endwhile;						
    endif;

    svDir = TARGETDIR;

// Beginning of UI Sequence
Dlg_Start:
	Disable ( LOGGING );
	RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE );
	Enable ( LOGGING );
/*
Dlg_SdWelcome:
    szTitle = "";
    szMsg = "";
    nResult = SdWelcome( szTitle, szMsg );
    if (nResult = BACK) goto Dlg_Start;
*/
Dlg_SdStartCopy2:
    szTitle = "";
    szMsg = "";     
    Disable ( BACKBUTTON );
    nResult = SdStartCopy2( szTitle, szMsg );	
 //   if (nResult = BACK) goto Dlg_Start;

    return 0;   
 end;
 

//---------------------------------------------------------------------------
// OnFirstUIAfter
//
// First Install UI Sequence - After Move Data
//
// The OnFirstUIAfter event called by OnShowUI after the file transfer
// of the setup when the setup is running in first install mode. By default
// this event displays UI that informs the end user that the setup has been
// completed successfully.
//
// Note: This event will not be called automatically in a
// program...endprogram style setup.
//---------------------------------------------------------------------------
function OnFirstUIAfter()
    STRING szTitle, szMsg1, szMsg2, szOpt1, szOpt2;
    NUMBER bvOpt1, bvOpt2;
    STRING szMsg , svValue, szNumValue, szMsg3;
    number	nResult, nvSize, nvType;
    NUMBER OldValue;
begin

    ShowObjWizardPages(NEXT);
   
//++++++++++++++++++++++++++++++++++++++
Post_installation:
	PostInstallation();

Dlg_Start:

  
  
  if (SYSINFO.WINNT.bWinXP) then 
  RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE );           
  svValue = "0"; 
  nvType      = REGDB_NUMBER;
  szNumValue = "1";
  szMsg3 = "Setup is updating system files";
  nResult = RegDBGetKeyValueEx ( "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Hotfix\\kb822603", "Installed" , nvType , svValue , nvSize );
  if (svValue != szNumValue) then
      SdShowMsg ( szMsg3 , TRUE );
    	  LaunchAppAndWait ( WINDIR ^ "windowsxp-kb822603-x86.exe","-u" , LAAW_OPTION_WAIT );
      SdShowMsg ( szMsg3 , FALSE );
  endif;    
  
endif; 

Dlg_SdFinish:
//--------------------------------------
    szTitle = "";
    szMsg1 = ""; 
    szMsg2 = "";
    szOpt1 = "";
    szOpt2 = "";
	bvOpt1   = FALSE;
    bvOpt2   = FALSE;
    
    if ( BATCH_INSTALL ) then
    	SdFinishReboot ( szTitle , szMsg1 , SYS_BOOTMACHINE , szMsg2 , 0 );
    else
		
		// If the update service is enabled, show finish dialog that includes
		// update check option.
		if( ENABLED_ISERVICES & SERVICE_ISUPDATE ) then

			if( SdFinishUpdateEx( szTitle, szMsg1, szMsg2, szOpt1, szOpt2, TRUE ) ) then

				// Don't check for updates in silent mode.
				if( MODE != SILENTMODE ) then
					UpdateServiceCheckForUpdates( "", FALSE );
				endif;

			endif;

		else
			SdFinish ( szTitle , szMsg1 , szMsg2 , szOpt1 , szOpt2 , bvOpt1 , bvOpt2 );
		endif;

    endif;
	if ( ((SYSINFO.nWinMajor == 5) && (SYSINFO.nWinMinor == 2)) || ((SYSINFO.nWinMajor == 6) && (SYSINFO.nWinMinor == 0))) then
		Wow64DisableWow64FsRedirection(&OldValue);
    	LaunchAppAndWait ( WINSYSDIR ^ "rundll32.exe", WINSYSDIR^"csnp2std.dll,Rescan" , LAAW_OPTION_NOWAIT );
		Wow64RevertWow64FsRedirection(OldValue);
	else
	 	LaunchAppAndWait ( WINSYSDIR ^ "rundll32.exe", WINSYSDIR^"csnp2std.dll,Rescan" , LAAW_OPTION_NOWAIT );
    
    endif;
    
      	
end;
 
//---------------------------------------------------------------------------
// OnMaintUIBefore
//
// Maintenance UI Sequence - Before Move Data
//
// The OnMaintUIBefore event is called by OnShowUI when the setup is
// running in maintenance mode. By default this event displays UI that
// allows the end user to add or remove components, repair currently
// installed components or uninstall the application.
//
// Note: This event will not be called automatically in a
// program...endprogram style setup.
//---------------------------------------------------------------------------
function OnMaintUIBefore()
    number	nResult, nType, nMediaFlags;
    string	szTitle, szMsg, szIgnore;
begin
	
//++++++++++++++++++++++++++++++++++++++
	// nType defaults to MODIFY.
	//nType = MODIFY;
	// nType defaults to REMOVEALL.
	nType = REMOVEALL;
//--------------------------------------

// Beginning of UI Sequence
Dlg_Start:

// Show Uninstall Confirmation Dialog
    if ( nType = REMOVEALL ) then
		nResult = MessageBox( SdLoadString( IFX_MAINTUI_MSG ), MB_YESNO );
		if (nResult != IDYES ) then
			abort;
		endif;
	endif;

	switch(nType)

        case REMOVEALL:
						
			// Ensure that all previously installed components are removed
			// for media that supports updating.
			MediaGetData( MEDIA, MEDIA_FIELD_MEDIA_FLAGS, nMediaFlags, szIgnore );
			
			if( nMediaFlags & MEDIA_FLAG_UPDATEMODE_SUPPORTED ) then
				ComponentRemoveAllInMediaAndLog();
			else
				ComponentRemoveAllInMedia();
			endif;

        case REPAIR:
			                          
			// Reinstall all previously installed components.
			ComponentReinstall();

    endswitch;
 
end;
 
//---------------------------------------------------------------------------
// OnShowUI
//
// This function drives the UI sequence and file transfer of the setup.
// 
// The OnShowUI event is called directly by the framework to initiate
// the UI sequence and file transfer of the setup. By default this event
// displays UI that informs the end user that the maintenance setup has been
// completed successfully.
// 
// Note: This event will not be called automatically in a
// program...endprogram style setup.
//---------------------------------------------------------------------------
function OnShowUI()
BOOL	bMaintenanceMode, bUpdateMode;
string	szIgnore, szTitle;
HWND	hMain;  
HWND	hViewer;
NUMBER	hProcessSnap;   
PROCESSENTRY32 pe32;  
NUMBER	hProcess;
NUMBER nResult;
NUMBER hkResult, dwDisposition;
begin
		
		// Enable dialog caching
		Enable( DIALOGCACHE );
		
		// Determine what events to show.
		bUpdateMode	= FALSE;
		bMaintenanceMode = FALSE;
	
		// Remove this to disabled update mode.
		if( UPDATEMODE ) then
			bUpdateMode = TRUE;
		endif;

		// Remove this to disable maintenance mode.
		if ( MAINTENANCE ) then
			bMaintenanceMode = TRUE;
		endif;

		// Show appropriate UI

		// TODO: Enable if you want to enable background etc.
		if ( LoadStringFromStringTable( "TITLE_MAIN", szTitle ) < ISERR_SUCCESS ) then // Load the title string.
			szTitle = IFX_SETUP_TITLE;
		endif;
		//SetTitle( szTitle, 24, WHITE );
		//Enable( FULLWINDOWMODE );						   
		Disable( BACKGROUND );
		//SetColor( BACKGROUND, RGB( 0, 128, 128 ) );

//++++++++++++++++++++++++++++++++++++++
		Sprintf ( szTitle , szTitle , IFX_PRODUCT_NAME, IFX_PRODUCT_VERSION );
		SetTitle( szTitle, 0, BACKGROUNDCAPTION );   
		IFX_SETUP_TITLE = szTitle;
		InitializeCustomInfo();

		// set window to top most		
		hMain = GetWindowHandle(HWND_INSTALL);
		SetWindowPos(hMain, -1, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
//--------------------------------------

		if( bUpdateMode ) then
			OnUpdateUIBefore();
		else
			if ( bMaintenanceMode ) then
				OnMaintUIBefore();
			else
				OnFirstUIBefore();
			endif;
		endif;

//++++++++++++++++++++++++++++++++++++++
		hViewer = FindWindow ( "" , "CameraMonitor(SN9C201/SN9C202)" );   
		SendMessage ( hViewer , WM_CLOSE , 0 , 0 ); 
	    RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE );    
	    RegDBDeleteValue ( "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run" , "SNP2STD" );

		if ( ((SYSINFO.nWinMajor == 5) && (SYSINFO.nWinMinor == 2)) || ((SYSINFO.nWinMajor == 6) && (SYSINFO.nWinMinor == 0))) then
	    	nResult = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\SONIX\\PCCAM\\SNP2STD", 0, 0, 0, (KEY_ALL_ACCESS | KEY_WOW64_64KEY), 0, &hkResult, &dwDisposition);
	        //RegDBDeleteValue ( "SOFTWARE\\SONIX\\PCCAM\\SNP2STD" , "DisplaySelection" );
	        RegDeleteValueA(hkResult, "DisplaySelection");
	        RegCloseKey(hkResult);
	        Sleep(500);
	    else
	    	nResult = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\SONIX\\PCCAM\\SNP2STD", 0, 0, 0, KEY_ALL_ACCESS, 0, &hkResult, &dwDisposition);
	         //RegDBDeleteValue ( "SOFTWARE\\SONIX\\PCCAM\\SNP2STD" , "DisplaySelection" );
	        RegDeleteValueA(hkResult, "DisplaySelection");
	        RegCloseKey(hkResult); 
	        Sleep(500);
	    endif;
	    
	    	
		// Take a snapshot of all processes in the system.
		hProcessSnap = CreateToolhelp32Snapshot( 0x00000002, 0 ); //TH32CS_SNAPPROCESS:0x00000002
		if( hProcessSnap != INVALID_HANDLE_VALUE ) then       
			// Set the size of the structure before using it.
	  		pe32.dwSize = SizeOf( pe32 );
	
			// Retrieve information about the first process,
			// and exit if unsuccessful
			if( Process32First( hProcessSnap, &pe32 ) ) then
				// Now walk the snapshot of processes, and
				// display information about each process in turn
				repeat
//					if (pe32.szExeFile % "VM303_STI.EXE") then   
					if ((pe32.szExeFile % "tsnp2std.EXE") || (pe32.szExeFile % "VM303_STI.EXE")) then   
						hProcess = OpenProcess( 0x1f0fff, FALSE, pe32.th32ProcessID ); //PROCESS_ALL_ACCESS:0X1F0FFF   
						if (hProcess != NULL) then
							TerminateProcess( hProcess, 0 );
							CloseHandle( hProcess );  
						endif;
					endif;
				until ( !Process32Next( hProcessSnap, &pe32 ) );
			endif;   
			
			// Don't forget to clean up the snapshot object!
			CloseHandle( hProcessSnap );
		endif;
//--------------------------------------

		// Move Data
		OnMoveData();
		
		// Show appropriate UI after move data
		if( bUpdateMode ) then
			OnUpdateUIAfter();
		else
			if ( bMaintenanceMode ) then
				OnMaintUIAfter();
			else
				OnFirstUIAfter();
			endif;
		endif;

		// Disable dialog caching
		Disable(DIALOGCACHE);

end;
 
// --- include script file section ---
#include "ComponentEvents.rul"
#include "utils.rul"