[Jsmooth-cvs] jsmooth/skeletons/winservice WinService.cpp, 1.1, 1.2 main.cpp, 1.1, 1.2 WinService.h
Status: Beta
Brought to you by:
reyes
From: Rodrigo R. <re...@us...> - 2007-04-09 21:47:04
|
Update of /cvsroot/jsmooth/jsmooth/skeletons/winservice In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv19885 Modified Files: WinService.cpp main.cpp WinService.h Log Message: update windows service Index: main.cpp =================================================================== RCS file: /cvsroot/jsmooth/jsmooth/skeletons/winservice/main.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** main.cpp 9 Apr 2007 17:15:50 -0000 1.1 --- main.cpp 9 Apr 2007 21:46:59 -0000 1.2 *************** *** 38,41 **** --- 38,42 ---- void _debugOutput(const std::string& text) { + if (winservice_ref != 0) winservice_ref->log(text); } *************** *** 44,65 **** int main(int argc, char *argv[]) ! { ! WinService winserv("mytest"); if (argc>1) { ! if (strcmp(argv[1], "-i")==0) { ! winserv.install(); } ! else if (strcmp(argv[1], "-d")==0) { ! winserv.uninstall(); } } else ! { ! winserv.connect(); ! } return 0; --- 45,102 ---- int main(int argc, char *argv[]) ! { ! winservice_ref = 0; ! ResourceManager resman("JAVA", PROPID, JARID); ! ! std::string serviceName = resman.getProperty("skel_ServiceName"); ! std::string serviceDisplayName = resman.getProperty("skel_ServiceDisplayName"); ! std::string serviceDescription = resman.getProperty("skel_ServiceDescription"); ! if (serviceDisplayName == "") ! serviceDisplayName = serviceName; ! ! std::string autostart = resman.getProperty("skel_Autostart"); ! ! fflush(stdout); ! ! WinService winserv(serviceName); ! winserv.setDisplayName(serviceDisplayName); ! winserv.setDescription(serviceDescription); ! winserv.setAutostart( (StringUtils::parseInt(autostart)==1)?true:false ); ! ! winserv.setAutostart( true ); if (argc>1) { ! if ((strcmp(argv[1], "-i")==0) || (strcmp(argv[1], "install")==0)) { ! if (winserv.install()) ! printf("Service %s installed.\n", serviceName.c_str()); ! else ! printf("Failed to install service %s.\n", serviceName.c_str()); } ! else if ((strcmp(argv[1], "-d")==0) || (strcmp(argv[1], "uninstall")==0)) { ! if (winserv.uninstall()) ! printf("Service %s uninstalled.\n", serviceName.c_str()); ! else ! printf("Failed to uninstall service %s.\n", serviceName.c_str()); ! } ! else if (strcmp(argv[1], "start")==0) ! { ! if (winserv.startService()) ! printf("Service %s started.\n", serviceName.c_str()); ! else ! printf("Failed to start service %s.\n", serviceName.c_str()); ! } ! else if (strcmp(argv[1], "stop")==0) ! { ! if (winserv.stopService()) ! printf("Service %s stopped.\n", serviceName.c_str()); ! else ! printf("Failed to stop service %s.\n", serviceName.c_str()); } } else ! winserv.connect(); return 0; Index: WinService.cpp =================================================================== RCS file: /cvsroot/jsmooth/jsmooth/skeletons/winservice/WinService.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** WinService.cpp 9 Apr 2007 17:15:50 -0000 1.1 --- WinService.cpp 9 Apr 2007 21:46:59 -0000 1.2 *************** *** 57,61 **** { WinService* service = (WinService*)param; ! service->stop(); } --- 57,61 ---- { WinService* service = (WinService*)param; ! service->kill(); } *************** *** 69,72 **** --- 69,75 ---- m_serviceName = name; + m_serviceDescription = ""; + m_autostart = false; + strcpy(m_cname, m_serviceName.c_str()); m_dispatchTable[0].lpServiceName = m_cname; *************** *** 75,78 **** --- 78,82 ---- m_dispatchTable[1].lpServiceProc = 0; winservice_ref = this; + m_log->out("Constructing winservice object " + name + " ... done!"); } *************** *** 99,113 **** std::string exepath = FileUtils::concFile(FileUtils::getExecutablePath(), FileUtils::getExecutableFileName()); HANDLE service = (HANDLE)CreateService((SC_HANDLE)scman, m_cname, ! m_cname, // service name to display ! SERVICE_ALL_ACCESS, // desired access ! SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS, // service type ! SERVICE_DEMAND_START, // start type ! SERVICE_ERROR_NORMAL, // error control type ! exepath.c_str(), // service's binary ! NULL, // no load ordering group ! NULL, // no tag identifier ! NULL, // no dependencies ! NULL, // LocalSystem account ! NULL); // no password CloseServiceHandle((SC_HANDLE)scman); --- 103,117 ---- std::string exepath = FileUtils::concFile(FileUtils::getExecutablePath(), FileUtils::getExecutableFileName()); HANDLE service = (HANDLE)CreateService((SC_HANDLE)scman, m_cname, ! m_serviceDisplayName.c_str(), // service name to display ! SERVICE_ALL_ACCESS, // desired access ! SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS, // service type ! m_autostart?SERVICE_AUTO_START:SERVICE_DEMAND_START, // start type ! SERVICE_ERROR_NORMAL, // error control type ! exepath.c_str(), // service's binary ! NULL, // no load ordering group ! NULL, // no tag identifier ! NULL, // no dependencies ! NULL, // LocalSystem account ! NULL); // no password CloseServiceHandle((SC_HANDLE)scman); *************** *** 228,232 **** { setStatus(SERVICE_START_PENDING); ! ResourceManager* globalResMan = new ResourceManager("JAVA", PROPID, JARID); --- 232,237 ---- { setStatus(SERVICE_START_PENDING); ! log("Run..."); ! ResourceManager* globalResMan = new ResourceManager("JAVA", PROPID, JARID); *************** *** 234,237 **** --- 239,243 ---- // sets up the debug mode, if requested std::string dodebug = globalResMan->getProperty("skel_Debug"); + dodebug = "1"; if (StringUtils::parseInt(dodebug) != 0) { *************** *** 261,265 **** } ! void WinService::stop() { log("requesting stop..."); --- 267,271 ---- } ! void WinService::kill() { log("requesting stop..."); *************** *** 274,275 **** --- 280,343 ---- } } + + bool WinService::startService() + { + HANDLE scman = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); + if (scman == 0) + return false; + + SC_HANDLE service = OpenService((SC_HANDLE)scman, // handle to service control manager database + m_cname, // pointer to name of service to start + SERVICE_ALL_ACCESS // type of access to service + ); + if (service != 0) + { + return StartService(service, 0, NULL); + } + + return false; + } + + bool WinService::stopService() + { + HANDLE scman = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); + if (scman == 0) + return false; + + SC_HANDLE service = OpenService((SC_HANDLE)scman, // handle to service control manager database + m_cname, // pointer to name of service to start + SERVICE_ALL_ACCESS // type of access to service + ); + if (service != 0) + { + SERVICE_STATUS status; + + status.dwServiceType = SERVICE_WIN32_OWN_PROCESS; + status.dwCurrentState = SERVICE_STOP_PENDING; + status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN; + status.dwWin32ExitCode = NO_ERROR; + status.dwServiceSpecificExitCode = 0; + status.dwCheckPoint = m_status_checkpoint++; + status.dwWaitHint = 60*1000; + + return ControlService(service, SERVICE_CONTROL_STOP, &status); + } + + return false; + } + + + void WinService::setDescription(const std::string& description) + { + m_serviceDescription = description; + } + + void WinService::setAutostart(bool b) + { + m_autostart = b; + } + + void WinService::setDisplayName(const std::string& displayname) + { + m_serviceDisplayName = displayname; + } Index: WinService.h =================================================================== RCS file: /cvsroot/jsmooth/jsmooth/skeletons/winservice/WinService.h,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** WinService.h 9 Apr 2007 17:15:50 -0000 1.1 --- WinService.h 9 Apr 2007 21:46:59 -0000 1.2 *************** *** 37,40 **** --- 37,44 ---- Log* m_log; std::string m_serviceName; + std::string m_serviceDisplayName; + std::string m_serviceDescription; + bool m_autostart; + SERVICE_TABLE_ENTRY m_dispatchTable[2]; SERVICE_STATUS_HANDLE m_serviceStatusHandle; *************** *** 57,65 **** void run(); ! void stop(); bool install(); bool uninstall(); bool setStatus(int currentState); --- 61,73 ---- void run(); ! void kill(); bool install(); bool uninstall(); + bool startService(); + bool stopService(); + + bool setStatus(int currentState); *************** *** 68,71 **** --- 76,84 ---- void log(const std::string& msg) const; void log(const char* msg) const; + + void setDisplayName(const std::string& displayname); + void setDescription(const std::string& description); + void setAutostart(bool b); + }; |