/[AnywhereTS-MSSQL]/trunk/TSAdminTool/Globals.cs
ViewVC logotype

Contents of /trunk/TSAdminTool/Globals.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 182 - (show annotations) (download)
Mon Jul 16 13:25:17 2012 UTC (8 years ago) by william
File size: 49214 byte(s)
terminalserverconfig value was not being read from registry: fixed

1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Reflection; // For path (assembly)
5 using System.IO; // For Path
6 using System.Security; // Security Exception
7 using System.Windows.Forms; // Message Box
8 using System.Text.RegularExpressions; // For validate MAC address
9 using log4net;
10
11 namespace AnywhereTS
12 {
13 public static class ATSGlobals
14 {
15 #region Global Definitions
16 // Version
17 public const string strDatabaseVersion = "1.0.0.4"; // The version string for the ATS database
18 public const string strDatabaseName = "AnywhereTS"; // THe name of the ATS database
19 //[SerializableAttribute] public struct Color;
20
21 // Registry keys
22 public static string strATSregRoot; // Registry root for AnywhereTS
23 public static string strTFTPD32RegRoot; // Registry root for TFTPD32
24 public const string strRegDHCPconfig = "DHCP"; // DCHP config in registry
25 public const string strRegTFTPconfig = "TFTP"; // TFTP root directory in registry
26 public const string strRegTFTP_root = "TFTP_root"; // TFTP root directory in registry
27 public const string strRegManagedMode = "ManagedMode"; // Reg key for managed mode.
28 public const string strRegConfigured = "Configured"; // Reg key for indicating of ATS is configured.
29 public const string strRegTerminalServer = "TerminalServer"; // Reg key for terinal server config.
30 public const string strRegRunFirstTime = "RunFirstTime"; // Reg key for first time run.
31 public const string strRegAdminVersion = "AdminVersion"; // Reg key for Admin version. Indicates that Admin app is intalled on this computer and the version of the admin app.
32
33 // Help
34 public static string helpFile; // The ATS help file
35
36 // Directories
37 static public string strTFTPdir; // The TFP root directory, including final "\"
38 static public string strExecPath; // The directory for the application exe-file
39 static public string strHelpFilePath; // The path (incl filname) to the application help file
40
41
42 // Other global variables
43 static public int managedMode; // 0=Not definied, 1=Managed, 2=Unmanged
44 static public int configured; // 0=ATS not configured, 1=ATS is configured, 2=Configured for control panel only.
45 static public int dhcpConfig; // 0=Use internal DHCP, 1=Other DHCP
46 static public int tftpConfig; // 0=Use internal TFTP, 1=Other TFTP
47 static public int terminalServerConfig; // 0=Terminal server on this computer, 1=No terminal server on this computer, 2=other terminal servers.
48 static public int runFirstTime; // 1=The application (or new version of it) is newly installed and the first time run procedures should be run. 0=The application has already been run.
49
50 static public bool isPro=true; // True = we are running the Pro version. Always true for the open source version.
51 static public bool isEval=true; //True if evalversion of client Non pro feature
52 static public string SelectedGraphicsAdaptersFile; // Name of the file for graphics adapters that corresponds to the users selection of linux kernel
53 static public string SelectedNicAdaptersFile; // Name of the file for network adapters that corresponds to the users selection of linux kernel
54 static public string SelectedSoundAdaptersFile; // Name of the file for sound adapters that corresponds to the users selection of linux kernel
55 static public string ApplicationName; // The name of the application. Used everywhere except for in about box and main window bar. Can be either original or an OEM name.
56 static public string ApplicationTitle = ApplicationName; // The title of the application, used for the main window bar and about box.
57
58 //File names
59 public const string GraphicsAdaptersA = "Graphics30.ats"; //The file containing the list of all graphics adapters
60 public const string NicAdaptersA = "Nic30.ats"; // The file containing the list of all network adapters
61 public const string SoundAdaptersA = "Sound30.ats"; // The file containing the list of all sound adapters
62
63 // Client connect time options
64 public static int[,] ScreenResolutions = new int[,] { { 640, 480 }, { 800, 600 }, { 1024, 600 }, { 1024, 768 }, { 1152, 864 }, { 1280, 768 }, { 1280, 800 }, { 1280, 960 }, { 1280, 1024 }, { 1368, 768 }, { 1400, 1050 }, { 1440, 900 }, { 1440, 1050 }, { 1600, 1000 }, { 1600, 1024 }, { 1600, 1200 }, { 1680, 1050 }, { 1792, 1344 }, { 1856, 1392 }, { 1920, 1080 }, { 1920, 1200 }, { 1920, 1440 } };
65 #endregion
66 #region static ATSGlobals()
67 // Constructor
68 static ATSGlobals()
69 {
70 Logging.ATSAdminLog.Debug("Entering ATSGlobals()");
71 ApplicationName = "Anywhere" + "TS"; // The name of the application. Used everywhere except for in about box and main window bar. Can be either original or an OEM name.
72
73 // Set registry root
74 // This is how we find the OS on run time
75 // on 32 bit OS IntPtr.Size = 4
76 // on 64 bit OS IntPtr.Size = 8
77 if (IntPtr.Size == 8)
78 { // 64 bit OS
79 strATSregRoot = @"SOFTWARE\Wow6432Node\" + ATSGlobals.ApplicationName + @"\ts-config";
80 }
81 else
82 { // 32 bit OS
83 strATSregRoot = @"SOFTWARE\" + ATSGlobals.ApplicationName + @"\ts-config";
84 }
85
86
87 // Set registry root for TFTPD32
88 // This is how we find the OS on run time
89 // on 32 bit OS IntPtr.Size = 4
90 // on 64 bit OS IntPtr.Size = 8
91 if (IntPtr.Size == 8)
92 { // 64 bit OS
93 strTFTPD32RegRoot = @"SOFTWARE\Wow6432Node\TFTPD32";
94 }
95 else
96 { // 32 bit OS
97 strTFTPD32RegRoot = @"SOFTWARE\TFTPD32";
98 }
99
100
101 helpFile = "AnywhereTS.chm"; // The ATS help file
102
103 //if (GetATSRegValueInt(strRegRunFirstTime) == 1)
104 //{ // We are runnning this version for the first time, write the needed registry values
105 // CreateRegistryValues();
106 //}
107
108 strExecPath = Path.GetDirectoryName(Application.ExecutablePath);
109
110 strHelpFilePath = strExecPath + @"\" + helpFile;
111
112 // Get TFTP config
113 tftpConfig = GetATSRegValueInt(strRegTFTPconfig);
114
115 // Get TFTP root
116 strTFTPdir = GetATSRegValueString(strRegTFTP_root);
117
118 // Get DHCP config
119 dhcpConfig = GetATSRegValueInt(strRegDHCPconfig);
120
121 configured = GetATSRegValueInt(strRegConfigured);
122
123 managedMode = GetATSRegValueInt(strRegManagedMode);
124
125 runFirstTime = GetATSRegValueInt(strRegRunFirstTime);
126
127 terminalServerConfig = GetATSRegValueInt(strRegTerminalServer);
128 // Currently only the 3.0 version of the drivers are supported
129 SelectedGraphicsAdaptersFile = GraphicsAdaptersA;
130 SelectedNicAdaptersFile = NicAdaptersA;
131 SelectedSoundAdaptersFile = SoundAdaptersA;
132
133 ProSupport.strDatabaseServer = GetATSRegValueString(ProSupport.strRegDatabaseServer);
134 ProSupport.strDatabaseInstance = GetATSRegValueString(ProSupport.strRegDatabaseInstance);
135
136 Logging.ATSAdminLog.Debug("Leaving ATSGlobals()");
137
138 }
139 #endregion
140
141 #region public static string GetATSRegValueString(string regValue)
142 // Get a registry string
143 public static string GetATSRegValueString(string regValue)
144 {
145 string value = string.Empty;
146 using (log4net.NDC.Push(string.Format("key={0} name={1}", strATSregRoot, regValue)))
147 {
148 Logging.ATSAdminLog.DebugFormat(@"Getting value from Registry: {0}\{1}", strATSregRoot, regValue);
149 value = GetRegValue(strATSregRoot, regValue);
150 Logging.ATSAdminLog.DebugFormat(@"value={0}", value);
151 }
152 return value;
153 }
154 #endregion
155
156 #region public static int GetATSRegValueInt(string regValue)
157 // Get a registry integer from the application key
158 public static int GetATSRegValueInt(string regValue)
159 {
160 int value = -1;
161 using (log4net.NDC.Push(string.Format("key={0} name={1}", strATSregRoot, regValue)))
162 {
163 Logging.ATSAdminLog.DebugFormat(@"Getting value from Registry: {0}\{1}", strATSregRoot, regValue);
164 value = GetRegValueInt(strATSregRoot, regValue);
165 Logging.ATSAdminLog.DebugFormat(@"value={0}", value);
166 }
167 return value;
168 }
169 #endregion
170
171 #region public static void SetATSRegValue(string regValue, int value)
172 // Set a registry integer in the the application key
173 public static void SetATSRegValue(string regValue, int value)
174 {
175 SetRegValue(strATSregRoot, regValue, value);
176 }
177 #endregion
178
179 #region public static void SetATSRegValue(string regValue, string value)
180 // Set a registry string in the the application key
181 public static void SetATSRegValue(string regValue, string value)
182 {
183 SetRegValue(strATSregRoot, regValue, value);
184 }
185 #endregion
186
187 #region public static string GetRegValue(string regKey, string regValue)
188 // Get a registry string
189 public static string GetRegValue(string regKey, string regValue)
190 {
191 Microsoft.Win32.RegistryKey objRegkey;
192
193 try
194 {
195 objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey);
196 }
197 catch (ArgumentNullException ex)
198 {
199 //MessageBox.Show("Error when reading from registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (22411)");
200 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", ex.Message, System.Environment.NewLine, ex.ToString())))
201 {
202 using (log4net.NDC.Push("Installation info missing."))
203 {
204 Logging.ATSAdminLog.Error("Error when reading from registry.");
205 }
206 }
207 Application.Exit();
208 return "";
209 }
210 catch (SecurityException ex)
211 {
212 //MessageBox.Show("Error when reading from registry. You do not have the necessary permission (22412). Key: " + regKey + " Value: " + regValue + " Error details:" + ex.Message);
213 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", ex.Message, System.Environment.NewLine, ex.ToString())))
214 {
215 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
216 {
217 using (log4net.NDC.Push("Invalid users rights."))
218 {
219 Logging.ATSAdminLog.Error("Error when reading from registry.");
220 }
221 }
222 }
223 Application.Exit();
224 return "";
225 }
226 catch (Exception ex)
227 {
228 //MessageBox.Show("Error when reading from registry (22413). Key: " + regKey + " Value: " + regValue + " Error details:" + ex.Message);
229 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", ex.Message, System.Environment.NewLine, ex.ToString())))
230 {
231 Logging.ATSAdminLog.Error("Error when reading from registry.");
232 }
233 Application.Exit();
234 return "";
235 }
236
237 if (objRegkey != null)
238 {
239 if (objRegkey.GetValue(regValue) != null)
240 {
241 try
242 {
243 return objRegkey.GetValue(regValue).ToString();
244 }
245 catch (Exception e)
246 {
247 //MessageBox.Show("Error when reading from registry (22414). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
248 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
249 {
250 Logging.ATSAdminLog.Error("Error when reading from registry.");
251 }
252 Application.Exit();
253 return "";
254 }
255 }
256 else
257 {
258 //MessageBox.Show("Error when reading from registry (55222). Value missing. Key: " + regKey + " Value: " + regValue);
259 using (log4net.NDC.Push(string.Format("key={0}", regKey)))
260 {
261 using (log4net.NDC.Push("Value is missing"))
262 {
263 Logging.ATSAdminLog.Error("Error when reading from registry.");
264 }
265 }
266 Application.Exit();
267 return "";
268 }
269 }
270 else
271 {
272 //MessageBox.Show("Error when reading from registry (55221). Key missing. Key: " + regKey + " Value: " + strRegTFTP_root);
273 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
274 {
275 using (log4net.NDC.Push("Key is missing"))
276 {
277 Logging.ATSAdminLog.Error("Error when reading from registry.");
278 }
279 }
280 Application.Exit();
281 }
282 return ""; // No value could be retrieved from registry
283 }
284 #endregion
285
286 #region public static int GetRegValueInt(string regKey, string regValue)
287 // Get a registry integer
288 public static int GetRegValueInt(string regKey, string regValue)
289 {
290
291 Microsoft.Win32.RegistryKey objRegkey;
292
293 try
294 {
295 objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey);
296 }
297 catch (ArgumentNullException ex)
298 {
299 //MessageBox.Show("Error when reading from registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (22411)");
300 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", ex.Message, System.Environment.NewLine, ex.ToString())))
301 {
302 using (log4net.NDC.Push("Installation info missing."))
303 {
304 Logging.ATSAdminLog.Error("Error when reading from registry.");
305 }
306 }
307 Application.Exit();
308 return 0;
309 }
310 catch (SecurityException e)
311 {
312 //MessageBox.Show("Error when reading from registry. You do not have the necessary permission (22412). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
313 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
314 {
315 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
316 {
317 using (log4net.NDC.Push("Invalid user rights."))
318 {
319 Logging.ATSAdminLog.Error("Error when reading from registry.");
320 }
321 }
322 }
323 Application.Exit();
324 return 0;
325 }
326 catch (Exception e)
327 {
328 //MessageBox.Show("Error when reading from registry (22413). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
329 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
330 {
331 Logging.ATSAdminLog.Error("Error when reading from registry.");
332 }
333 Application.Exit();
334 return 0;
335 }
336
337 if (objRegkey != null)
338 {
339 if (objRegkey.GetValue(regValue) != null)
340 {
341 try
342 {
343 return (int)objRegkey.GetValue(regValue);
344 }
345 catch (Exception e)
346 {
347 //MessageBox.Show("Error when reading from registry (22414). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
348 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
349 {
350 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
351 {
352 Logging.ATSAdminLog.Error("Error when reading from registry.");
353 }
354 }
355 Application.Exit();
356 return 0;
357 }
358 }
359 else
360 {
361 //MessageBox.Show("Error when reading from registry (55222). Value missing. Key: " + regKey + " Value: " + regValue);
362
363 using (log4net.NDC.Push(string.Format("key={0}", regKey)))
364 {
365 using (log4net.NDC.Push("Value is missing"))
366 {
367 Logging.ATSAdminLog.Error("Error when reading from registry.");
368 }
369 }
370
371 Application.Exit();
372 return 0;
373 }
374 }
375 else
376 {
377 //MessageBox.Show("Error when reading from registry (55221). Key missing. Key: " + regKey + " Value: " + regValue);
378
379 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
380 {
381 using (log4net.NDC.Push("Key is missing"))
382 {
383 Logging.ATSAdminLog.Error("Error when reading from registry.");
384 }
385 }
386
387 Application.Exit();
388 }
389 return 0; // No value could be retrieved from registry
390 }
391 #endregion
392
393 #region public static void SetRegValue(string regKey, string regValue, int value)
394 // Set a registry integer
395 public static void SetRegValue(string regKey, string regValue, int value)
396 {
397 using (log4net.NDC.Push(string.Format("key={0} name={1} data={2}", regKey, regValue, value)))
398 {
399 Logging.ATSAdminLog.DebugFormat(@"Setting value in Registry: {0}\{1}={2}", regKey, regValue, value);
400 Microsoft.Win32.RegistryKey objRegkey;
401
402 try
403 {
404 objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey, true);
405 }
406 catch (ArgumentNullException e)
407 {
408 MessageBox.Show("Error when writing to registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (62411)");
409 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
410 {
411 using (log4net.NDC.Push("Installation info missing"))
412 {
413 Logging.ATSAdminLog.Error("Error when writing to registry.");
414 }
415 }
416 Application.Exit();
417 return;
418 }
419 catch (SecurityException e)
420 {
421 MessageBox.Show("Error when writing to registry. You do not have the necessary permission (62412). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
422 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
423 {
424 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
425 {
426 using (log4net.NDC.Push("Invalid user rights"))
427 {
428 Logging.ATSAdminLog.Error("Error when writing to registry.");
429 }
430 }
431 }
432 Application.Exit();
433 return;
434 }
435 catch (Exception e)
436 {
437 MessageBox.Show("Error when reading from registry (62413). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
438 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
439 {
440 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
441 {
442 Logging.ATSAdminLog.Error("Error when reading from registry.");
443 }
444 }
445 Application.Exit();
446 return;
447 }
448
449 if (objRegkey != null)
450 {
451 try
452 {
453 objRegkey.SetValue(regValue, value);
454 }
455 catch (Exception e)
456 {
457 MessageBox.Show("Error when writing to registry (62414). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
458 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
459 {
460 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
461 {
462 Logging.ATSAdminLog.Error("Error when writing to registry.");
463 }
464 }
465 Application.Exit();
466 }
467 }
468 else
469 {
470 MessageBox.Show("Error when writing to registry (65222). Key missing. Key: " + regKey + " Value: " + regValue);
471
472 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
473 {
474 using (log4net.NDC.Push("Key is missing"))
475 {
476 Logging.ATSAdminLog.Error("Error when writing to registry.");
477 }
478 }
479
480 Application.Exit();
481 }
482 }
483 }
484 #endregion
485
486 #region public static void SetRegValue(string regKey, string regValue, string value)
487 // Set a registry string
488 public static void SetRegValue(string regKey, string regValue, string value)
489 {
490 using (log4net.NDC.Push(string.Format("key={0} name={1} data={2}", regKey, regValue, value)))
491 {
492 Logging.ATSAdminLog.DebugFormat(@"Setting value in Registry: {0}\{1}={2}", regKey, regValue, value);
493 Microsoft.Win32.RegistryKey objRegkey;
494
495 try
496 {
497 objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey, true);
498 }
499 catch (ArgumentNullException e)
500 {
501 MessageBox.Show("Error when writing to registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (62411)");
502 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
503 {
504 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
505 {
506 Logging.ATSAdminLog.Error("Error when writing to registry.");
507 }
508 }
509 Application.Exit();
510 return;
511 }
512 catch (SecurityException e)
513 {
514 MessageBox.Show("Error when writing to registry. You do not have the necessary permission (62412). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
515 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
516 {
517 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
518 {
519 using (log4net.NDC.Push("Invalid user rights"))
520 {
521 Logging.ATSAdminLog.Error("Error when writing to registry.");
522 }
523 }
524 }
525 Application.Exit();
526 return;
527 }
528 catch (Exception e)
529 {
530 MessageBox.Show("Error when reading from registry (62413). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
531 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
532 {
533 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
534 {
535 Logging.ATSAdminLog.Error("Error when reading from registry.");
536 }
537 }
538 Application.Exit();
539 return;
540 }
541
542 if (objRegkey != null)
543 {
544 try
545 {
546 objRegkey.SetValue(regValue, value);
547 }
548 catch (Exception e)
549 {
550 MessageBox.Show("Error when writing to registry (62414). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
551 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
552 {
553 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
554 {
555 Logging.ATSAdminLog.Error("Error when writing to registry.");
556 }
557 }
558 Application.Exit();
559 }
560 }
561 else
562 {
563 MessageBox.Show("Error when writing to registry (65222). Key missing. Key: " + regKey + " Value: " + regValue);
564 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
565 {
566 using (log4net.NDC.Push("Key is missing"))
567 {
568 Logging.ATSAdminLog.Error("Error when writing to registry.");
569 }
570 }
571
572 Application.Exit();
573 }
574 }
575 }
576 #endregion
577
578 #region public static bool RegValueExists(string regKey, string regValue)
579 // Check if a registry int value exists
580 public static bool RegValueExists(string regKey, string regValue)
581 {
582 Microsoft.Win32.RegistryKey objRegkey;
583
584 try
585 {
586 objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey);
587 }
588 catch (ArgumentNullException e)
589 {
590 //MessageBox.Show("Error when reading from registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (29411)");
591 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
592 {
593 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
594 {
595 using (log4net.NDC.Push("Installation info missing"))
596 {
597 Logging.ATSAdminLog.Error("Error when reading from registry.");
598 }
599 }
600 }
601 Application.Exit();
602 return false;
603 }
604 catch (SecurityException e)
605 {
606 //MessageBox.Show("Error when reading from registry. You do not have the necessary permission (29412). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
607 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
608 {
609 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
610 {
611 using (log4net.NDC.Push("Invalid user rights"))
612 {
613 Logging.ATSAdminLog.Error("Error when reading from registry.");
614 }
615 }
616 }
617 Application.Exit();
618 return false;
619 }
620 catch (Exception e)
621 {
622 //MessageBox.Show("Error when reading from registry (29413). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
623 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
624 {
625 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
626 {
627 Logging.ATSAdminLog.Error("Error when reading from registry.");
628 }
629 }
630 Application.Exit();
631 return false;
632 }
633
634 if (objRegkey != null)
635 {
636 if (objRegkey.GetValue(regValue) != null)
637 {
638 return true; // We could retrive the value
639 }
640 else
641 {
642 return false; // No value found
643 }
644 }
645 else
646 {
647 //MessageBox.Show("Error when reading from registry (59221). Key missing. Key: " + regKey + " Value: " + strRegTFTP_root);
648
649 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
650 {
651 using (log4net.NDC.Push("Key is missing"))
652 {
653 Logging.ATSAdminLog.Error("Error when reading from registry.");
654 }
655 }
656
657 Application.Exit();
658 }
659 return false; // No value could be retrieved from registry
660 }
661 #endregion
662
663 #region public static string IsValidMAC(string macAddress)
664 // Validate MAC a MAC address.
665 public static string IsValidMAC(string macAddress)
666 {
667 macAddress = macAddress.Replace(":", "");
668 macAddress = macAddress.ToUpper();
669 string result = "";
670 Regex rx = new Regex("([0-9a-fA-F][0-9a-fA-F]-){5}([0-9a-fA-F][0-9a-fA-F])", RegexOptions.IgnoreCase);
671 Match m = rx.Match(macAddress);
672 result = m.Groups[0].Value;
673 if (result.Length == 17)
674 {
675 return result;
676 }
677 else
678 {
679 rx = new Regex("([0-9a-fA-F][0-9a-fA-F]){5}([0-9a-fA-F][0-9a-fA-F])", RegexOptions.IgnoreCase);
680 Match m2 = rx.Match(macAddress);
681 result = m2.Groups[0].Value;
682 if (result.Length == 12)
683 {
684 return result;
685 }
686 return result;
687 }
688 }
689 #endregion
690
691 #region public static string GetMacFromATSname(string name)
692 // Try to extract a mac address from an ATS client name in the format 'ATSxxxxxxxxxxxx', where xxxxxxxxxx is a mac address.
693 public static string GetMacFromATSname(string name)
694 {
695 string strMac;
696 if (name.Length != 15)
697 {
698 return ""; // Not an ATS name
699 }
700 if (!name.StartsWith("ATS"))
701 {
702 return ""; // Not an ATS name
703 }
704 strMac = ATSGlobals.IsValidMAC(name.Substring(3));
705 if (strMac == "")
706 {
707 return ""; // Not an ATS name
708 }
709 // We have a MAC address
710 return strMac;
711 }
712 #endregion
713
714 #region public static void CreateRegistryValues()
715 // Create all needed registry values
716 public static void CreateRegistryValues()
717 {
718 // Create registry values for ATS Admin Application
719 CreateRegistryValue(strATSregRoot, "CheckUpdate", 1);
720 CreateRegistryValue(strATSregRoot, strRegConfigured, 0);
721 CreateRegistryValue(strATSregRoot, ProSupport.strRegAnywhereTSServer, "");
722 #if HAVE_FRMCONFIGMODE
723 CreateRegistryValue(strATSregRoot, ProSupport.strRegDatabaseDir, "");
724 CreateRegistryValue(strATSregRoot, ProSupport.strRegDestDir, "");
725 #endif
726 CreateRegistryValue(strATSregRoot, ProSupport.strRegDatabaseServer, ProSupport.strDatabaseServer);
727 CreateRegistryValue(strATSregRoot, ProSupport.strRegDatabaseInstance, ProSupport.strDatabaseInstance);
728
729
730 CreateRegistryValue(strATSregRoot, strRegDHCPconfig , 0);
731 CreateRegistryValue(strATSregRoot, strRegManagedMode, 1);
732 CreateRegistryValue(strATSregRoot, strRegTerminalServer, 0);
733 CreateRegistryValue(strATSregRoot, strRegTFTPconfig, 0);
734 CreateRegistryValue(strATSregRoot, strRegTFTP_root,"");
735
736 // Check if the admin app is intalled on this computer and if so, add reg values for TFTP 32
737 if (RegValueExists(strATSregRoot, strRegAdminVersion))
738 { // The admin app is installed, define keys for TFTP32. These should not be installed for the control panel.
739 // Create registry values for TFTPD32
740 CreateRegistryValue(strTFTPD32RegRoot, "BaseDirectory", "");
741 CreateRegistryValue(strTFTPD32RegRoot, "Beep", 0);
742 CreateRegistryValue(strTFTPD32RegRoot, "DirText", 0);
743 CreateRegistryValue(strTFTPD32RegRoot, "Hide", 0);
744 CreateRegistryValue(strTFTPD32RegRoot, "LastWindowPos", "60 49 860 642 ");
745 CreateRegistryValue(strTFTPD32RegRoot, "LocalIP", "");
746 CreateRegistryValue(strTFTPD32RegRoot, "MaxRetransmit", 6);
747 CreateRegistryValue(strTFTPD32RegRoot, "Negociate", 1);
748 CreateRegistryValue(strTFTPD32RegRoot, "PXECompatibility", 0);
749 CreateRegistryValue(strTFTPD32RegRoot, "SaveSyslogFile", "");
750 CreateRegistryValue(strTFTPD32RegRoot, "SecurityLevel", 1);
751 CreateRegistryValue(strTFTPD32RegRoot, "Services", 2);
752 CreateRegistryValue(strTFTPD32RegRoot, "ShowProgressBar", 0);
753 CreateRegistryValue(strTFTPD32RegRoot, "TftpLogFile", "");
754 CreateRegistryValue(strTFTPD32RegRoot, "TftpPort", 69);
755 CreateRegistryValue(strTFTPD32RegRoot, "Timeout", 3);
756 CreateRegistryValue(strTFTPD32RegRoot, "UnixStrings", 1);
757 CreateRegistryValue(strTFTPD32RegRoot, "VirtualRoot", 1);
758 CreateRegistryValue(strTFTPD32RegRoot, "WinSize", 0);
759
760 CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "AddOptionNumber1", 0);
761 CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "AddOptionNumber2", 0);
762 CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "AddOptionValue1", "");
763 CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "AddOptionValue2", "");
764 CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "BootFile", "client.zpxe");
765 CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "DNS", 16834314);
766 CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "DomainName", "");
767 CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "Gateway", 16834314);
768 CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "IP_Pool", 335601418);
769 CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "Mask", 65535);
770 CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "PoolSize", 200);
771 }
772 }
773 #endregion
774
775 #region public static void CreateRegistryValue(string regKey, string regValue, int value)
776 // If a registry key does not exist, create it with the given default value
777 public static void CreateRegistryValue(string regKey, string regValue, int value)
778 {
779 using (log4net.NDC.Push(string.Format("key={0} name={1} data={2}",regKey, regValue, value)))
780 {
781 Logging.ATSAdminLog.DebugFormat(@"Creating value in Registry: {0}\{1}={2}", regKey, regValue, value);
782 Microsoft.Win32.RegistryKey objRegkey;
783
784 try
785 {
786 objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey);
787 }
788 catch (ArgumentNullException e)
789 {
790 MessageBox.Show("Error when reading from registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (92411)");
791 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
792 {
793 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
794 {
795 using (log4net.NDC.Push("Installation info missing"))
796 {
797 Logging.ATSAdminLog.Error("Failed to create value in registry");
798 }
799 }
800 }
801 Application.Exit();
802 return;
803 }
804 catch (SecurityException e)
805 {
806 MessageBox.Show("Error when reading from registry. You do not have the necessary permission (92412). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
807 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
808 {
809 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
810 {
811 using (log4net.NDC.Push("Invalid user rights"))
812 {
813 Logging.ATSAdminLog.Error("Failed to create value in registry");
814 }
815 }
816 }
817 Application.Exit();
818 return;
819 }
820 catch (Exception e)
821 {
822 MessageBox.Show("Error when reading from registry (92413). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
823 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
824 {
825 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
826 {
827 Logging.ATSAdminLog.Error("Failed to create value in registry");
828
829 }
830 }
831 Application.Exit();
832 return;
833 }
834
835 if (objRegkey != null)
836 {
837 if (objRegkey.GetValue(regValue) == null)
838 { // No reg value, create one
839 SetRegValue(regKey, regValue, value);
840 }
841 }
842 else
843 {
844 MessageBox.Show("Error when reading from registry (95221). Key missing. Key: " + regKey + " Value: " + strRegTFTP_root);
845
846 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
847 {
848 Logging.ATSAdminLog.Error("Failed to create value in registry");
849 }
850
851 Application.Exit();
852 }
853 }
854 }
855 #endregion
856
857 #region public static void CreateRegistryValue(string regKey, string regValue, string value)
858 // If a registry key does not exist, create it with the given default value
859 public static void CreateRegistryValue(string regKey, string regValue, string value)
860 {
861 using (log4net.NDC.Push(string.Format("key={0} name={1} data={2}", regKey, regValue, value)))
862 {
863 Logging.ATSAdminLog.DebugFormat(@"Creating value in Registry: {0}\{1}={2}", regKey, regValue, value);
864 Microsoft.Win32.RegistryKey objRegkey;
865
866 try
867 {
868 objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey);
869 }
870 catch (ArgumentNullException e)
871 {
872 MessageBox.Show("Error when reading from registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (92411)");
873 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
874 {
875 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
876 {
877 using (log4net.NDC.Push("Installation info missing"))
878 {
879 Logging.ATSAdminLog.Error("Failed to create value in registry");
880 }
881 }
882 }
883 Application.Exit();
884 return;
885 }
886 catch (SecurityException e)
887 {
888 MessageBox.Show("Error when reading from registry. You do not have the necessary permission (92412). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
889 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
890 {
891 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
892 {
893 using (log4net.NDC.Push("Invalid users rights"))
894 {
895 Logging.ATSAdminLog.Error("Failed to create value in registry");
896 }
897 }
898 }
899 Application.Exit();
900 return;
901 }
902 catch (Exception e)
903 {
904 MessageBox.Show("Error when reading from registry (92413). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
905 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
906 {
907 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
908 {
909 Logging.ATSAdminLog.Error("Failed to create value in registry");
910 }
911 }
912 Application.Exit();
913 return;
914 }
915
916 if (objRegkey != null)
917 {
918 if (objRegkey.GetValue(regValue) == null)
919 { // No reg value, create one
920 SetRegValue(regKey, regValue, value);
921 }
922 }
923 else
924 {
925 MessageBox.Show("Error when reading from registry (95221). Key missing. Key: " + regKey + " Value: " + strRegTFTP_root);
926
927 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
928 {
929 Logging.ATSAdminLog.Error("Failed to create value in registry");
930 }
931
932 Application.Exit();
933 }
934 }
935 }
936 #endregion
937
938 #region public static bool ExtractIntegers(string integers, char separator, out int[] parsed_integers)
939 // Extract two integers from a string, separated by a character
940 // Result: True = extraction ok, false = invalid string.
941 // twointegers: String with two integers, separated by a character
942 // separator: the character separating the strings
943 // value1: (out) The first integer in the string
944 // value2: (out) The second integer in the string
945 public static bool ExtractIntegers(string integers, char separator, out int[] parsed_integers)
946 {
947 string[] parts;
948 parsed_integers = new int[0];
949 parts = integers.Split(separator);
950
951 List<int> t_ints = new List<int>();
952
953 foreach (string t in parts)
954 {
955 t_ints.Add(Convert.ToInt32(t));
956 }
957 parsed_integers = t_ints.ToArray();
958 return true;
959 }
960 #endregion
961 } // Class ATSGlobals
962 } // Namespace Anywhere TS

  ViewVC Help
Powered by ViewVC 1.1.22