c++ - Unable to stop a windows service with net stop command -


i have created windows service running fine doing following.

 sc_handle hservice = ::createservice(*m_serviceconfig,         // scm database                                                  name.c_str(),               // name of service                                                  displayname.c_str(),        // service name display                                                  service_all_access,         // desired access                                                   service_win32_own_process | service_interactive_process,    // service type (interactive debug)                                                  service_auto_start,         // start type                                                  service_error_normal,       // error control type                                                  path.c_str(),               // path service's binary                                                  nullptr,                    // no load ordering group                                                  nullptr,                    // no tag identifier                                                  dependencies,               // dependencies                                                  nullptr,                    // localsystem account                                                  nullptr);                   // no password 

as can see specified access service_all_access , online constant states have access pause continue , stop service,

however when run command net stop <service-name> following ouput

the requested pause continue or stop not valid service

my question is there wrong way im creating service?

service control handler function

dword winapi servicecontrolhandler(dword dwcontrol, dword dweventtype, lpvoid /*lpeventdata*/, lpvoid /*lpcontext*/) {     // handle requested control code.      switch (dwcontrol)     {     case service_control_stop:     tvlog_info(l"service_control_stop");     {         messageboxa(null, "please stop service", "uninstall service error!", mb_ok | mb_iconerror);      } 

the access rights specify in dwdesiredaccess parameter of createservice() have no effect on net stop command. access rights apply returned sc_handle , effect how handle interacts subsequent api calls.

the error seeing being caused calls setservicestatus() not including service_accept_stop flag in service_status::dwcontrolsaccepted field.

service_status structure

dwcontrolsaccepted
control codes service accepts , processes in handler function (see handler , handlerex). user interface process can control service specifying control command in controlservice or controlserviceex function. default, services accept service_control_interrogate value. accept service_control_deviceevent value, service must register receive device events using registerdevicenotification function.

the following control codes.

...

service_accept_stop
0x00000001

the service can stopped.

this control code allows service receive service_control_stop notifications.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -