/// <summary> /// Obtain the information about the KWM instance that is related to /// our instance, if any. /// </summary> public void FindOtherProcess() { State = WmOtherProcessState.None; OtherProcess = null; OtherWindowHandle = IntPtr.Zero; Process currentProcess = Process.GetCurrentProcess(); String procName = currentProcess.ProcessName; WindowsIdentity userWI = WindowsIdentity.GetCurrent(); // Try to find a matching process. foreach (Process p in Process.GetProcessesByName(procName)) { String stringSID = KSyscalls.GetProcessSid(p); // Ignore our current process. if (p.Id == currentProcess.Id) { continue; } bool InOurSessionFlag = currentProcess.SessionId == p.SessionId; // This process has been started by the current user. if (String.Compare(stringSID, userWI.User.Value, true) == 0) { if (InOurSessionFlag) { State = WmOtherProcessState.OurInCurrentSession; } else { State = WmOtherProcessState.OurInOtherSession; } OtherProcess = p; break; } if (InOurSessionFlag) { State = WmOtherProcessState.NotOurInCurrentSession; OtherProcess = p; break; } } // Process found. Get the main window handle. if (OtherProcess != null) { OtherWindowHandle = Program.GetOtherKwmHandle(); } }
/// <summary> /// Initialize the application on startup. /// </summary> private static void AppInit() { // Somehow this call doesn't make the output visible in cygwin // bash. It works for cmd.exe. KSyscalls.AttachConsole(KSyscalls.ATTACH_PARENT_PROCESS); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); MsgWindow = new WmMsgWindow(); KBase.InvokeUiControl = new Control(); KBase.InvokeUiControl.CreateControl(); KBase.HandleErrorCallback = WmUi.HandleError; Application.ThreadException += HandleUnhandledException; KwmCfg.Cur = KwmCfg.Spawn(); KLogging.Logger = KwmLogger.Logger; KwmLogger.SetLoggingLevel(KwmCfg.Cur.KwmDebuggingFlag ? KwmLoggingLevel.Normal : KwmLoggingLevel.Debug); }
/// <summary> /// Start the main process. /// </summary> private void StartMainProcess() { Status = VncSessionStatus.MainProcess; // Handle server session miscellaneous actions. if (ServerSessionFlag) { // Set the support mode. SetSupportSessionMode(SupportSessionFlag); // If a window is being shared (not the desktop), set it // visible and in foreground. if (WindowHandle != 0) { IntPtr hWnd = new IntPtr(WindowHandle); if (KSyscalls.IsIconic(hWnd)) { KSyscalls.ShowWindowAsync(hWnd, (int)KSyscalls.WindowStatus.SW_RESTORE); } KSyscalls.SetForegroundWindow(hWnd); } } // Remove any indication of previous server's listening port. RegistryKey key = null; try { key = KwmReg.GetKwmCURegKey(); key.DeleteValue(m_portRegItem, false); key.DeleteValue(m_portRegItemWritten, false); } finally { if (key != null) { key.Close(); } } // Start the process. StartProcess(true); }
/// <summary> /// Send the message specified to the other KWM specified, if possible. /// </summary> private static void SendMsgToOtherKwm(WmOtherProcess other, UInt32 msgID, String path) { try { // Wait 10 seconds for the other process to finish initializing, // then send the message. if (other.OtherProcess != null && other.OtherWindowHandle != IntPtr.Zero && other.OtherProcess.WaitForInputIdle(10 * 1000)) { KSyscalls.COPYDATASTRUCT cds; cds.dwData = new IntPtr(msgID); cds.lpData = path; cds.cbData = Encoding.Default.GetBytes(path).Length + 1; KSyscalls.SendMessage(other.OtherWindowHandle.ToInt32(), KSyscalls.WM_COPYDATA, 0, ref cds); } } catch (Exception) { } }
/// <summary> /// This method is called when a timer event has been received to check /// if the process has started. /// </summary> private void OnProcessPollEvent(Object[] args) { if (Status != VncSessionStatus.MainProcess && Status != VncSessionStatus.DummyProcess) { return; } try { bool foundFlag = false; // Check for window handle. if (ServerSessionFlag && Status == VncSessionStatus.MainProcess) { IntPtr serverHandle = KSyscalls.FindWindow("WinVNC Tray Icon", 0); foundFlag = (serverHandle != IntPtr.Zero); } // Check the registry value. Get the port. else { RegistryKey key = KwmReg.GetKwmCURegKey(); try { foundFlag = (key.GetValue(m_portRegItemWritten) != null); if (foundFlag) { ProcessPort = (int)key.GetValue(m_portRegItem); } } finally { if (key != null) { key.Close(); } } } // Pass to the next step. if (foundFlag) { HandleNextSessionStep(); } // Poll again later, if possible. else { NbTimerEvent++; if (NbTimerEvent >= 30) { throw new Exception("VNC process does not start"); } Timer.WakeMeUp(200); } } catch (Exception ex) { HandleSessionTrouble(ex); } }