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

Annotation of /trunk/TSAdminTool/Globals.cs

Parent Directory Parent Directory | Revision Log Revision Log


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

1 william 4 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 william 47 using log4net;
10 william 4
11     namespace AnywhereTS
12     {
13     public static class ATSGlobals
14     {
15 william 93 #region Global Definitions
16 william 4 // Version
17 william 77 public const string strDatabaseVersion = "1.0.0.4"; // The version string for the ATS database
18 william 81 public const string strDatabaseName = "AnywhereTS"; // THe name of the ATS database
19 william 4 //[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 william 181 static public int terminalServerConfig; // 0=Terminal server on this computer, 1=No terminal server on this computer, 2=other terminal servers.
48 william 4 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 william 93 #endregion
66     #region static ATSGlobals()
67 william 4 // Constructor
68     static ATSGlobals()
69     {
70 william 94 Logging.ATSAdminLog.Debug("Entering ATSGlobals()");
71 william 4 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 william 149 //if (GetATSRegValueInt(strRegRunFirstTime) == 1)
104     //{ // We are runnning this version for the first time, write the needed registry values
105     // CreateRegistryValues();
106     //}
107 william 4
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 william 182 terminalServerConfig = GetATSRegValueInt(strRegTerminalServer);
128 william 4 // Currently only the 3.0 version of the drivers are supported
129     SelectedGraphicsAdaptersFile = GraphicsAdaptersA;
130     SelectedNicAdaptersFile = NicAdaptersA;
131     SelectedSoundAdaptersFile = SoundAdaptersA;
132 william 149
133     ProSupport.strDatabaseServer = GetATSRegValueString(ProSupport.strRegDatabaseServer);
134     ProSupport.strDatabaseInstance = GetATSRegValueString(ProSupport.strRegDatabaseInstance);
135    
136 william 94 Logging.ATSAdminLog.Debug("Leaving ATSGlobals()");
137 william 4
138     }
139 william 93 #endregion
140 william 4
141 william 93 #region public static string GetATSRegValueString(string regValue)
142 william 4 // Get a registry string
143     public static string GetATSRegValueString(string regValue)
144     {
145 william 116 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 william 4 }
154 william 93 #endregion
155 william 4
156 william 93 #region public static int GetATSRegValueInt(string regValue)
157 william 4 // Get a registry integer from the application key
158     public static int GetATSRegValueInt(string regValue)
159     {
160 william 116 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 william 4 }
169 william 93 #endregion
170 william 4
171 william 93 #region public static void SetATSRegValue(string regValue, int value)
172 william 4 // 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 william 93 #endregion
178 william 4
179 william 93 #region public static void SetATSRegValue(string regValue, string value)
180 william 4 // 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 william 93 #endregion
186 william 4
187 william 93 #region public static string GetRegValue(string regKey, string regValue)
188 william 4 // Get a registry string
189     public static string GetRegValue(string regKey, string regValue)
190 william 116 {
191     Microsoft.Win32.RegistryKey objRegkey;
192    
193     try
194 william 46 {
195 william 116 objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey);
196     }
197     catch (ArgumentNullException ex)
198     {
199 william 151 //MessageBox.Show("Error when reading from registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (22411)");
200 william 116 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", ex.Message, System.Environment.NewLine, ex.ToString())))
201 william 46 {
202 william 116 using (log4net.NDC.Push("Installation info missing."))
203     {
204     Logging.ATSAdminLog.Error("Error when reading from registry.");
205     }
206 william 46 }
207 william 116 Application.Exit();
208     return "";
209 william 46 }
210 william 116 catch (SecurityException ex)
211 william 46 {
212 william 151 //MessageBox.Show("Error when reading from registry. You do not have the necessary permission (22412). Key: " + regKey + " Value: " + regValue + " Error details:" + ex.Message);
213 william 116 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", ex.Message, System.Environment.NewLine, ex.ToString())))
214 william 46 {
215 william 116 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
216 william 46 {
217 william 116 using (log4net.NDC.Push("Invalid users rights."))
218     {
219     Logging.ATSAdminLog.Error("Error when reading from registry.");
220     }
221 william 46 }
222     }
223 william 116 Application.Exit();
224     return "";
225 william 46 }
226 william 116 catch (Exception ex)
227 william 46 {
228 william 151 //MessageBox.Show("Error when reading from registry (22413). Key: " + regKey + " Value: " + regValue + " Error details:" + ex.Message);
229 william 116 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 william 46 }
236 william 116
237     if (objRegkey != null)
238 william 4 {
239 william 116 if (objRegkey.GetValue(regValue) != null)
240 william 4 {
241 william 116 try
242     {
243     return objRegkey.GetValue(regValue).ToString();
244     }
245     catch (Exception e)
246     {
247 william 151 //MessageBox.Show("Error when reading from registry (22414). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
248 william 116 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 william 4 }
256 william 116 else
257 william 4 {
258 william 151 //MessageBox.Show("Error when reading from registry (55222). Value missing. Key: " + regKey + " Value: " + regValue);
259 william 116 using (log4net.NDC.Push(string.Format("key={0}", regKey)))
260 william 46 {
261 william 116 using (log4net.NDC.Push("Value is missing"))
262     {
263     Logging.ATSAdminLog.Error("Error when reading from registry.");
264     }
265 william 46 }
266 william 4 Application.Exit();
267     return "";
268     }
269     }
270     else
271     {
272 william 151 //MessageBox.Show("Error when reading from registry (55221). Key missing. Key: " + regKey + " Value: " + strRegTFTP_root);
273 william 116 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
274 william 46 {
275 william 116 using (log4net.NDC.Push("Key is missing"))
276 william 46 {
277     Logging.ATSAdminLog.Error("Error when reading from registry.");
278     }
279     }
280 william 4 Application.Exit();
281     }
282 william 116 return ""; // No value could be retrieved from registry
283 william 4 }
284 william 93 #endregion
285 william 4
286 william 93 #region public static int GetRegValueInt(string regKey, string regValue)
287 william 4 // Get a registry integer
288     public static int GetRegValueInt(string regKey, string regValue)
289     {
290 william 116
291     Microsoft.Win32.RegistryKey objRegkey;
292 william 4
293 william 116 try
294 william 46 {
295 william 116 objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey);
296     }
297     catch (ArgumentNullException ex)
298     {
299 william 151 //MessageBox.Show("Error when reading from registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (22411)");
300 william 116 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", ex.Message, System.Environment.NewLine, ex.ToString())))
301 william 46 {
302 william 116 using (log4net.NDC.Push("Installation info missing."))
303     {
304     Logging.ATSAdminLog.Error("Error when reading from registry.");
305     }
306 william 46 }
307 william 116 Application.Exit();
308     return 0;
309 william 46 }
310 william 116 catch (SecurityException e)
311 william 46 {
312 william 151 //MessageBox.Show("Error when reading from registry. You do not have the necessary permission (22412). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
313 william 116 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
314 william 46 {
315 william 116 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
316 william 46 {
317 william 116 using (log4net.NDC.Push("Invalid user rights."))
318     {
319     Logging.ATSAdminLog.Error("Error when reading from registry.");
320     }
321 william 46 }
322     }
323 william 116 Application.Exit();
324     return 0;
325 william 46 }
326 william 116 catch (Exception e)
327 william 46 {
328 william 151 //MessageBox.Show("Error when reading from registry (22413). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
329 william 116 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 william 46 }
336 william 4
337 william 116 if (objRegkey != null)
338 william 4 {
339 william 116 if (objRegkey.GetValue(regValue) != null)
340 william 4 {
341 william 116 try
342 william 46 {
343 william 116 return (int)objRegkey.GetValue(regValue);
344     }
345     catch (Exception e)
346     {
347 william 151 //MessageBox.Show("Error when reading from registry (22414). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
348 william 116 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
349 william 46 {
350 william 116 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
351     {
352     Logging.ATSAdminLog.Error("Error when reading from registry.");
353     }
354 william 46 }
355 william 116 Application.Exit();
356     return 0;
357 william 46 }
358 william 4 }
359 william 116 else
360     {
361 william 151 //MessageBox.Show("Error when reading from registry (55222). Value missing. Key: " + regKey + " Value: " + regValue);
362 william 116
363 william 93 using (log4net.NDC.Push(string.Format("key={0}", regKey)))
364 william 46 {
365     using (log4net.NDC.Push("Value is missing"))
366     {
367     Logging.ATSAdminLog.Error("Error when reading from registry.");
368     }
369     }
370 william 116
371     Application.Exit();
372     return 0;
373     }
374 william 4 }
375 william 116 else
376     {
377 william 151 //MessageBox.Show("Error when reading from registry (55221). Key missing. Key: " + regKey + " Value: " + regValue);
378 william 116
379 william 46 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 william 116
387     Application.Exit();
388     }
389     return 0; // No value could be retrieved from registry
390 william 4 }
391 william 93 #endregion
392 william 4
393 william 93 #region public static void SetRegValue(string regKey, string regValue, int value)
394 william 4 // Set a registry integer
395     public static void SetRegValue(string regKey, string regValue, int value)
396     {
397 william 116 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 william 4
402 william 116 try
403 william 46 {
404 william 116 objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey, true);
405 william 46 }
406 william 116 catch (ArgumentNullException e)
407 william 46 {
408 william 116 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 william 46 {
411 william 116 using (log4net.NDC.Push("Installation info missing"))
412 william 46 {
413     Logging.ATSAdminLog.Error("Error when writing to registry.");
414     }
415     }
416 william 116 Application.Exit();
417     return;
418 william 46 }
419 william 116 catch (SecurityException e)
420 william 46 {
421 william 116 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 william 46 {
424 william 116 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 william 46 }
432 william 116 Application.Exit();
433     return;
434 william 46 }
435 william 4 catch (Exception e)
436     {
437 william 116 MessageBox.Show("Error when reading from registry (62413). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
438 william 46 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 william 116 Logging.ATSAdminLog.Error("Error when reading from registry.");
443 william 46 }
444     }
445 william 4 Application.Exit();
446 william 116 return;
447 william 4 }
448 william 116
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 william 46 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 william 116 }
478 william 46 }
479 william 116
480     Application.Exit();
481     }
482 william 4 }
483     }
484 william 93 #endregion
485 william 4
486 william 93 #region public static void SetRegValue(string regKey, string regValue, string value)
487 william 4 // Set a registry string
488     public static void SetRegValue(string regKey, string regValue, string value)
489     {
490 william 116 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 william 4
495 william 116 try
496 william 46 {
497 william 116 objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey, true);
498 william 46 }
499 william 116 catch (ArgumentNullException e)
500 william 46 {
501 william 116 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 william 46 {
504 william 116 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
505 william 46 {
506     Logging.ATSAdminLog.Error("Error when writing to registry.");
507     }
508     }
509 william 116 Application.Exit();
510     return;
511 william 46 }
512 william 116 catch (SecurityException e)
513 william 46 {
514 william 116 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 william 46 {
519 william 116 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 william 46 }
528 william 4 catch (Exception e)
529     {
530 william 116 MessageBox.Show("Error when reading from registry (62413). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
531 william 46 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 william 116 Logging.ATSAdminLog.Error("Error when reading from registry.");
536 william 46 }
537     }
538 william 4 Application.Exit();
539 william 116 return;
540 william 4 }
541 william 116
542     if (objRegkey != null)
543     {
544     try
545 william 46 {
546 william 116 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 william 46 using (log4net.NDC.Push("Key is missing"))
567     {
568     Logging.ATSAdminLog.Error("Error when writing to registry.");
569     }
570     }
571 william 116
572     Application.Exit();
573     }
574 william 4 }
575     }
576 william 93 #endregion
577 william 4
578 william 93 #region public static bool RegValueExists(string regKey, string regValue)
579 william 4 // 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 william 47 catch (ArgumentNullException e)
589 william 4 {
590 william 151 //MessageBox.Show("Error when reading from registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (29411)");
591 william 46 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 william 4 Application.Exit();
602     return false;
603     }
604     catch (SecurityException e)
605     {
606 william 151 //MessageBox.Show("Error when reading from registry. You do not have the necessary permission (29412). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
607 william 46 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 william 4 Application.Exit();
618     return false;
619     }
620     catch (Exception e)
621     {
622 william 151 //MessageBox.Show("Error when reading from registry (29413). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
623 william 46 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 william 4 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 william 151 //MessageBox.Show("Error when reading from registry (59221). Key missing. Key: " + regKey + " Value: " + strRegTFTP_root);
648 william 47
649 william 46 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 william 47
657 william 4 Application.Exit();
658     }
659     return false; // No value could be retrieved from registry
660     }
661 william 93 #endregion
662 william 4
663 william 93 #region public static string IsValidMAC(string macAddress)
664 william 4 // 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 william 93 #endregion
690 william 4
691 william 93 #region public static string GetMacFromATSname(string name)
692 william 4 // 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 william 93 #endregion
713    
714     #region public static void CreateRegistryValues()
715 william 4 // 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 william 165 CreateRegistryValue(strATSregRoot, ProSupport.strRegAnywhereTSServer, "");
722     #if HAVE_FRMCONFIGMODE
723     CreateRegistryValue(strATSregRoot, ProSupport.strRegDatabaseDir, "");
724 william 4 CreateRegistryValue(strATSregRoot, ProSupport.strRegDestDir, "");
725 william 165 #endif
726 william 149 CreateRegistryValue(strATSregRoot, ProSupport.strRegDatabaseServer, ProSupport.strDatabaseServer);
727     CreateRegistryValue(strATSregRoot, ProSupport.strRegDatabaseInstance, ProSupport.strDatabaseInstance);
728 william 98
729    
730 william 4 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 william 93 }
772 william 4 }
773 william 93 #endregion
774 william 4
775 william 93 #region public static void CreateRegistryValue(string regKey, string regValue, int value)
776 william 4 // If a registry key does not exist, create it with the given default value
777 william 93 public static void CreateRegistryValue(string regKey, string regValue, int value)
778 william 4 {
779 william 93 using (log4net.NDC.Push(string.Format("key={0} name={1} data={2}",regKey, regValue, value)))
780 william 4 {
781 william 116 Logging.ATSAdminLog.DebugFormat(@"Creating value in Registry: {0}\{1}={2}", regKey, regValue, value);
782 william 93 Microsoft.Win32.RegistryKey objRegkey;
783    
784     try
785 william 46 {
786 william 93 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 william 46 {
793 william 93 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
794 william 46 {
795 william 93 using (log4net.NDC.Push("Installation info missing"))
796     {
797     Logging.ATSAdminLog.Error("Failed to create value in registry");
798     }
799 william 46 }
800     }
801 william 93 Application.Exit();
802     return;
803 william 46 }
804 william 93 catch (SecurityException e)
805 william 46 {
806 william 93 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 william 46 {
809 william 93 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
810 william 46 {
811 william 93 using (log4net.NDC.Push("Invalid user rights"))
812     {
813     Logging.ATSAdminLog.Error("Failed to create value in registry");
814     }
815 william 46 }
816     }
817 william 93 Application.Exit();
818     return;
819 william 46 }
820 william 93 catch (Exception e)
821 william 46 {
822 william 93 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 william 46 {
825 william 93 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 william 46 }
831 william 93 Application.Exit();
832     return;
833 william 46 }
834 william 93
835     if (objRegkey != null)
836     {
837     if (objRegkey.GetValue(regValue) == null)
838     { // No reg value, create one
839     SetRegValue(regKey, regValue, value);
840     }
841 william 4 }
842 william 93 else
843     {
844     MessageBox.Show("Error when reading from registry (95221). Key missing. Key: " + regKey + " Value: " + strRegTFTP_root);
845    
846 william 46 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
847     {
848 william 93 Logging.ATSAdminLog.Error("Failed to create value in registry");
849 william 46 }
850 william 93
851     Application.Exit();
852     }
853 william 4 }
854     }
855 william 93 #endregion
856 william 4
857 william 93 #region public static void CreateRegistryValue(string regKey, string regValue, string value)
858 william 4 // 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 william 93 using (log4net.NDC.Push(string.Format("key={0} name={1} data={2}", regKey, regValue, value)))
862     {
863 william 116 Logging.ATSAdminLog.DebugFormat(@"Creating value in Registry: {0}\{1}={2}", regKey, regValue, value);
864 william 93 Microsoft.Win32.RegistryKey objRegkey;
865 william 4
866 william 93 try
867 william 46 {
868 william 93 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 william 46 {
875 william 93 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
876 william 46 {
877 william 93 using (log4net.NDC.Push("Installation info missing"))
878     {
879     Logging.ATSAdminLog.Error("Failed to create value in registry");
880     }
881 william 46 }
882     }
883 william 93 Application.Exit();
884     return;
885 william 46 }
886 william 93 catch (SecurityException e)
887 william 46 {
888 william 93 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 william 46 {
891 william 93 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
892 william 46 {
893 william 93 using (log4net.NDC.Push("Invalid users rights"))
894     {
895     Logging.ATSAdminLog.Error("Failed to create value in registry");
896     }
897 william 46 }
898     }
899 william 93 Application.Exit();
900     return;
901 william 46 }
902 william 93 catch (Exception e)
903 william 46 {
904 william 93 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 william 46 {
907 william 93 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 william 46 }
912 william 93 Application.Exit();
913     return;
914 william 46 }
915 william 4
916 william 93 if (objRegkey != null)
917     {
918     if (objRegkey.GetValue(regValue) == null)
919     { // No reg value, create one
920     SetRegValue(regKey, regValue, value);
921     }
922 william 4 }
923 william 93 else
924     {
925     MessageBox.Show("Error when reading from registry (95221). Key missing. Key: " + regKey + " Value: " + strRegTFTP_root);
926    
927 william 46 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
928     {
929 william 93 Logging.ATSAdminLog.Error("Failed to create value in registry");
930 william 46 }
931 william 93
932     Application.Exit();
933     }
934 william 4 }
935     }
936 william 93 #endregion
937 william 4
938 william 93 #region public static bool ExtractIntegers(string integers, char separator, out int[] parsed_integers)
939 william 4 // 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 william 93 public static bool ExtractIntegers(string integers, char separator, out int[] parsed_integers)
946 william 4 {
947     string[] parts;
948 william 93 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 william 4 {
955 william 93 t_ints.Add(Convert.ToInt32(t));
956 william 4 }
957 william 93 parsed_integers = t_ints.ToArray();
958 william 4 return true;
959     }
960 william 93 #endregion
961 william 4 } // Class ATSGlobals
962     } // Namespace Anywhere TS

  ViewVC Help
Powered by ViewVC 1.1.22