bool install_service() { SCManager service_control_manager(SC_MANAGER_CREATE_SERVICE); const fs::path path = get_module_filename(); SC_HANDLE service = ::CreateService( service_control_manager.handle(), SERVICE_NAME, SERVICE_NAME, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, path.string<std::basic_string<TCHAR> >().c_str(), NULL, NULL, SERVICE_DEPENDENCIES, SERVICE_START_NAME, NULL ); if (service) { ::CloseServiceHandle(service); return true; } else { DWORD last_error = ::GetLastError(); switch (last_error) { case ERROR_SERVICE_EXISTS: return false; } throw boost::system::system_error(last_error, boost::system::system_category(), "CreateService()"); } }
int main(int argc, char** argv) { int rc = 127; HINSTANCE hInstance = GetModuleHandle(NULL); const TCHAR* args = skip_program_name(GetCommandLine()); TCHAR* modulename = get_module_filename(hInstance); TCHAR* moduleext = (TCHAR*)get_file_ext(modulename); size_t maxlen = 0; const TCHAR** ext; TCHAR* name; TCHAR* nameext; int found = 0; for (ext = config.extensions; *ext; ++ext) { size_t len = _tcslen(*ext); if (maxlen < len) maxlen = len; } name = malloc(((moduleext - modulename) + maxlen + 1) * sizeof(TCHAR)); _tcsncpy(name, modulename, moduleext - modulename); nameext = name + (moduleext - modulename); SetConsoleCtrlHandler(&CtrlHandler, TRUE); /* let children handle Ctrl+C */ for (ext = config.extensions; *ext; ++ext) { _tcscpy(nameext, *ext); if (!try_script(name, args, &rc)) { found = 1; break; } } SetConsoleCtrlHandler(&CtrlHandler, FALSE); /* restore default behavior */ if (!found) _ftprintf(stderr, _T("Error: stub could not find or execute the script file\n")); free(name); free(modulename); return rc; }