@ OP: How are you trying to run this? You're not just running the binary outright are you? You should be kicking this off with sc start <NAME_OF_YOUR_SERVICE, after you've added it with sc create <NAME_OF_YOUR_SERVICE binPath= <PATH_AND_NAME_OF_YOUR_BINARY> from the command prompt like kbw pointed out. Services are weird, they're PE's but they can't be run on their own.
Oh that one's easy. It's "OpenSCManager()" and "CreateService()". I would put it to execute if your call to "StartServiceCtrlDispatcher()" fails. Note that you cannot have duplicate names in the service control table.
Well you do have to be an admin OP. It says that right in the documentation:
Only processes with Administrator privileges are able to open a database handle that can be used by the CreateService function.
I kind of thought that one would be obvious. If you are an admin then your UAC settings might be too high, in that case right click and "Run As Administrator" and you may have to enter your credentials again.
Hello.
Right...
CreateService(.....
hr= GetLastError ();
It says hr 0x00000431 The specified service already exists.
But I can't find it in the Service control manager.
It won't matter if you can find it or not. You would delete it with sc delete <SERVICE_NAME> at the command prompt. Sometimes it will get stuck in "Service Pending Delete Status" which means that some part of it is still running.
Services are weird, they're PE's but they can't be run on their own.
A windows service is just a normal console application, it is not unusual that the same EXE is used for multiple purposes, like a service (after registration or self-registration) and as a normal user console application. The difference is usually make by command line arguments.