
 ; Macro - Upgrade or Deinstall Active X Object File
 ; ------------------------
 ; precondition: file to register is already on target system
 ;
 ; Example of usage:
 ; UpgradeActiveXObject sourceFile, GUID, majorDotMinorVersion, LangCultureCode, SYS_TYPE
 ;  ; 409 = US English 
 ;  !insertmacro UpgradeActiveXObject "scrrun.dll" 
 ;  	"{420B2830-E718-11CF-893D-00A0C9054228}" "1.0" "409" "win32"
 ; See 
 ;  	[url]http://msdn.microsoft.com/library/default.asp[/url]
 ;	?url=/library/en-us/csvr2002/htm/cs_rp_appcfg_lism_c.asp/
 ; for a complete list of language culture codes.
 ;
 !macro UpgradeActiveXObject SOURCEFILE GUID VERSION LANGCULTURE SYS_TYPE
	Push $R0  
	Push $R1 
	Push $R2 
	Push $R3 
	Push $R4 ; System ActiveX object location
	Push $R5 

	; Read registry for Source File Location

	ReadRegStr $R4 HKCR "TypeLib\${GUID}\${VERSION}\${LANGCULTURE}\${SYS_TYPE}" ""

	; If registry empty, or err check neutral language key "0"
	strcmp $R4 "" 0 "gotFile_${SOURCEFILE}"
	 ReadRegStr $R4 HKCR "TypeLib\${GUID}\${VERSION}\0\${SYS_TYPE}" ""
	 StrCmp $R4 "" "justRegister_${SOURCEFILE}" "gotFile_${SOURCEFILE}"
	
"gotFile_${SOURCEFILE}:"

	IfFileExists "${SOURCEFILE}" "" "justRegister_${SOURCEFILE}"
	 ClearErrors
	 GetDLLVersion "${SOURCEFILE}" $R0 $R1
	 GetDLLVersion $R4 $R2 $R3
	 IfErrors "justRegister_${SOURCEFILE}"
	 IntCmpU $R0 $R2 "" "done_${SOURCEFILE}" "upgrade_${SOURCEFILE}"
	 IntCmpU $R1 $R3 "done_${SOURCEFILE}" "done_${SOURCEFILE}" \
	  		 "upgrade_${SOURCEFILE}" 

"upgrade_${SOURCEFILE}:"

	MessageBox MB_YESNO|MB_ICONEXCLAMATION "The file: $R4 \
		is older than the version used by this application.  Would you \
		like to update your version?  If you choose NO the \
		application may not run properly" \
		IDYES 0 IDNO "done_${SOURCEFILE}"
	
	;Unregister the DLL
	UnRegDLL $R4

"justRegister_${SOURCEFILE}:"
	; Find "exe" substring in Sourcefile
	Push $R4
	Push ".exe"
	Call StrStr
	Pop $R5
	StrCmp $R5 ".exe" 0 doRegSvr32_${SOURCEFILE}
	 execwait '"${SOURCEFILE}" /regserver' 
	 Goto "done_${SOURCEFILE}"

"doRegSvr32_${SOURCEFILE}:"
	RegDll "${SOURCEFILE}"
	
"done_${SOURCEFILE}:"
	Pop $R5
	Pop $R4
	Pop $R3
	Pop $R2
	Pop $R1
	Pop $R0

 !macroend

 ; Macro - Upgrade or Deinstall Active X Object File
 ; ------------------------
 ; precondition: file to register is already on target system
 ;
 ; Example of usage:
 ; RemoveActiveXObject fileName, sourceFile, GUID, majorDotMinorVersion, \ 
 ;                     LangCultureCode, SYS_TYPE
 ;  ; 409 = US English 
 ;  !insertmacro RemoveActiveXObject "scrrun.dll" "$INSTDIR\scrrun.dll" 
 ;  	"{420B2830-E718-11CF-893D-00A0C9054228}" "1.0" "409" "win32"
 ; See 
 ;  	[url]http://msdn.microsoft.com/library/default.asp[/url]
 ;	?url=/library/en-us/csvr2002/htm/cs_rp_appcfg_lism_c.asp/
 ; for a complete list of language culture codes.
 ;
 !macro RemoveActiveXObject FILENAME SOURCEFILE GUID VERSION LANGCULTURE SYS_TYPE
	Push $R4 ; System ActiveX object location
	Push $R5 

	; Read registry for Source File Location

	ReadRegStr $R4 HKCR "TypeLib\${GUID}\${VERSION}\${LANGCULTURE}\${SYS_TYPE}" ""

	; If registry empty, or err check neutral language key "0"
	strcmp $R4 "" 0 "foundFile_${SOURCEFILE}"
	 ReadRegStr $R4 HKCR "TypeLib\${GUID}\${VERSION}\0\${SYS_TYPE}" ""
	 ; If registry empty, or err on neutral language key then the 
	 ; active x object must not be registered.  it is safe to delete.
	 StrCmp $R4 "" "delete_${SOURCEFILE}" "foundFile_${SOURCEFILE}"
	
; found file in registry, make sure it matches up with file in our 
; installation directory.  if it doesn't match it isn't being used, so 
; it is safe to delete. 
"foundFile_${SOURCEFILE}:"
	
	strcmp "${SOURCEFILE}" $R4 "unregisterAXO_${SOURCEFILE}" "delete_${SOURCEFILE}"

"unregisterAXO_${SOURCEFILE}:"

	; Find "exe" substring in Sourcefile
	Push $R4
	Push ".exe"
	Call un.StrStr
	Pop $R5
	
	;Unregister
	StrCmp $R5 ".exe" 0 doUnRegDll_${SOURCEFILE}
	 execwait '"${SOURCEFILE}" /regserver' 
	 GoTo "getUserFeedBack_${SOURCEFILE}"

"doUnRegDll_${SOURCEFILE}:"
	UnRegDLL "${SOURCEFILE}"

"getUserFeedBack_${SOURCEFILE}:"

	MessageBox MB_YESNO|MB_ICONEXCLAMATION "The file: ${SOURCEFILE} \
		is a shared library.  Other applications may be depending \
		on its availability. Would you like us to move it to the \
		System directory?  If you choose NO other applications may \
		not run properly" \
		IDYES 0 IDNO "delete_${SOURCEFILE}"

	; Move file into System Directory.  We don't have to worry about
	; the file being in use since it wasn't the registered file.
	Delete $SYSDIR\${FILENAME}
	ClearErrors
	Rename "${SOURCEFILE}" $SYSDIR\${FILENAME}
	IfErrors "errorCopyingToSystemDir_${SOURCEFILE}"

	; Register File
	StrCmp $R5 ".exe" 0 doRegSvr32_${SOURCEFILE}
	 execwait '"$SYSDIR\${FILENAME}" /regserver' 
	 Goto "done_${SOURCEFILE}"

"errorCopyingToSystemDir_${SOURCEFILE}:"
	Rename "${SOURCEFILE}" $DESKTOP\${FILENAME}
	MessageBox MB_ICONEXCLAMATION "Error moving file to $SYSDIR.  Moving \
		to $DESKTOP.  To manually install shared libraries, move \
		files into directory of your choice and run regsvr32 \
		from the commandline."
	GoTo "done_${SOURCEFILE}"

"doRegSvr32_${SOURCEFILE}:"
	RegDll "$SYSDIR\${FILENAME}"
	GoTo "done_${SOURCEFILE}"

"delete_${SOURCEFILE}:"
	Delete "${SOURCEFILE}"
	
"done_${SOURCEFILE}:"
	Pop $R5
	Pop $R4

 !macroend

