Changes to attempt to silence bcc64x
[ACE_TAO.git] / TAO / orbsvcs / Notify_Service / NT_Notify_Service.cpp
blob3e1b4adbd72ef95e0948f1101d9e810cd1cc2bd8
1 /* -*- C++ -*- */
2 #include /**/ "NT_Notify_Service.h"
4 #if defined (ACE_WIN32) && !defined (ACE_LACKS_WIN32_SERVICES)
6 #include /**/ "Notify_Service.h"
7 #include "tao/ORB_Core.h"
8 #include "ace/ARGV.h"
9 #include "ace/Reactor.h"
10 #include "orbsvcs/Log_Macros.h"
12 #define REGISTRY_KEY_ROOT HKEY_LOCAL_MACHINE
13 #define TAO_REGISTRY_SUBKEY ACE_TEXT ("SOFTWARE\\ACE\\TAO")
14 #define TAO_NOTIFY_SERVICE_OPTS_NAME ACE_TEXT ("TaoNotifyServiceOptions")
15 #define TAO_SERVICE_PARAM_COUNT ACE_TEXT ("TaoServiceParameterCount")
17 TAO_NT_Notify_Service::TAO_NT_Notify_Service ()
18 : argc_ (0),
19 argc_save_ (0),
20 argv_ (0),
21 argv_save_ (0)
25 TAO_NT_Notify_Service::~TAO_NT_Notify_Service ()
27 if (argv_save_)
29 for (int i = 0; i < argc_save_; i++)
30 ACE_OS::free (argv_save_[i]);
32 ACE_OS::free (argv_save_);
36 void
37 TAO_NT_Notify_Service::handle_control (DWORD control_code)
39 if (control_code == SERVICE_CONTROL_SHUTDOWN
40 || control_code == SERVICE_CONTROL_STOP)
42 report_status (SERVICE_STOP_PENDING);
43 TAO_ORB_Core_instance ()->reactor ()->end_reactor_event_loop ();
44 TAO_ORB_Core_instance ()->orb ()->shutdown (1);
45 report_status (SERVICE_STOPPED);
47 else
48 ACE_NT_Service::handle_control (control_code);
51 int
52 TAO_NT_Notify_Service::handle_exception (ACE_HANDLE)
54 return 0;
57 void
58 TAO_NT_Notify_Service::report_error (const ACE_TCHAR *format,
59 const ACE_TCHAR *val,
60 LONG result)
62 ACE_TCHAR msg[100];
63 ACE_TEXT_FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM,
65 result,
66 LANG_SYSTEM_DEFAULT,
67 msg,
68 100,0);
69 ORBSVCS_DEBUG ((LM_DEBUG, format, val, msg));
72 void
73 TAO_NT_Notify_Service::arg_manip (char *args, DWORD arglen, bool query)
75 HKEY hkey = 0;
77 // It looks in the NT Registry under
78 // \\HKEY_LOCAL_MACHINE\SOFTWARE\ACE\TAO for the value of
79 // "TaoNotifyServiceOptions" for any Notify Service options such as
80 // "-ORBListenEndpoints".
82 // Get/Set Notify Service options from the NT Registry.
84 LONG result = ACE_TEXT_RegOpenKeyEx (REGISTRY_KEY_ROOT,
85 TAO_REGISTRY_SUBKEY,
87 query ? KEY_READ : KEY_WRITE,
88 &hkey);
90 DWORD type = REG_EXPAND_SZ;
92 if (query)
94 *args = '\0';
95 if (result == ERROR_SUCCESS)
97 result = ACE_TEXT_RegQueryValueEx (hkey,
98 TAO_NOTIFY_SERVICE_OPTS_NAME,
100 &type,
101 (BYTE *)args,
102 &arglen);
103 if (result != ERROR_SUCCESS)
105 this->report_error (ACE_TEXT ("Could not query %s, %s\n"),
106 TAO_NOTIFY_SERVICE_OPTS_NAME, result);
109 else
111 this->report_error (ACE_TEXT ("No key for %s, %s\n"),
112 TAO_REGISTRY_SUBKEY, result);
115 else
117 if (result != ERROR_SUCCESS)
119 result = ACE_TEXT_RegCreateKeyEx (REGISTRY_KEY_ROOT,
120 TAO_REGISTRY_SUBKEY,
124 KEY_WRITE,
126 &hkey,
129 DWORD bufSize = static_cast<DWORD>(ACE_OS::strlen (args) + 1);
130 if (result == ERROR_SUCCESS)
132 result = ACE_TEXT_RegSetValueEx (hkey,
133 TAO_NOTIFY_SERVICE_OPTS_NAME,
135 type,
136 (BYTE *)args,
137 bufSize);
138 if (result != ERROR_SUCCESS)
140 this->report_error (ACE_TEXT ("Could not set %s, %s\n"),
141 TAO_NOTIFY_SERVICE_OPTS_NAME, result);
144 else
146 this->report_error (ACE_TEXT ("Could not create key %s, %s\n"),
147 TAO_REGISTRY_SUBKEY, result);
151 RegCloseKey (hkey);
155 TAO_NT_Notify_Service::set_args (const ACE_TCHAR *args)
157 char argbuf[ACE_DEFAULT_ARGV_BUFSIZ];
158 if (args == 0)
160 this->arg_manip (argbuf, ACE_DEFAULT_ARGV_BUFSIZ, true);
161 ACE_OS::printf ("%s\n", argbuf);
163 else
165 ACE_OS::strcpy (argbuf, ACE_TEXT_ALWAYS_CHAR (args));
166 this->arg_manip (argbuf, 0, false);
168 return 0;
172 TAO_NT_Notify_Service::init (int argc,
173 ACE_TCHAR *argv[])
175 HKEY hkey = 0;
176 BYTE buf[ACE_DEFAULT_ARGV_BUFSIZ];
178 *buf = '\0';
180 // This solution is very kludgy. It looks in the NT Registry under
181 // \\HKEY_LOCAL_MACHINE\SOFTWARE\ACE\TAO for the value of
182 // "TaoNotifyServiceOptions" for any Notify Service options such as
183 // "-ORBEndpoint".
185 // Get Notify Service options from the NT Registry.
187 ACE_TEXT_RegOpenKeyEx (REGISTRY_KEY_ROOT,
188 TAO_REGISTRY_SUBKEY,
190 KEY_READ,
191 &hkey);
193 DWORD type;
194 DWORD bufSize = sizeof (buf);
196 ACE_TEXT_RegQueryValueEx (hkey,
197 TAO_NOTIFY_SERVICE_OPTS_NAME,
199 &type,
200 buf,
201 &bufSize);
203 RegCloseKey (hkey);
205 // Add options to the args list (if any).
207 if (ACE_OS::strlen ((char *) buf) > 0)
209 ACE_ARGV args ((const char*) buf);
210 // Allocate the internal args list to be one bigger than the
211 // args list passed into the function. We use a 'save' list in
212 // case we use a 'destructive' args list processor - this way we
213 // maintain the correct argv and argc for memory freeing
214 // operations in the destructor.
215 argv_save_ = (ACE_TCHAR **) ACE_OS::malloc (sizeof (ACE_TCHAR *) * (argc + args.argc ()));
217 // Copy the values into the internal args buffer.
218 int i;
219 for (i = 0; i < argc; i++)
220 argv_save_[i] = ACE_OS::strdup (argv[i]);
222 int j = 0;
223 for (i = argc; i < static_cast<int> ((args.argc () + argc)); i++)
224 argv_save_[i] = ACE_OS::strdup (args.argv ()[j++]);
226 // Set the arg counter.
227 argc_save_ = argc + args.argc ();
228 argc_ = argc_save_;
229 argv_ = argv_save_;
231 else
233 argc_ = argc;
234 argv_ = argv;
237 return 0;
241 TAO_NT_Notify_Service::svc ()
243 TAO_Notify_Service_Driver notify_service;
247 if (notify_service.init (argc_, argv_) == -1)
248 return -1;
250 report_status (SERVICE_RUNNING);
251 notify_service.run ();
253 catch (const CORBA::Exception& ex)
255 ex._tao_print_exception (ACE_TEXT ("TAO NT Notify Service"));
256 return -1;
259 return 0;
262 #endif /* ACE_WIN32 && !ACE_LACKS_WIN32_SERVICES */