From daad975f5d46438450c6465fff44132e5ef8e3ae Mon Sep 17 00:00:00 2001 From: Mikhail Titov Date: Thu, 10 Mar 2016 00:12:46 -0600 Subject: [PATCH] fixup! invoke win32app functions from main --- DaemonWin32.cpp | 43 +++++++++++++++++++++++++++++++++++++++++-- build/CMakeLists.txt | 14 +++++++++----- i2pd.cpp | 23 +++++++++++++++++++---- 3 files changed, 69 insertions(+), 11 deletions(-) diff --git a/DaemonWin32.cpp b/DaemonWin32.cpp index 02c602f0..64b5b511 100644 --- a/DaemonWin32.cpp +++ b/DaemonWin32.cpp @@ -5,6 +5,7 @@ #ifdef _WIN32 +#include "Win32/Win32Service.h" #include "Win32/Win32App.h" namespace i2p @@ -16,8 +17,46 @@ namespace i2p setlocale(LC_CTYPE, ""); SetConsoleCP(1251); SetConsoleOutputCP(1251); - setlocale(LC_ALL, "Russian"); - return Daemon_Singleton::init(argc, argv); + + if (!Daemon_Singleton::init(argc, argv)) + return false; + + std::string serviceControl; i2p::config::GetOption("svcctl", serviceControl); + if (serviceControl == "install") + { + LogPrint(eLogInfo, "WinSVC: installing ", SERVICE_NAME, " as service"); + InstallService( + SERVICE_NAME, // Name of service + SERVICE_DISPLAY_NAME, // Name to display + SERVICE_START_TYPE, // Service start type + SERVICE_DEPENDENCIES, // Dependencies + SERVICE_ACCOUNT, // Service running account + SERVICE_PASSWORD // Password of the account + ); + return false; + } + else if (serviceControl == "remove") + { + LogPrint(eLogInfo, "WinSVC: uninstalling ", SERVICE_NAME, " service"); + UninstallService(SERVICE_NAME); + return false; + } + + if (isDaemon == 1) + { + LogPrint(eLogDebug, "Daemon: running as service"); + I2PService service(SERVICE_NAME); + if (!I2PService::Run(service)) + { + LogPrint(eLogError, "Daemon: Service failed to run w/err 0x%08lx\n", GetLastError()); + return false; + } + return false; + } + else + LogPrint(eLogDebug, "Daemon: running as user"); + + return true; } bool DaemonWin32::start() diff --git a/build/CMakeLists.txt b/build/CMakeLists.txt index ef5be84c..c9c590f6 100644 --- a/build/CMakeLists.txt +++ b/build/CMakeLists.txt @@ -157,6 +157,7 @@ elseif (CMAKE_SYSTEM_NAME STREQUAL "OpenBSD") list (APPEND DAEMON_SRC "${CMAKE_SOURCE_DIR}/DaemonLinux.cpp") elseif (CMAKE_SYSTEM_NAME STREQUAL "Windows" OR MSYS) list (APPEND DAEMON_SRC "${CMAKE_SOURCE_DIR}/DaemonWin32.cpp") + list (APPEND DAEMON_SRC "${CMAKE_SOURCE_DIR}/Win32/Win32App.cpp") list (APPEND DAEMON_SRC "${CMAKE_SOURCE_DIR}/Win32/Win32Service.cpp") list (APPEND DAEMON_SRC "${CMAKE_SOURCE_DIR}/Win32/Resource.rc") endif () @@ -309,11 +310,14 @@ include(GNUInstallDirs) if (WITH_BINARY) add_executable ( "${PROJECT_NAME}" ${DAEMON_SRC} ) - if(NOT MSVC) # FIXME: incremental linker file name (.ilk) collision for dll & exe - if (WITH_STATIC) - set_target_properties("${PROJECT_NAME}" PROPERTIES LINK_FLAGS "-static" ) - endif () - endif() + if (WIN32) + set_target_properties("${PROJECT_NAME}" PROPERTIES WIN32_EXECUTABLE TRUE ) + endif() + if(NOT MSVC) + if (WITH_STATIC) + set_target_properties("${PROJECT_NAME}" PROPERTIES LINK_FLAGS "-static" ) + endif () + endif() if (WITH_PCH) if (MSVC) diff --git a/i2pd.cpp b/i2pd.cpp index 6167f10e..f3ac6b3f 100644 --- a/i2pd.cpp +++ b/i2pd.cpp @@ -3,10 +3,25 @@ int main( int argc, char* argv[] ) { - Daemon.init(argc, argv); - if (Daemon.start()) - Daemon.run (); - Daemon.stop(); + if (Daemon.init(argc, argv)) + { + if (Daemon.start()) + Daemon.run (); + Daemon.stop(); + } return EXIT_SUCCESS; } +#ifdef _WIN32 +#include + +int CALLBACK WinMain( + _In_ HINSTANCE hInstance, + _In_ HINSTANCE hPrevInstance, + _In_ LPSTR lpCmdLine, + _In_ int nCmdShow + ) +{ + return main(__argc, __argv); +} +#endif