karikari
karikari

Reputation: 6797

How to code the OnStart function for windows service program?

I created a service in Visual C++. Installed it using instsrv.exe and srvany.exe. When I check at the Services applet in Control Panel, the service is registered. But when I want to start it, this message appears:

the service on Local Computer started and then stopped. Some service stop automatically if they have no work to do, for example the  Performance logs and Alert Service.

I just have this code inside my OnStart function:

(void)system("C:\ReleaseExe\Example1Server.exe");

I my code correct? Or is there any other way to code it to make sure that the service can be in running state?

Upvotes: 0

Views: 1109

Answers (1)

Steve Townsend
Steve Townsend

Reputation: 54148

Assuming that exe you are starting up is actually your service, this is the wrong way to do it.

The Windows Service Control Manager (SCM) actually calls into your service's EXE via defined entry points - you need to provide the logic to do what your particular service needs in those entry points, including notifying the SCM of state changes in your service (STOPPED-> RUNNING, RUNNING->STOPPED, and so on).

Read the background info on MSDN, starting here, for clarification of what you must provide. Note that OnStart is specific to implementing Services in managed code, you won't be doing it that way in Visual C++. Perhaps C# would be an easier route?

Upvotes: 1

Related Questions