create service

Hello.I try to create a service.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int _tmain (int argc, TCHAR *argv[])
{

   SERVICE_TABLE_ENTRY ServiceTable[2] ; 

ServiceTable[0].lpServiceName=L"My Sample Service";
ServiceTable[0].lpServiceProc= (LPSERVICE_MAIN_FUNCTION) ServiceMain;
ServiceTable[1].lpServiceName=NULL;
ServiceTable[1].lpServiceProc=NULL;


    if (StartServiceCtrlDispatcher(ServiceTable) == FALSE)
       return GetLastError ();
    return 0;
}

I have no error during compilation but
It returns StartServiceCtrlDispatcher(ServiceTable) = FALSE
Did I miss to declare something?.
Thanks
Is the service installed?

What does GetLastError() return?
Last edited on
Hello.
1
2
3
HRESULT hr;
    if (StartServiceCtrlDispatcher(ServiceTable) == FALSE)
       hr= GetLastError ();

hr 0x00000427 The service process could not connect to the service controller.

I need to install the service first ?,how do I code it?


Last edited on
@ 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.
Hello.
[SC] StartService:OpenService FAILED 1060:
The specified service does not exist as an installed service.
I need to install the service first ?,how do I code it?
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.
Hello
 
SC_HANDLE h=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);

Access is denied.
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.
Last edited on
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.
Thank you very much,the Service loads and unloads.
I can see it in the Service control manager.
We keep in touch,I maybe need help.
Thanks
Computergeek01 wrote:
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.
Topic archived. No new replies allowed.