what did I do wrong ?
I'm guessing Debasis moved this from the Introductions Forum to the C/C++ Forum, but we usually request that members post what they have tried. It helps us understand if you are just completely missing something basic, a simple syntax error, or something more complicated.
Right now, you've asked a very general question, and anything I respond to you with will be stuff you can find by doing a simple Google search. So have you attempted to implement this?
(Though I would suggest looking for the Winternals utility to create a service - that might do what you're looking for.)