visual studio - Start a service appliction using StartService in C++ -
i created windows service
application in c++
using visual studio 2013
. in main()
function, call function body contains following code: :
service_table_entry sertable[] = { { const_cast<char *>(servicename.c_str()), (lpservice_main_function)servicemain }, { null, null } }; int res = startservicectrldispatcher(sertable); if (res == 0) { debug_log(servicename+":startservicectrldispatcher failed", getlasterror()); return qerror; }
my main() function contains system("start notepad");
i can build program , executable generated. try open exe program using openscmanager()
, , createservice()
. service created , listed under services.msc
. right-clicked , started service , shows status
started
. nothing happens..
now if double click on exe
shows message: error 1063: startservicecontroldispatcher failed
, opens notepad.
why notepad not opened when service started under services.msc
?
you must launch notepad servicemain
not main
. servicemain
function invoked when service started os, move system("start notepad")
call servicemain
.
more clarifications
as response comment op - error happens 1063 or error_failed_service_controller_connect
. documentation of startservicectrldispatcher explains that:
this error returned if program being run console application rather service.
that why error when launching executable double click - launching console app. when launch service (either control panel, command prompt or winapi), function succeed.
when service control manager starts service process, waits process call startservicectrldispatcher function. main thread of service process should make call possible after starts (within 30 seconds). if startservicectrldispatcher succeeds, connects calling thread service control manager , not return until running services in process have entered service_stopped state. service control manager uses connection send control , service start requests main thread of service process. main thread acts dispatcher invoking appropriate handlerex function handle control requests, or creating new thread execute appropriate servicemain function when new service started.
so yes, after proper launch, servicemain
called.
Comments
Post a Comment