From 6fe89b1847b7d3bd2fd0dfb6584c9b4a3e55103f Mon Sep 17 00:00:00 2001 From: "Stephen D. Huston" Date: Tue, 11 Sep 2012 03:01:25 +0000 Subject: Fix QPID-4269 git-svn-id: https://svn.apache.org/repos/asf/qpid/branches/0.18@1383229 13f79535-47bb-0310-9956-ffa450edef68 --- qpid/cpp/src/windows/QpiddBroker.cpp | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/qpid/cpp/src/windows/QpiddBroker.cpp b/qpid/cpp/src/windows/QpiddBroker.cpp index 89a8945d00..4ca3f3059c 100644 --- a/qpid/cpp/src/windows/QpiddBroker.cpp +++ b/qpid/cpp/src/windows/QpiddBroker.cpp @@ -30,8 +30,15 @@ #include "qpid/broker/Broker.h" #include +#include +#include #include +namespace { + // This will accept args from the command line; augmented with service args. + std::vector cmdline_args; +} + namespace qpid { namespace broker { @@ -229,6 +236,25 @@ VOID WINAPI SvcCtrlHandler(DWORD control) VOID WINAPI ServiceMain(DWORD argc, LPTSTR *argv) { + // The arguments can come from 2 places. Args set with the executable + // name when the service is installed come through main() and are now + // in cmdline_args. Arguments set in StartService come into argc/argv + // above; if they are set, argv[0] is the service name. Make command + // line args first; StartService args come later and can override + // command line args. + int all_argc = argc + cmdline_args.size(); + if (argc == 0 && !cmdline_args.empty()) + ++all_argc; // No StartService args, so need to add prog name argv[0] + const char **all_argv = new const char *[all_argc]; + if (all_argc > 0) { + int i = 0; + all_argv[i++] = argc > 0 ? argv[0] : svcName.c_str(); + for (size_t j = 0; j < cmdline_args.size(); ++j) + all_argv[i++] = cmdline_args[j].c_str(); + for (DWORD k = 1; k < argc; ++k) + all_argv[i++] = argv[k]; + } + ::memset(&svcStatus, 0, sizeof(svcStatus)); svcStatusHandle = ::RegisterServiceCtrlHandler(svcName.c_str(), SvcCtrlHandler); @@ -238,7 +264,9 @@ VOID WINAPI ServiceMain(DWORD argc, LPTSTR *argv) svcStatus.dwCurrentState = SERVICE_START_PENDING; ::SetServiceStatus(svcStatusHandle, &svcStatus); // QpiddBroker class resets state to running. - svcStatus.dwWin32ExitCode = run_broker(argc, argv, true); + svcStatus.dwWin32ExitCode = run_broker(all_argc, + const_cast(all_argv), + true); svcStatus.dwCurrentState = SERVICE_STOPPED; svcStatus.dwCheckPoint = 0; svcStatus.dwWaitHint = 0; @@ -464,6 +492,11 @@ int main(int argc, char* argv[]) { "", (LPSERVICE_MAIN_FUNCTION)qpid::broker::ServiceMain }, { NULL, NULL } }; + // Copy any command line args to be available in case we're started + // as a service. Pick these back up in ServiceMain. + for (int i = 1; i < argc; ++i) + cmdline_args.push_back(argv[i]); + if (!StartServiceCtrlDispatcher(dispatchTable)) { DWORD err = ::GetLastError(); if (err == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) // Run as console -- cgit v1.2.1