Detecting Overdamage

Coding Help

Is there a way to detect the amount of damage that a spell or attack does beyond the lethal part?
Example: Spell does 300 damage. Target has 100 life and no damage reduction towards spell damage. Difference is 200, this is what I need.
In a scenario where units have different damage reductions this will be kind of complicated.
I was thinking about dealing a little damage (A), getting the actual damage done (B) and calculating the reduction from the quotient (A-B) / A. Then one could calculate the damage needed to kill this unit and thus get the difference. I wonder if there's a more efficient way though, because these seems rather complicated. Help please :)

EDIT: Cannot remove my own post. I know how it's done. /thread
Just for information for other users, this can be done with a damage detection system or giving a good use of [lcode=jass]GetEventDamage[/lcode] function, with the event [lcode=jass]TriggerRegisterUnitEvent[/lcode].
Since you are talking about Damage Detection. I think it is so hard to decide which one to use because there are so many out there, and some are not finished or just not effective anymore. Could you recommend a decent one? I'm looking for a system with the possibility of a little customization (like damage block, different damage types detected) but also don't want to create 10 abilities myself and look for 5 different libraries to make it work.