Detecting when head phones are plugged in
Solution 1:
In Windows Vista and beyond, you can use the device arrival and removal notifications and retrieve the endpoint formfactor to determine if the manufacturer of your audio solution considers a particular endpoint a "headphone".
Before Vista there was no way of determining this information.
Solution 2:
Many audio codecs provide "jack detection", that's probably the term you need to google.
It appears that under Vista, you can use IMMDeviceEnumerator::GetDefaultAudioEndpoint() and IMMDevice::GetState() to check the state of an 'endpoint'. There is also the 'IKsJackDescription Interface'.
Under XP you might try 'waveInGetNumDevs' - the driver may report 0 until the headphones are plugged in. See the comments in this discussion for more info.
Solution 3:
I think you cannot provide a universal solution.
I also point out the nray's solution. Most Realtek sound cards, Specially realtek on Intel main boards has this feature. But in that case the hardware is not a "Standard" one. In other words in those sound cards there should be special mechanism to detect the change in resistence over the nodes of the output sockets and "Fire an event"
If you have that sort of sound card + you just need a working, but not perfect solution, Just modify the routine that calls the configure program. (A program to mute/unmute ). Then replace the configurer program (You may find it using process explorer) with your new utility.
N.B I am not 100% sure whether we can identify plug in - plug off distinctly.
Solution 4:
It might be possible - RealTek has a system tray utility that pops up an annoying a helpful utility for tuning your audio whenever you connect something to the microphone jack, maybe the headphone jack is similar.