c++ - StartServiceCtrlDispatcher don't can access 1063 error -
c++ - StartServiceCtrlDispatcher don't can access 1063 error -
i write on c++ service , have error, , don't can fixed it.
my service main function
int main (int argc, tchar *argv[]) {dword f; for(int i=0;i<113;i++) f = getlasterror(); servicepath = lptstr(argv[0]); outputdebugstring(_t("my sample service: main: entry")); //installservice(); service_table_entry servicetable[] = { {service_name, (lpservice_main_function) servicemain}, {null, null} }; // startservice(); if(!startservicectrldispatcher(servicetable)) { f = getlasterror(); //addlogmessage("error: startservicectrldispatcher"); } else if( memcmp(argv[argc-1],"install",7)) { installservice(); } else if( memcmp(argv[argc-1],"remove",6)) { removeservice(); } else if( memcmp(argv[argc-1],"start",5)) { startservice(); } else if( memcmp(argv[argc-1],"stop",4)) { // stopservice(); } // stopservice(); outputdebugstring(_text("my sample service: main: exit")); homecoming 0; }
where debug programm command line arguments (service_path , action) on startservicectrldispatcher every time homecoming 1063 error. visual studio run under administrator. write wrong code, please help.
update
void winapi servicemain (dword argc, lptstr *argv) { dword status = e_fail; outputdebugstring(_t("my sample service: servicemain: entry")); g_statushandle = registerservicectrlhandler (service_name, servicectrlhandler); if (g_statushandle == null) { outputdebugstring(_t("my sample service: servicemain: registerservicectrlhandler returned error")); goto exit; } // tell service controller starting zeromemory (&g_servicestatus, sizeof (g_servicestatus)); g_servicestatus.dwservicetype = service_win32_own_process; g_servicestatus.dwcontrolsaccepted = 0; g_servicestatus.dwcurrentstate = service_start_pending; g_servicestatus.dwwin32exitcode = 0; g_servicestatus.dwservicespecificexitcode = 0; g_servicestatus.dwcheckpoint = 0; if (setservicestatus (g_statushandle, &g_servicestatus) == false) { outputdebugstring(_t("my sample service: servicemain: setservicestatus returned error")); } /* * perform tasks neccesary start service here */ outputdebugstring(_t("my sample service: servicemain: performing service start operations")); // create stop event wait on later. g_servicestopevent = createevent (null, true, false, null); if (g_servicestopevent == null) { outputdebugstring(_t("my sample service: servicemain: createevent(g_servicestopevent) returned error")); g_servicestatus.dwcontrolsaccepted = 0; g_servicestatus.dwcurrentstate = service_stopped; g_servicestatus.dwwin32exitcode = getlasterror(); g_servicestatus.dwcheckpoint = 1; if (setservicestatus (g_statushandle, &g_servicestatus) == false) { outputdebugstring(_t("my sample service: servicemain: setservicestatus returned error")); } goto exit; } // tell service controller started g_servicestatus.dwcontrolsaccepted = service_accept_stop; g_servicestatus.dwcurrentstate = service_running; g_servicestatus.dwwin32exitcode = 0; g_servicestatus.dwcheckpoint = 0; if (setservicestatus (g_statushandle, &g_servicestatus) == false) { outputdebugstring(_t("my sample service: servicemain: setservicestatus returned error")); } // start thread perform main task of service handle hthread = createthread (null, 0, serviceworkerthread, null, 0, null); outputdebugstring(_t("my sample service: servicemain: waiting worker thread complete")); // wait until our worker thread exits signaling service needs stop waitforsingleobject (hthread, infinite); outputdebugstring(_t("my sample service: servicemain: worker thread stop event signaled")); /* * perform cleanup tasks */ outputdebugstring(_t("my sample service: servicemain: performing cleanup operations")); closehandle (g_servicestopevent); g_servicestatus.dwcontrolsaccepted = 0; g_servicestatus.dwcurrentstate = service_stopped; g_servicestatus.dwwin32exitcode = 0; g_servicestatus.dwcheckpoint = 3; if (setservicestatus (g_statushandle, &g_servicestatus) == false) { outputdebugstring(_t("my sample service: servicemain: setservicestatus returned error")); } exit: outputdebugstring(_t("my sample service: servicemain: exit")); return; } void winapi servicectrlhandler (dword ctrlcode) { outputdebugstring(_t("my sample service: servicectrlhandler: entry")); switch (ctrlcode) { case service_control_stop : outputdebugstring(_t("my sample service: servicectrlhandler: service_control_stop request")); if (g_servicestatus.dwcurrentstate != service_running) break; /* * perform tasks neccesary stop service here */ g_servicestatus.dwcontrolsaccepted = 0; g_servicestatus.dwcurrentstate = service_stop_pending; g_servicestatus.dwwin32exitcode = 0; g_servicestatus.dwcheckpoint = 4; if (setservicestatus (g_statushandle, &g_servicestatus) == false) { outputdebugstring(_t("my sample service: servicectrlhandler: setservicestatus returned error")); } // signal worker thread start shutting downwards setevent (g_servicestopevent); break; default: break; } outputdebugstring(_t("my sample service: servicectrlhandler: exit")); } dword winapi serviceworkerthread (lpvoid lpparam) { outputdebugstring(_t("my sample service: serviceworkerthread: entry")); main2(); outputdebugstring(_t("my sample service: serviceworkerthread: exit")); homecoming error_success; }
you can phone call startservicectrldispatcher
when process started service command manager, i.e., when running service. when called other context, error_failed_service_controller_connect
(1063).
from looks of code, should calling startservicecontroldispatcher
if no command-line argument passed, e.g., like
if (argc < 2) { if (!startservicectrldispatcher(servicetable)) { f = getlasterror(); } } else if (strcmp(argv[1], "install") { installservice(); }
and on.
there other problems main() function, notably:
the wrong signature; argv[] char, not tchar
casting argv[0] tchar
a loop calls getlasterror no reason, 114 times
the utilize of memcmp instead of strcmp
i didn't @ servicemain().
c++ winapi service
Comments
Post a Comment