ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/AnywhereTS-MSSQL/trunk/TSAdminTool/Globals.cs
Revision: 116
Committed: Sat Jul 14 08:03:33 2012 UTC (10 years, 8 months ago) by william
File size: 48853 byte(s)
Log Message:
+ more logging work

File Contents

# Content
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.
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 // Currently only the 3.0 version of the drivers are supported
128 SelectedGraphicsAdaptersFile = GraphicsAdaptersA;
129 SelectedNicAdaptersFile = NicAdaptersA;
130 SelectedSoundAdaptersFile = SoundAdaptersA;
131 Logging.ATSAdminLog.Debug("Leaving ATSGlobals()");
132
133 }
134 #endregion
135
136 #region public static string GetATSRegValueString(string regValue)
137 // Get a registry string
138 public static string GetATSRegValueString(string regValue)
139 {
140 string value = string.Empty;
141 using (log4net.NDC.Push(string.Format("key={0} name={1}", strATSregRoot, regValue)))
142 {
143 Logging.ATSAdminLog.DebugFormat(@"Getting value from Registry: {0}\{1}", strATSregRoot, regValue);
144 value = GetRegValue(strATSregRoot, regValue);
145 Logging.ATSAdminLog.DebugFormat(@"value={0}", value);
146 }
147 return value;
148 }
149 #endregion
150
151 #region public static int GetATSRegValueInt(string regValue)
152 // Get a registry integer from the application key
153 public static int GetATSRegValueInt(string regValue)
154 {
155 int value = -1;
156 using (log4net.NDC.Push(string.Format("key={0} name={1}", strATSregRoot, regValue)))
157 {
158 Logging.ATSAdminLog.DebugFormat(@"Getting value from Registry: {0}\{1}", strATSregRoot, regValue);
159 value = GetRegValueInt(strATSregRoot, regValue);
160 Logging.ATSAdminLog.DebugFormat(@"value={0}", value);
161 }
162 return value;
163 }
164 #endregion
165
166 #region public static void SetATSRegValue(string regValue, int value)
167 // Set a registry integer in the the application key
168 public static void SetATSRegValue(string regValue, int value)
169 {
170 SetRegValue(strATSregRoot, regValue, value);
171 }
172 #endregion
173
174 #region public static void SetATSRegValue(string regValue, string value)
175 // Set a registry string in the the application key
176 public static void SetATSRegValue(string regValue, string value)
177 {
178 SetRegValue(strATSregRoot, regValue, value);
179 }
180 #endregion
181
182 #region public static string GetRegValue(string regKey, string regValue)
183 // Get a registry string
184 public static string GetRegValue(string regKey, string regValue)
185 {
186 Microsoft.Win32.RegistryKey objRegkey;
187
188 try
189 {
190 objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey);
191 }
192 catch (ArgumentNullException ex)
193 {
194 MessageBox.Show("Error when reading from registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (22411)");
195 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", ex.Message, System.Environment.NewLine, ex.ToString())))
196 {
197 using (log4net.NDC.Push("Installation info missing."))
198 {
199 Logging.ATSAdminLog.Error("Error when reading from registry.");
200 }
201 }
202 Application.Exit();
203 return "";
204 }
205 catch (SecurityException ex)
206 {
207 MessageBox.Show("Error when reading from registry. You do not have the necessary permission (22412). Key: " + regKey + " Value: " + regValue + " Error details:" + ex.Message);
208 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", ex.Message, System.Environment.NewLine, ex.ToString())))
209 {
210 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
211 {
212 using (log4net.NDC.Push("Invalid users rights."))
213 {
214 Logging.ATSAdminLog.Error("Error when reading from registry.");
215 }
216 }
217 }
218 Application.Exit();
219 return "";
220 }
221 catch (Exception ex)
222 {
223 MessageBox.Show("Error when reading from registry (22413). Key: " + regKey + " Value: " + regValue + " Error details:" + ex.Message);
224 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", ex.Message, System.Environment.NewLine, ex.ToString())))
225 {
226 Logging.ATSAdminLog.Error("Error when reading from registry.");
227 }
228 Application.Exit();
229 return "";
230 }
231
232 if (objRegkey != null)
233 {
234 if (objRegkey.GetValue(regValue) != null)
235 {
236 try
237 {
238 return objRegkey.GetValue(regValue).ToString();
239 }
240 catch (Exception e)
241 {
242 MessageBox.Show("Error when reading from registry (22414). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
243 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
244 {
245 Logging.ATSAdminLog.Error("Error when reading from registry.");
246 }
247 Application.Exit();
248 return "";
249 }
250 }
251 else
252 {
253 MessageBox.Show("Error when reading from registry (55222). Value missing. Key: " + regKey + " Value: " + regValue);
254 using (log4net.NDC.Push(string.Format("key={0}", regKey)))
255 {
256 using (log4net.NDC.Push("Value is missing"))
257 {
258 Logging.ATSAdminLog.Error("Error when reading from registry.");
259 }
260 }
261 Application.Exit();
262 return "";
263 }
264 }
265 else
266 {
267 MessageBox.Show("Error when reading from registry (55221). Key missing. Key: " + regKey + " Value: " + strRegTFTP_root);
268 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
269 {
270 using (log4net.NDC.Push("Key is missing"))
271 {
272 Logging.ATSAdminLog.Error("Error when reading from registry.");
273 }
274 }
275 Application.Exit();
276 }
277 return ""; // No value could be retrieved from registry
278 }
279 #endregion
280
281 #region public static int GetRegValueInt(string regKey, string regValue)
282 // Get a registry integer
283 public static int GetRegValueInt(string regKey, string regValue)
284 {
285
286 Microsoft.Win32.RegistryKey objRegkey;
287
288 try
289 {
290 objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey);
291 }
292 catch (ArgumentNullException ex)
293 {
294 MessageBox.Show("Error when reading from registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (22411)");
295 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", ex.Message, System.Environment.NewLine, ex.ToString())))
296 {
297 using (log4net.NDC.Push("Installation info missing."))
298 {
299 Logging.ATSAdminLog.Error("Error when reading from registry.");
300 }
301 }
302 Application.Exit();
303 return 0;
304 }
305 catch (SecurityException e)
306 {
307 MessageBox.Show("Error when reading from registry. You do not have the necessary permission (22412). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
308 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
309 {
310 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
311 {
312 using (log4net.NDC.Push("Invalid user rights."))
313 {
314 Logging.ATSAdminLog.Error("Error when reading from registry.");
315 }
316 }
317 }
318 Application.Exit();
319 return 0;
320 }
321 catch (Exception e)
322 {
323 MessageBox.Show("Error when reading from registry (22413). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
324 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
325 {
326 Logging.ATSAdminLog.Error("Error when reading from registry.");
327 }
328 Application.Exit();
329 return 0;
330 }
331
332 if (objRegkey != null)
333 {
334 if (objRegkey.GetValue(regValue) != null)
335 {
336 try
337 {
338 return (int)objRegkey.GetValue(regValue);
339 }
340 catch (Exception e)
341 {
342 MessageBox.Show("Error when reading from registry (22414). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
343 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
344 {
345 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
346 {
347 Logging.ATSAdminLog.Error("Error when reading from registry.");
348 }
349 }
350 Application.Exit();
351 return 0;
352 }
353 }
354 else
355 {
356 MessageBox.Show("Error when reading from registry (55222). Value missing. Key: " + regKey + " Value: " + regValue);
357
358 using (log4net.NDC.Push(string.Format("key={0}", regKey)))
359 {
360 using (log4net.NDC.Push("Value is missing"))
361 {
362 Logging.ATSAdminLog.Error("Error when reading from registry.");
363 }
364 }
365
366 Application.Exit();
367 return 0;
368 }
369 }
370 else
371 {
372 MessageBox.Show("Error when reading from registry (55221). Key missing. Key: " + regKey + " Value: " + regValue);
373
374 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
375 {
376 using (log4net.NDC.Push("Key is missing"))
377 {
378 Logging.ATSAdminLog.Error("Error when reading from registry.");
379 }
380 }
381
382 Application.Exit();
383 }
384 return 0; // No value could be retrieved from registry
385 }
386 #endregion
387
388 #region public static void SetRegValue(string regKey, string regValue, int value)
389 // Set a registry integer
390 public static void SetRegValue(string regKey, string regValue, int value)
391 {
392 using (log4net.NDC.Push(string.Format("key={0} name={1} data={2}", regKey, regValue, value)))
393 {
394 Logging.ATSAdminLog.DebugFormat(@"Setting value in Registry: {0}\{1}={2}", regKey, regValue, value);
395 Microsoft.Win32.RegistryKey objRegkey;
396
397 try
398 {
399 objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey, true);
400 }
401 catch (ArgumentNullException e)
402 {
403 MessageBox.Show("Error when writing to registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (62411)");
404 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
405 {
406 using (log4net.NDC.Push("Installation info missing"))
407 {
408 Logging.ATSAdminLog.Error("Error when writing to registry.");
409 }
410 }
411 Application.Exit();
412 return;
413 }
414 catch (SecurityException e)
415 {
416 MessageBox.Show("Error when writing to registry. You do not have the necessary permission (62412). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
417 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
418 {
419 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
420 {
421 using (log4net.NDC.Push("Invalid user rights"))
422 {
423 Logging.ATSAdminLog.Error("Error when writing to registry.");
424 }
425 }
426 }
427 Application.Exit();
428 return;
429 }
430 catch (Exception e)
431 {
432 MessageBox.Show("Error when reading from registry (62413). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
433 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
434 {
435 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
436 {
437 Logging.ATSAdminLog.Error("Error when reading from registry.");
438 }
439 }
440 Application.Exit();
441 return;
442 }
443
444 if (objRegkey != null)
445 {
446 try
447 {
448 objRegkey.SetValue(regValue, value);
449 }
450 catch (Exception e)
451 {
452 MessageBox.Show("Error when writing to registry (62414). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
453 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
454 {
455 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
456 {
457 Logging.ATSAdminLog.Error("Error when writing to registry.");
458 }
459 }
460 Application.Exit();
461 }
462 }
463 else
464 {
465 MessageBox.Show("Error when writing to registry (65222). Key missing. Key: " + regKey + " Value: " + regValue);
466
467 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
468 {
469 using (log4net.NDC.Push("Key is missing"))
470 {
471 Logging.ATSAdminLog.Error("Error when writing to registry.");
472 }
473 }
474
475 Application.Exit();
476 }
477 }
478 }
479 #endregion
480
481 #region public static void SetRegValue(string regKey, string regValue, string value)
482 // Set a registry string
483 public static void SetRegValue(string regKey, string regValue, string value)
484 {
485 using (log4net.NDC.Push(string.Format("key={0} name={1} data={2}", regKey, regValue, value)))
486 {
487 Logging.ATSAdminLog.DebugFormat(@"Setting value in Registry: {0}\{1}={2}", regKey, regValue, value);
488 Microsoft.Win32.RegistryKey objRegkey;
489
490 try
491 {
492 objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey, true);
493 }
494 catch (ArgumentNullException e)
495 {
496 MessageBox.Show("Error when writing to registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (62411)");
497 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
498 {
499 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
500 {
501 Logging.ATSAdminLog.Error("Error when writing to registry.");
502 }
503 }
504 Application.Exit();
505 return;
506 }
507 catch (SecurityException e)
508 {
509 MessageBox.Show("Error when writing to registry. You do not have the necessary permission (62412). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
510 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
511 {
512 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
513 {
514 using (log4net.NDC.Push("Invalid user rights"))
515 {
516 Logging.ATSAdminLog.Error("Error when writing to registry.");
517 }
518 }
519 }
520 Application.Exit();
521 return;
522 }
523 catch (Exception e)
524 {
525 MessageBox.Show("Error when reading from registry (62413). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
526 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
527 {
528 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
529 {
530 Logging.ATSAdminLog.Error("Error when reading from registry.");
531 }
532 }
533 Application.Exit();
534 return;
535 }
536
537 if (objRegkey != null)
538 {
539 try
540 {
541 objRegkey.SetValue(regValue, value);
542 }
543 catch (Exception e)
544 {
545 MessageBox.Show("Error when writing to registry (62414). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
546 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
547 {
548 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
549 {
550 Logging.ATSAdminLog.Error("Error when writing to registry.");
551 }
552 }
553 Application.Exit();
554 }
555 }
556 else
557 {
558 MessageBox.Show("Error when writing to registry (65222). Key missing. Key: " + regKey + " Value: " + regValue);
559 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
560 {
561 using (log4net.NDC.Push("Key is missing"))
562 {
563 Logging.ATSAdminLog.Error("Error when writing to registry.");
564 }
565 }
566
567 Application.Exit();
568 }
569 }
570 }
571 #endregion
572
573 #region public static bool RegValueExists(string regKey, string regValue)
574 // Check if a registry int value exists
575 public static bool RegValueExists(string regKey, string regValue)
576 {
577 Microsoft.Win32.RegistryKey objRegkey;
578
579 try
580 {
581 objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey);
582 }
583 catch (ArgumentNullException e)
584 {
585 MessageBox.Show("Error when reading from registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (29411)");
586 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
587 {
588 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
589 {
590 using (log4net.NDC.Push("Installation info missing"))
591 {
592 Logging.ATSAdminLog.Error("Error when reading from registry.");
593 }
594 }
595 }
596 Application.Exit();
597 return false;
598 }
599 catch (SecurityException e)
600 {
601 MessageBox.Show("Error when reading from registry. You do not have the necessary permission (29412). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
602 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
603 {
604 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
605 {
606 using (log4net.NDC.Push("Invalid user rights"))
607 {
608 Logging.ATSAdminLog.Error("Error when reading from registry.");
609 }
610 }
611 }
612 Application.Exit();
613 return false;
614 }
615 catch (Exception e)
616 {
617 MessageBox.Show("Error when reading from registry (29413). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
618 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
619 {
620 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
621 {
622 Logging.ATSAdminLog.Error("Error when reading from registry.");
623 }
624 }
625 Application.Exit();
626 return false;
627 }
628
629 if (objRegkey != null)
630 {
631 if (objRegkey.GetValue(regValue) != null)
632 {
633 return true; // We could retrive the value
634 }
635 else
636 {
637 return false; // No value found
638 }
639 }
640 else
641 {
642 MessageBox.Show("Error when reading from registry (59221). Key missing. Key: " + regKey + " Value: " + strRegTFTP_root);
643
644 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
645 {
646 using (log4net.NDC.Push("Key is missing"))
647 {
648 Logging.ATSAdminLog.Error("Error when reading from registry.");
649 }
650 }
651
652 Application.Exit();
653 }
654 return false; // No value could be retrieved from registry
655 }
656 #endregion
657
658 #region public static string IsValidMAC(string macAddress)
659 // Validate MAC a MAC address.
660 public static string IsValidMAC(string macAddress)
661 {
662 macAddress = macAddress.Replace(":", "");
663 macAddress = macAddress.ToUpper();
664 string result = "";
665 Regex rx = new Regex("([0-9a-fA-F][0-9a-fA-F]-){5}([0-9a-fA-F][0-9a-fA-F])", RegexOptions.IgnoreCase);
666 Match m = rx.Match(macAddress);
667 result = m.Groups[0].Value;
668 if (result.Length == 17)
669 {
670 return result;
671 }
672 else
673 {
674 rx = new Regex("([0-9a-fA-F][0-9a-fA-F]){5}([0-9a-fA-F][0-9a-fA-F])", RegexOptions.IgnoreCase);
675 Match m2 = rx.Match(macAddress);
676 result = m2.Groups[0].Value;
677 if (result.Length == 12)
678 {
679 return result;
680 }
681 return result;
682 }
683 }
684 #endregion
685
686 #region public static string GetMacFromATSname(string name)
687 // Try to extract a mac address from an ATS client name in the format 'ATSxxxxxxxxxxxx', where xxxxxxxxxx is a mac address.
688 public static string GetMacFromATSname(string name)
689 {
690 string strMac;
691 if (name.Length != 15)
692 {
693 return ""; // Not an ATS name
694 }
695 if (!name.StartsWith("ATS"))
696 {
697 return ""; // Not an ATS name
698 }
699 strMac = ATSGlobals.IsValidMAC(name.Substring(3));
700 if (strMac == "")
701 {
702 return ""; // Not an ATS name
703 }
704 // We have a MAC address
705 return strMac;
706 }
707 #endregion
708
709 #region public static void CreateRegistryValues()
710 // Create all needed registry values
711 public static void CreateRegistryValues()
712 {
713 // Create registry values for ATS Admin Application
714 CreateRegistryValue(strATSregRoot, "CheckUpdate", 1);
715 CreateRegistryValue(strATSregRoot, strRegConfigured, 0);
716 CreateRegistryValue(strATSregRoot, ProSupport.strRegAnywhereTSServer, "");
717 CreateRegistryValue(strATSregRoot, ProSupport.strRegDatabaseDir, "");
718 CreateRegistryValue(strATSregRoot, ProSupport.strRegDatabaseServer, "");
719 CreateRegistryValue(strATSregRoot, ProSupport.strRegDestDir, "");
720 CreateRegistryValue(strATSregRoot, ProSupport.strRegDatabaseServer, "");
721 CreateRegistryValue(strATSregRoot, ProSupport.strRegDatabaseInstance, "");
722
723
724 CreateRegistryValue(strATSregRoot, strRegDHCPconfig , 0);
725 CreateRegistryValue(strATSregRoot, strRegManagedMode, 1);
726 CreateRegistryValue(strATSregRoot, strRegTerminalServer, 0);
727 CreateRegistryValue(strATSregRoot, strRegTFTPconfig, 0);
728 CreateRegistryValue(strATSregRoot, strRegTFTP_root,"");
729
730 // Check if the admin app is intalled on this computer and if so, add reg values for TFTP 32
731 if (RegValueExists(strATSregRoot, strRegAdminVersion))
732 { // The admin app is installed, define keys for TFTP32. These should not be installed for the control panel.
733 // Create registry values for TFTPD32
734 CreateRegistryValue(strTFTPD32RegRoot, "BaseDirectory", "");
735 CreateRegistryValue(strTFTPD32RegRoot, "Beep", 0);
736 CreateRegistryValue(strTFTPD32RegRoot, "DirText", 0);
737 CreateRegistryValue(strTFTPD32RegRoot, "Hide", 0);
738 CreateRegistryValue(strTFTPD32RegRoot, "LastWindowPos", "60 49 860 642 ");
739 CreateRegistryValue(strTFTPD32RegRoot, "LocalIP", "");
740 CreateRegistryValue(strTFTPD32RegRoot, "MaxRetransmit", 6);
741 CreateRegistryValue(strTFTPD32RegRoot, "Negociate", 1);
742 CreateRegistryValue(strTFTPD32RegRoot, "PXECompatibility", 0);
743 CreateRegistryValue(strTFTPD32RegRoot, "SaveSyslogFile", "");
744 CreateRegistryValue(strTFTPD32RegRoot, "SecurityLevel", 1);
745 CreateRegistryValue(strTFTPD32RegRoot, "Services", 2);
746 CreateRegistryValue(strTFTPD32RegRoot, "ShowProgressBar", 0);
747 CreateRegistryValue(strTFTPD32RegRoot, "TftpLogFile", "");
748 CreateRegistryValue(strTFTPD32RegRoot, "TftpPort", 69);
749 CreateRegistryValue(strTFTPD32RegRoot, "Timeout", 3);
750 CreateRegistryValue(strTFTPD32RegRoot, "UnixStrings", 1);
751 CreateRegistryValue(strTFTPD32RegRoot, "VirtualRoot", 1);
752 CreateRegistryValue(strTFTPD32RegRoot, "WinSize", 0);
753
754 CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "AddOptionNumber1", 0);
755 CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "AddOptionNumber2", 0);
756 CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "AddOptionValue1", "");
757 CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "AddOptionValue2", "");
758 CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "BootFile", "client.zpxe");
759 CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "DNS", 16834314);
760 CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "DomainName", "");
761 CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "Gateway", 16834314);
762 CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "IP_Pool", 335601418);
763 CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "Mask", 65535);
764 CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "PoolSize", 200);
765 }
766 }
767 #endregion
768
769 #region public static void CreateRegistryValue(string regKey, string regValue, int value)
770 // If a registry key does not exist, create it with the given default value
771 public static void CreateRegistryValue(string regKey, string regValue, int value)
772 {
773 using (log4net.NDC.Push(string.Format("key={0} name={1} data={2}",regKey, regValue, value)))
774 {
775 Logging.ATSAdminLog.DebugFormat(@"Creating value in Registry: {0}\{1}={2}", regKey, regValue, value);
776 Microsoft.Win32.RegistryKey objRegkey;
777
778 try
779 {
780 objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey);
781 }
782 catch (ArgumentNullException e)
783 {
784 MessageBox.Show("Error when reading from registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (92411)");
785 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
786 {
787 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
788 {
789 using (log4net.NDC.Push("Installation info missing"))
790 {
791 Logging.ATSAdminLog.Error("Failed to create value in registry");
792 }
793 }
794 }
795 Application.Exit();
796 return;
797 }
798 catch (SecurityException e)
799 {
800 MessageBox.Show("Error when reading from registry. You do not have the necessary permission (92412). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
801 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
802 {
803 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
804 {
805 using (log4net.NDC.Push("Invalid user rights"))
806 {
807 Logging.ATSAdminLog.Error("Failed to create value in registry");
808 }
809 }
810 }
811 Application.Exit();
812 return;
813 }
814 catch (Exception e)
815 {
816 MessageBox.Show("Error when reading from registry (92413). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
817 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
818 {
819 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
820 {
821 Logging.ATSAdminLog.Error("Failed to create value in registry");
822
823 }
824 }
825 Application.Exit();
826 return;
827 }
828
829 if (objRegkey != null)
830 {
831 if (objRegkey.GetValue(regValue) == null)
832 { // No reg value, create one
833 SetRegValue(regKey, regValue, value);
834 }
835 }
836 else
837 {
838 MessageBox.Show("Error when reading from registry (95221). Key missing. Key: " + regKey + " Value: " + strRegTFTP_root);
839
840 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
841 {
842 Logging.ATSAdminLog.Error("Failed to create value in registry");
843 }
844
845 Application.Exit();
846 }
847 }
848 }
849 #endregion
850
851 #region public static void CreateRegistryValue(string regKey, string regValue, string value)
852 // If a registry key does not exist, create it with the given default value
853 public static void CreateRegistryValue(string regKey, string regValue, string value)
854 {
855 using (log4net.NDC.Push(string.Format("key={0} name={1} data={2}", regKey, regValue, value)))
856 {
857 Logging.ATSAdminLog.DebugFormat(@"Creating value in Registry: {0}\{1}={2}", regKey, regValue, value);
858 Microsoft.Win32.RegistryKey objRegkey;
859
860 try
861 {
862 objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey);
863 }
864 catch (ArgumentNullException e)
865 {
866 MessageBox.Show("Error when reading from registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (92411)");
867 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
868 {
869 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
870 {
871 using (log4net.NDC.Push("Installation info missing"))
872 {
873 Logging.ATSAdminLog.Error("Failed to create value in registry");
874 }
875 }
876 }
877 Application.Exit();
878 return;
879 }
880 catch (SecurityException e)
881 {
882 MessageBox.Show("Error when reading from registry. You do not have the necessary permission (92412). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
883 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
884 {
885 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
886 {
887 using (log4net.NDC.Push("Invalid users rights"))
888 {
889 Logging.ATSAdminLog.Error("Failed to create value in registry");
890 }
891 }
892 }
893 Application.Exit();
894 return;
895 }
896 catch (Exception e)
897 {
898 MessageBox.Show("Error when reading from registry (92413). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
899 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
900 {
901 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
902 {
903 Logging.ATSAdminLog.Error("Failed to create value in registry");
904 }
905 }
906 Application.Exit();
907 return;
908 }
909
910 if (objRegkey != null)
911 {
912 if (objRegkey.GetValue(regValue) == null)
913 { // No reg value, create one
914 SetRegValue(regKey, regValue, value);
915 }
916 }
917 else
918 {
919 MessageBox.Show("Error when reading from registry (95221). Key missing. Key: " + regKey + " Value: " + strRegTFTP_root);
920
921 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
922 {
923 Logging.ATSAdminLog.Error("Failed to create value in registry");
924 }
925
926 Application.Exit();
927 }
928 }
929 }
930 #endregion
931
932 #region public static bool ExtractIntegers(string integers, char separator, out int[] parsed_integers)
933 // Extract two integers from a string, separated by a character
934 // Result: True = extraction ok, false = invalid string.
935 // twointegers: String with two integers, separated by a character
936 // separator: the character separating the strings
937 // value1: (out) The first integer in the string
938 // value2: (out) The second integer in the string
939 public static bool ExtractIntegers(string integers, char separator, out int[] parsed_integers)
940 {
941 string[] parts;
942 parsed_integers = new int[0];
943 parts = integers.Split(separator);
944
945 List<int> t_ints = new List<int>();
946
947 foreach (string t in parts)
948 {
949 t_ints.Add(Convert.ToInt32(t));
950 }
951 parsed_integers = t_ints.ToArray();
952 return true;
953 }
954 #endregion
955 } // Class ATSGlobals
956 } // Namespace Anywhere TS