Author: shuston
Date: Tue Sep  4 21:15:47 2012
New Revision: 1380890

URL: http://svn.apache.org/viewvc?rev=1380890&view=rev
Log:
Use command-line and service-start parameters in broker started as a service. 
Resolves QPID-4269.

Modified:
    qpid/trunk/qpid/cpp/src/windows/QpiddBroker.cpp

Modified: qpid/trunk/qpid/cpp/src/windows/QpiddBroker.cpp
URL: 
http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/src/windows/QpiddBroker.cpp?rev=1380890&r1=1380889&r2=1380890&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/src/windows/QpiddBroker.cpp (original)
+++ qpid/trunk/qpid/cpp/src/windows/QpiddBroker.cpp Tue Sep  4 21:15:47 2012
@@ -30,8 +30,15 @@
 #include "qpid/broker/Broker.h"
 
 #include <iostream>
+#include <string>
+#include <vector>
 #include <windows.h>
 
+namespace {
+  // This will accept args from the command line; augmented with service args.
+  std::vector<std::string> 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 (int 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, LPTS
     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<char**>(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



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to