!macro UpgradeDLL DLL_NAME DLL_LOCATION_PATH
        Call UpgradeDLL_A

        ClearErrors
        GetDLLVersionLocal ${DLL_NAME} $0 $6
        GetDLLVersion "${DLL_LOCATION_PATH}\${DLL_NAME}" $7 $8
        IfErrors upgrade_${DLL_NAME}
        IntCmpU $0 $7 noupgrade_${DLL_NAME} noupgrade_${DLL_NAME} upgrade_${DLL_NAME}
        IntCmpU $6 $8 noupgrade_${DLL_NAME} noupgrade_${DLL_NAME} upgrade_${DLL_NAME}
     upgrade_${DLL_NAME}:
        File "${DLL_NAME}"
     noupgrade_${DLL_NAME}:
	StrCpy $0 "${DLL_LOCATION_PATH}\${DLL_NAME}"
	StrCpy $6 "${DLL_NAME}"
	StrCpy $8 "${DLL_LOCATION_PATH}"
	Call UpgradeDLL_B
!macroend

; Utility function to extract filename, modified from GetParent
; Input: $0
; Output: $0
Function GetChild
  Exch $0 ; old $0 is on top of stack
  Push $1
  Push $2
  StrCpy $1 -1
  loop:
    StrCpy $2 $0 1 $1
    StrCmp $2 "" exit
    StrCmp $2 "\" exit
    IntOp $1 $1 - 1
  Goto loop
  exit:
    IntOp $1 $1 + 1
    StrCpy $0 $0 12 $1
    Pop $2
    Pop $1
    Exch $0 ; put $0 on top of stack, restore $0 to original value
FunctionEnd

Function UpgradeDLL_A
	Push $0
	Push $5
	Push $6
	Push $7
	Push $8
	GetTempFileName $5
        SetDetailsPrint textonly
        Delete $5             ;only need the name, not the actual file in the Tempdir
        SetDetailsPrint both
        Push $5
        Call GetChild
        Pop $5
FunctionEnd

Function UpgradeDLL_B
	ClearErrors
	IfFileExists "$TEMP\$6" rename_temp_file noupgrade
	
	rename_temp_file:
		Rename "$TEMP\$6" "$8\$5"
		IfFileExists "$8\$5" check_target_exists rename_temp_file

	check_target_exists:
		IfFileExists "$0" do_unreg dont_unreg

	do_unreg:
		UnRegDLL $0

	dont_unreg:
		Rename /REBOOTOK "$8\$5" "$8\$6"
		DetailPrint "Installed $0"
		IfRebootFlag regreboot regnoreboot
		
	regreboot:
		WriteRegStr HKLM Software\Microsoft\Windows\CurrentVersion\RunOnce $0 "$SYSDIR\regsvr32 /s $0"
		Goto done

	regnoreboot:
		RegDLL "$0"
		Goto done

	noupgrade:
		Goto done
		
	done:
                Delete "$TEMP\$6"
		Pop $8
		Pop $7
		Pop $6
		Pop $5
		Pop $0
FunctionEnd

;  Functions and macro to add shared DLL information to 
;  the registry
;------------------------------------------------------------
;!macro AddSharedDLL DLL_LOCATION
;  Call AddSharedDLLA
;  StrCpy $0 "${DLL_LOCATION}"
;  Call AddSharedDLLB
;!macroend

;Function AddSharedDLLA
;  Push $9
;  Push $0
;FunctionEnd

;Function AddSharedDLLB
;  ReadRegDWORD $0 HKLM Software\Microsoft\Windows\CurrentVersion\SharedDLLs $9
;  IntOp $0 $0 + 1
;  WriteRegDWORD HKLM Software\Microsoft\Windows\CurrentVersion\SharedDLLs $9 $0
;  Pop $0
;  Pop $9
;FunctionEnd
;------------------------------------------------------------
