The medicine to treat adult heartworm is a cyanide. The worms can't process and eliminate it but dogs can.
Ivermectin kills the immature worms as a preventative, or along with the other drug in treatment.