using System; using System.Collections.Generic; using System.Windows.Forms; using System.Diagnostics; using System.Net.NetworkInformation; using System.Threading; using System.Net.Sockets; using System.IO; using System.Management; namespace DirectToDesktop { static class Program { /// /// The main entry point for the application. /// /// The args. [STAThread] static void Main(string[] args) { if (args.Length < 3 || args.Length > 4) { ShowHelp(); return; } string vpnClientPath = args[0]; string vpnClientArgs = args[1]; string hostName = args[2]; string lookAlivePath = (args.Length > 3) ? args[3] : string.Empty; if (!String.IsNullOrEmpty(lookAlivePath) && File.Exists(lookAlivePath)) { // Only start a new instance of LookAlive for the specified host // if one is not already running. bool lookAliveRunning = false; string wmiQuery = string.Format("select CommandLine from Win32_Process where Name='{0}'", "LookAlive.exe"); ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiQuery); ManagementObjectCollection retObjectCollection = searcher.Get(); foreach (ManagementObject retObject in retObjectCollection) { string commandLine = retObject["CommandLine"] as string; if (commandLine != null) { if (commandLine.ToLower().Contains(hostName.ToLower())) { lookAliveRunning = true; break; } } } if (!lookAliveRunning) { Process.Start(lookAlivePath, hostName); } } if (!HostIsOnline(hostName)) { Process.Start(vpnClientPath, vpnClientArgs); // In case the VPN client is a single-instance process, we // search for any process with a matching name rather than // use the Process.Exited event handler. string vpnProcessName = Path.GetFileNameWithoutExtension(vpnClientPath); // Wait for the host to become available, or the VPN client process to exit. while (!HostIsOnline(hostName) && ProcessIsRunning(vpnProcessName)) { Thread.Sleep(250); } } // If the host is available at this point, start a remote desktop session to it. if (HostIsOnline(hostName)) { Process.Start(Environment.ExpandEnvironmentVariables(@"%windir%\system32\mstsc.exe"), "/v:" + hostName); } } /// /// Checks if a hosts can be pinged. /// /// Name of the host. /// True if the host can be pinged, otherwise false. private static bool HostIsOnline(string hostName) { bool online = false; Ping ping = new Ping(); try { PingReply reply = ping.Send(hostName, 1000); // 1 second timeout online = (reply.Status == IPStatus.Success); } catch (PingException) { // no reply received yet. } return online; } /// /// Returns true if a specified process is currently running. /// /// The name of the process (e.g. 'devenv'). /// True if the process is found to be running, otherwise false. private static bool ProcessIsRunning(string processName) { Process[] processes = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(processName)); return (processes.Length > 0); } /// /// Shows the help. /// private static void ShowHelp() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new FrmAbout()); } } }