[Jsmooth-cvs] jsmooth/skeletons/winservice Makefile.win, NONE, 1.1 WinService.cpp, NONE, 1.1 WinSer
Status: Beta
Brought to you by:
reyes
Update of /cvsroot/jsmooth/jsmooth/skeletons/winservice In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv8562/winservice Added Files: Makefile.win WinService.cpp WinService.h main.cpp mainres.rc resource.h winservice_private.h winservice_private.rc Log Message: adds windows service skeleton --- NEW FILE: main.cpp --- /* JSmooth: a VM wrapper toolkit for Windows Copyright (C) 2003-2007 Rodrigo Reyes <re...@ch...> This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include <iostream> #include <stdlib.h> #include <windows.h> #include "resource.h" #include "common.h" #include "ResourceManager.h" #include "JVMRegistryLookup.h" #include "JavaMachineManager.h" #include "WinService.h" using namespace std; extern WinService* winservice_ref; void _debugOutput(const std::string& text) { winservice_ref->log(text); } void _debugWaitKey() { } 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; } --- NEW FILE: WinService.h --- /* JSmooth: a VM wrapper toolkit for Windows Copyright (C) 2003-2007 Rodrigo Reyes <re...@ch...> This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifndef __JSMOOTHCOREWINSERVICE_H_ #define __JSMOOTHCOREWINSERVICE_H_ #include "Log.h" #include <string> #include <windows.h> #include <stdio.h> #include <stdlib.h> #include <process.h> #include "Thread.h" #include "SunJVMLauncher.h" class WinService { protected: Log* m_log; std::string m_serviceName; SERVICE_TABLE_ENTRY m_dispatchTable[2]; SERVICE_STATUS_HANDLE m_serviceStatusHandle; DWORD m_serviceCurrentStatusId; char m_cname[256]; int m_status_checkpoint; HANDLE m_stopEventHandle; Thread m_serviceThread; SunJVMLauncher* m_jvm; public: WinService(const std::string& name); void connect(); void serviceMain(DWORD argCount,LPSTR* arguments); void serviceCtrlHandler(DWORD); void run(); void stop(); bool install(); bool uninstall(); bool setStatus(int currentState); const char* getName() const; void log(const std::string& msg) const; void log(const char* msg) const; }; #endif --- NEW FILE: WinService.cpp --- /* JSmooth: a VM wrapper toolkit for Windows Copyright (C) 2003-2007 Rodrigo Reyes <re...@ch...> This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "WinService.h" #include "FileUtils.h" #include "StringUtils.h" #include "resource.h" #include "common.h" #include "ResourceManager.h" #include "JVMRegistryLookup.h" #include "JavaMachineManager.h" WinService* winservice_ref; void winservice_thread_support(void* param) { WinService* service = (WinService*)param; service->run(); } void winservice_ctrlhandler(DWORD ccode) { WinService* service = winservice_ref; service->serviceCtrlHandler(ccode); } WINAPI void *winservice_servicemain_support(DWORD argCount,LPSTR* arguments) { WinService* service = winservice_ref; service->serviceMain(argCount,arguments); } void winservice_thread_stop(void* param) { WinService* service = (WinService*)param; service->stop(); } WinService::WinService(const std::string& name) { m_log = new Log("c:\\winservice.log"); m_log->out("Constructing winservice object " + name); m_jvm = 0; m_status_checkpoint = 0; m_serviceName = name; strcpy(m_cname, m_serviceName.c_str()); m_dispatchTable[0].lpServiceName = m_cname; m_dispatchTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)winservice_servicemain_support; m_dispatchTable[1].lpServiceName = 0; m_dispatchTable[1].lpServiceProc = 0; winservice_ref = this; } void WinService::connect() { log(std::string("setting up a connection with the control dispatcher (") + getName() + ")"); int handle = StartServiceCtrlDispatcher(m_dispatchTable); if (handle == 0) { int err = GetLastError(); printf("connect error %d (%d)!\n", err, ERROR_INVALID_DATA); } printf("connection done %d!\n", handle); } bool WinService::install() { log(std::string("Installation of ") + getName()); HANDLE scman = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); if (scman == 0) return false; 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); if (service == 0) { log("Failed to install!"); return false; } log("Service installed!"); return true; } bool WinService::uninstall() { bool result = false; HANDLE scman = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); if (scman == 0) return result; std::string exepath = FileUtils::getExecutablePath(); 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) { result = DeleteService(service ); } log("Service uninstalled!"); CloseServiceHandle((SC_HANDLE)scman); return result; } bool WinService::setStatus(int currentState) { SERVICE_STATUS status; log("set status " + StringUtils::toString(currentState)); status.dwServiceType = SERVICE_WIN32_OWN_PROCESS; status.dwCurrentState = currentState; if ( currentState==SERVICE_START_PENDING ) status.dwControlsAccepted=0; else 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 SetServiceStatus(m_serviceStatusHandle,&status); } const char* WinService::getName() const { return m_cname; } void WinService::log(const std::string& msg) const { m_log->out(msg); } void WinService::log(const char* msg) const { m_log->out(msg); } void WinService::serviceMain(DWORD argCount,LPSTR* arguments) { log(std::string("in service main... (") + StringUtils::toString(argCount) + std::string(" args)")); for (int i=0; i<argCount; i++) { log( std::string("argument ") + StringUtils::toString(i) + ": " + arguments[i]); } m_serviceStatusHandle = RegisterServiceCtrlHandler(getName(), (LPHANDLER_FUNCTION)winservice_ctrlhandler); setStatus(SERVICE_START_PENDING); m_stopEventHandle = CreateEvent(NULL,TRUE, FALSE,NULL); if (!setStatus(SERVICE_START_PENDING)) return; m_serviceThread.start(winservice_thread_support, (void*)this); setStatus(SERVICE_RUNNING); m_serviceThread.join(); setStatus(SERVICE_STOPPED); } void WinService::serviceCtrlHandler(DWORD nControlCode) { switch(nControlCode) { case SERVICE_CONTROL_SHUTDOWN: case SERVICE_CONTROL_STOP: setStatus(SERVICE_STOP_PENDING); m_serviceThread.start(winservice_thread_stop, (void*)this); setStatus(SERVICE_STOP_PENDING); return; default: break; } setStatus(SERVICE_RUNNING); } void WinService::run() { setStatus(SERVICE_START_PENDING); ResourceManager* globalResMan = new ResourceManager("JAVA", PROPID, JARID); // // sets up the debug mode, if requested std::string dodebug = globalResMan->getProperty("skel_Debug"); if (StringUtils::parseInt(dodebug) != 0) { globalResMan->printDebug(); } JavaMachineManager man(*globalResMan); SunJVMLauncher* launcher = man.runDLLFromRegistry(true); if (launcher != 0) { m_jvm = launcher; log("JVM found and instanciated successfully (" + launcher->toString() + ")"); setStatus(SERVICE_RUNNING); launcher->dllInstanciate(*globalResMan, "wsreg"); log("Launched successfully the DLL (" + launcher->toString() + ")"); } else { log("ERROR: could not launch the JVM DLL"); } setStatus(SERVICE_STOPPED); log("stopped!"); return; } void WinService::stop() { log("requesting stop..."); setStatus(SERVICE_STOP_PENDING); if (m_jvm != 0) { setStatus(SERVICE_STOPPED); m_jvm->callDLLStaticMethod("java.lang.System", "exit", "()V"); log("exit called"); m_jvm->destroyVM(); log("vm destroyed"); } } --- NEW FILE: Makefile.win --- # Project: WinService Wrapper # Makefile created by Dev-C++ 4.9.8.0 RM = cmd /C DEL CPP = g++.exe CC = gcc.exe WINDRES = windres.exe RES = winservice_private.res OBJ = main.o $(RES) LINKOBJ = WinService.o main.o $(RES) LIBS = -L"/lib" -L"../commonjava" -L"../util-core" ../commonjava/CommonJava.a ../util-core/util-core.a INCS = -I"/include" -I"../util-core" -I"../commonjava" -I"$(JDK)/include" -I"$(JDK)/include/win32" CXXINCS = -I"/include/c++" -I"/include/c++/mingw32" -I"/include/c++/backward" -I"/include" -I"../commonjava" -I"$(JDK)/include" -I"$(JDK)/include/win32" -I../util-core BIN = winservice.exe CXXFLAGS = $(CUSTOMFLAGS) $(CXXINCS) -DJDK="$(JDK)" CFLAGS = $(INCS) .PHONY: all all-before all-after clean clean-custom all: all-before winservice.exe all-after clean: clean-custom $(RM) $(OBJ) $(BIN) $(BIN): $(LINKOBJ) ../commonjava/CommonJava.a $(CPP) $(LINKOBJ) -o "winservice.exe" $(LIBS) main.o: main.cpp $(CPP) -c main.cpp -o main.o $(CXXFLAGS) winservice_private.res: winservice_private.rc mainres.rc $(WINDRES) -i winservice_private.rc -I rc -o winservice_private.res -O coff --- NEW FILE: mainres.rc --- #include "resource.h" JARID JAVA "sample/sample.jar" PROPID JAVA "sample/sample.props" A2 ICON MOVEABLE PURE LOADONCALL DISCARDABLE "JWrap.ico" --- NEW FILE: winservice_private.rc --- // THIS FILE WILL BE OVERWRITTEN BY DEV-C++! // DO NOT EDIT! #include "mainres.rc" --- NEW FILE: resource.h --- #define JARID 102 #define PROPID 103 --- NEW FILE: winservice_private.h --- // THIS FILE WILL BE OVERWRITTEN BY DEV-C++! // DO NOT EDIT! #ifndef WINSERVICE_PRIVATE_H #define WINSERVICE_PRIVATE_H // VERSION DEFINITIONS #define VER_STRING "0.1.1.1" #define VER_MAJOR 0 #define VER_MINOR 1 #define VER_RELEASE 1 #define VER_BUILD 1 #define COMPANY_NAME "" #define FILE_VERSION "" #define FILE_DESCRIPTION "Developed using the Dev-C++ IDE" #define INTERNAL_NAME "" #define LEGAL_COPYRIGHT "" #define LEGAL_TRADEMARKS "" #define ORIGINAL_FILENAME "" #define PRODUCT_NAME "" #define PRODUCT_VERSION "" #endif //WINSERVICE_PRIVATE_H |