Пример #1
0
		public VirtualComPort(RegistryKey rootKey, string dll, string friendlyName, string portName)
		{
			string prefix;
			int index;

			// Extract the prefix and index from the port name.
			m_PortName = portName;
			if (m_PortName != "")
			{
				Match match = portNameRegex.Match(m_PortName);
				if (! match.Success)
					throw new Exception("Invalid virtual COM port name: " + portName);
				prefix = match.Groups[1].Value;
				index = int.Parse(match.Groups[2].Value);
			}
			else
			{
				prefix = "COM";
				index = -1;
			}

			// Create a uniquely named subkey under the root key for this port.
			string[] subKeyNames = rootKey.GetSubKeyNames();
			int lastSubKeyIndex = -1;
			foreach (string keyName in subKeyNames)
			{
				int val;
				try
				{
					val = int.Parse(keyName);
				}
				catch
				{
					val = -1;
				}
				lastSubKeyIndex = val > lastSubKeyIndex ? val : lastSubKeyIndex;
			}
			string subKeyName = ((int)(lastSubKeyIndex + 1)).ToString();
			RegistryKey subKey = rootKey.CreateSubKey(subKeyName);
			try
			{
				// Populate the subkey.
				subKey.SetValue("Dll", dll, RegistryValueKind.String);
				subKey.SetValue("Prefix", prefix, RegistryValueKind.String);
				subKey.SetValue("FriendlyName", friendlyName +
					(m_PortName == "" ? "" : " on " + m_PortName), RegistryValueKind.String);
				if (index >= 0)
					subKey.SetValue("Index", index, RegistryValueKind.DWord);
				subKey.Flush();

				// Attempt to load the driver, remembering to strip the "HKEY_LOCAL_MACHINE\" prefix
				// from the sub key name.
				DeviceHandle = ActivateDevice(subKey.Name.Remove(0, subKey.Name.IndexOf(@"\") + 1), 0);
				if (DeviceHandle != IntPtr.Zero)
				{
					try 
					{
						// The driver loaded successfully. Retrieve the port name from the Active drivers
						// subkey if we don't have it already.
						if (m_PortName == "")
						{
							RegistryKey activeDrivers = Registry.LocalMachine.OpenSubKey(@"Drivers\Active", false);
							try
							{
								string[] driverSubKeyNames = activeDrivers.GetSubKeyNames();
								foreach (string keyName in driverSubKeyNames)
								{
									RegistryKey driverKey = activeDrivers.OpenSubKey(keyName, false);
									try
									{
										object HndValue = driverKey.GetValue("Hnd");
										object NameValue = driverKey.GetValue("Name");
										if ((HndValue != null) && (HndValue.ToString() == DeviceHandle.ToString()) &&
											(NameValue != null))
										{
											m_PortName = NameValue.ToString();
											break;
										}
									}
									finally
									{
										driverKey.Dispose();
									}
								}
							}
							finally
							{
								activeDrivers.Dispose();
							}

							// Verify the port name and update the friendly name if we can.
							if (m_PortName != "")
							{
								Match match = portNameRegex.Match(m_PortName);
								if (! match.Success)
									throw new Exception("Invalid virtual COM port name: " + portName);
								subKey.SetValue("FriendlyName", friendlyName + " on " + m_PortName,
									RegistryValueKind.String);
								subKey.Flush();
							}
							else
							{
								throw new Exception("Unable to locate active device driver details for device handle " + DeviceHandle.ToString());
							}
						}
					}
					catch
					{
						DeactivateDevice(DeviceHandle);
						DeviceHandle = IntPtr.Zero;
						throw;
					}
				}
			}
			finally
			{
				subKey.Dispose();
				if (DeviceHandle == IntPtr.Zero)
				{
					rootKey.DeleteSubKey(subKeyName);
					rootKey.Flush();
				}
			}
		}
Пример #2
0
		public FormMain()
		{
			// Set the resize event handler.
			resizeEventHandler = new EventHandler(FormMain_Resize);
			Resize += resizeEventHandler;

			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			// Get the window handle.
			Capture = true;
			hwnd = OpenNETCF.Win32.Win32Window.GetCapture();
			Capture = false;

			// Add a message filter.
			ApplicationEx.AddMessageFilter(this);

			// Load the configuration.
			executablePath = ApplicationEx.StartupPath;
			extensionPath = Path.Combine(executablePath, "Extensions");
			string iniFileName = Path.Combine(executablePath, "GPSProxy.ini");
			if (! File.Exists(iniFileName))
			{
				try
				{
					File.Copy(Path.Combine(executablePath, "GPSProxy.ini.default"), iniFileName, false);
				}
				catch
				{
				}
			}
			if (! File.Exists(iniFileName))
				using (File.Create(iniFileName))
				{
				}
			configSource = new IniConfigSource(iniFileName);
			configSource.Alias.AddAlias("On", true);
			configSource.Alias.AddAlias("Off", false);
			configSource.Alias.AddAlias("True", true);
			configSource.Alias.AddAlias("False", false);
			if (configSource.Configs["Settings"] == null)
				configSource.AddConfig("Settings");

			configSettings = configSource.Configs["Settings"];
			settings["autoStart"].Value = configSettings.GetBoolean("Start Proxy Automatically on Launch", (bool)settings["autoStart"].Value);
			settings["runExternalApp"].Value = configSettings.GetBoolean("Run External App after Proxy Startup", (bool)settings["runExternalApp"].Value);
			settings["externalApp"].Value = configSettings.GetString("External Application", (string)settings["externalApp"].Value);
			settings["autoReconnect"].Value = configSettings.GetBoolean("Reconnect after Power-on", (bool)settings["autoReconnect"].Value);
			settings["reconnectDelay"].Value = configSettings.GetInt("Power-on Reconnect Delay", (int)settings["reconnectDelay"].Value);

			// Create the main and page menu panels.
			mainMenuPanel = new ScrollablePanel();
			mainMenuPanel.Visible = false;
			Controls.Add(mainMenuPanel);
			mainMenuPanel.Size = pluginPanel.ClientSize;
			mainMenuPanel.BringToFront();

			pageMenuPanel = new ScrollablePanel();
			pageMenuPanel.Visible = false;
			Controls.Add(pageMenuPanel);
			pageMenuPanel.Size = pluginPanel.ClientSize;
			pageMenuPanel.BringToFront();

			// Add the main menu buttons.
			AboutButton = AddMenuButton(mainMenuPanel, "About...",
				new EventHandler(AboutButton_Click));
			SettingsButton = AddMenuButton(mainMenuPanel, "Settings...",
				new EventHandler(SettingsButton_Click), true);
			ExtensionsButton = AddMenuButton(mainMenuPanel, "Extensions...",
				new EventHandler(ExtensionsButton_Click));
			VirtualComPortsButton = AddMenuButton(mainMenuPanel, "Virtual COM Ports...",
				new EventHandler(VirtualComPortsButton_Click));
			ExitButton = AddMenuButton(mainMenuPanel, "Exit",
				new EventHandler(ExitButton_Click), true);

			// Load the extension DLLs.
			LoadExtensions();

			// Activate extensions according to the configuration file.
			ActivateExtensions();

			// NOTE: do not attempt to show an message boxes before this point, as this triggers
			// other form events that expect the activeExtensions array to have been created.

			// Load the virtual COM port drivers.
			virtualComPortRootKey = Registry.LocalMachine.CreateSubKey(@"Drivers\GPSProxyVirtualCOMPorts");
			if (virtualComPortRootKey == null)
			{
				MessageBox.Show(@"Unable to open/create registry key: " + virtualComPortRootKey.Name,
					"Error", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
			}
			else
			{
				LoadVirtualComPortDrivers();
			}

			// Create and populate the extensions dialog.
			extensionsDialog = new FormExtensions();
			extensionsDialog.ComboBoxProvider.SelectedIndexChanged += new EventHandler(ComboBoxProvider_SelectedIndexChanged);
			extensionsDialog.ListBoxExtensions.SelectedIndexChanged += new EventHandler(ListBoxExtensions_SelectedIndexChanged);
			extensionsDialog.ButtonConfigureProvider.Click += new EventHandler(ButtonConfigureProvider_Click);
			extensionsDialog.ButtonAddExtension.Click += new EventHandler(ButtonAddExtension_Click);
			extensionsDialog.ButtonRemoveExtension.Click += new EventHandler(ButtonRemoveExtension_Click);
			extensionsDialog.ButtonConfigureExtension.Click += new EventHandler(ButtonConfigureExtension_Click);
			RepopulateExtensionsDialog();

			// Create and populate the virtual COM port dialog.
			virtualComPortsDialog = new FormVirtualComPorts();
			virtualComPortsDialog.VirtualComPortListBox.SelectedIndexChanged += new EventHandler(VirtualComPortListBox_SelectedIndexChanged);
			virtualComPortsDialog.ButtonAdd.Click += new EventHandler(ButtonAddVirtualComPort_Click);
			virtualComPortsDialog.ButtonRemove.Click += new EventHandler(ButtonRemoveVirtualComPort_Click);
			RepopulateVirtualComPortsDialog();
		}
Пример #3
0
		public VirtualComPort(RegistryKey rootKey, string dll, string friendlyName)
			: this(rootKey, dll, friendlyName, "")
		{
		}