is there any kind of function on windows API which will help me to know if there a device is connected or disconnected.
for example detecting a mass storage device(pen drive) is removed or attached.
For pen drives and similar, you don't have to do anything but listen to the message. I think that this message works for other hardware too, but you have to register to receive messages from them... I THINK. Haven't needed it so I think I forgot already about what I read once upon a time.