Example #1
0
        private static void FixClipThread()
        {
            try
            {
#if !KeePassUAP
                const string      strXSel = "xsel";
                const AppRunFlags rfW     = AppRunFlags.WaitForExit;

                string strLast = null;
                while (true)
                {
                    string str = NativeLib.RunConsoleApp(strXSel,
                                                         "--output --clipboard");
                    if (str == null)
                    {
                        return;                                 // 'xsel' not installed
                    }
                    if (str != strLast)
                    {
                        if (NeedClipboardWorkaround())
                        {
                            NativeLib.RunConsoleApp(strXSel,
                                                    "--input --clipboard", str, rfW);
                        }

                        strLast = str;
                    }

                    Thread.Sleep(250);
                }
#endif
            }
            catch (ThreadAbortException)
            {
                try { Thread.ResetAbort(); }
                catch (Exception) { Debug.Assert(false); }
            }
            catch (Exception) { Debug.Assert(false); }
            finally { m_thFixClip = null; }
        }
Example #2
0
        public static string RunConsoleApp(string strAppPath, string strParams,
			string strStdInput, AppRunFlags f)
        {
            if(strAppPath == null) throw new ArgumentNullException("strAppPath");
            if(strAppPath.Length == 0) throw new ArgumentException("strAppPath");

            bool bStdOut = ((f & AppRunFlags.GetStdOutput) != AppRunFlags.None);

            RunProcessDelegate fnRun = delegate()
            {
                try
                {
                    ProcessStartInfo psi = new ProcessStartInfo();

                    psi.CreateNoWindow = true;
                    psi.FileName = strAppPath;
                    psi.WindowStyle = ProcessWindowStyle.Hidden;
                    psi.UseShellExecute = false;
                    psi.RedirectStandardOutput = bStdOut;

                    if(strStdInput != null) psi.RedirectStandardInput = true;

                    if(!string.IsNullOrEmpty(strParams)) psi.Arguments = strParams;

                    Process p = Process.Start(psi);

                    if(strStdInput != null)
                    {
                        EnsureNoBom(p.StandardInput);

                        p.StandardInput.Write(strStdInput);
                        p.StandardInput.Close();
                    }

                    string strOutput = string.Empty;
                    if(bStdOut) strOutput = p.StandardOutput.ReadToEnd();

                    if((f & AppRunFlags.WaitForExit) != AppRunFlags.None)
                        p.WaitForExit();
                    else if((f & AppRunFlags.GCKeepAlive) != AppRunFlags.None)
                    {
                        Thread th = new Thread(delegate()
                        {
                            try { p.WaitForExit(); }
                            catch(Exception) { Debug.Assert(false); }
                        });
                        th.Start();
                    }

                    return strOutput;
                }
                catch(Exception) { Debug.Assert(false); }

                return null;
            };

            if((f & AppRunFlags.DoEvents) != AppRunFlags.None)
            {
                List<Form> lDisabledForms = new List<Form>();
                if((f & AppRunFlags.DisableForms) != AppRunFlags.None)
                {
                    foreach(Form form in Application.OpenForms)
                    {
                        if(!form.Enabled) continue;

                        lDisabledForms.Add(form);
                        form.Enabled = false;
                    }
                }

                IAsyncResult ar = fnRun.BeginInvoke(null, null);

                while(!ar.AsyncWaitHandle.WaitOne(0))
                {
                    Application.DoEvents();
                    Thread.Sleep(2);
                }

                string strRet = fnRun.EndInvoke(ar);

                for(int i = lDisabledForms.Count - 1; i >= 0; --i)
                    lDisabledForms[i].Enabled = true;

                return strRet;
            }

            return fnRun();
        }
Example #3
0
        public static string RunConsoleApp(string strAppPath, string strParams,
                                           string strStdInput, AppRunFlags f)
        {
            if (strAppPath == null)
            {
                throw new ArgumentNullException("strAppPath");
            }
            if (strAppPath.Length == 0)
            {
                throw new ArgumentException("strAppPath");
            }

            bool bStdOut = ((f & AppRunFlags.GetStdOutput) != AppRunFlags.None);

            RunProcessDelegate fnRun = delegate()
            {
                try
                {
                    ProcessStartInfo psi = new ProcessStartInfo();

                    psi.CreateNoWindow         = true;
                    psi.FileName               = strAppPath;
                    psi.WindowStyle            = ProcessWindowStyle.Hidden;
                    psi.UseShellExecute        = false;
                    psi.RedirectStandardOutput = bStdOut;

                    if (strStdInput != null)
                    {
                        psi.RedirectStandardInput = true;
                    }

                    if (!string.IsNullOrEmpty(strParams))
                    {
                        psi.Arguments = strParams;
                    }

                    Process p = Process.Start(psi);

                    if (strStdInput != null)
                    {
                        // Workaround for Mono Process StdIn BOM bug;
                        // https://sourceforge.net/p/keepass/bugs/1219/
                        EnsureNoBom(p.StandardInput);

                        p.StandardInput.Write(strStdInput);
                        p.StandardInput.Close();
                    }

                    string strOutput = string.Empty;
                    if (bStdOut)
                    {
                        strOutput = p.StandardOutput.ReadToEnd();
                    }

                    if ((f & AppRunFlags.WaitForExit) != AppRunFlags.None)
                    {
                        p.WaitForExit();
                    }
                    else if ((f & AppRunFlags.GCKeepAlive) != AppRunFlags.None)
                    {
                        Thread th = new Thread(delegate()
                        {
                            try { p.WaitForExit(); }
                            catch (Exception) { Debug.Assert(false); }
                        });
                        th.Start();
                    }

                    return(strOutput);
                }
                catch (Exception) { Debug.Assert(false); }

                return(null);
            };

            if ((f & AppRunFlags.DoEvents) != AppRunFlags.None)
            {
                List <Form> lDisabledForms = new List <Form>();
                if ((f & AppRunFlags.DisableForms) != AppRunFlags.None)
                {
                    foreach (Form form in Application.OpenForms)
                    {
                        if (!form.Enabled)
                        {
                            continue;
                        }

                        lDisabledForms.Add(form);
                        form.Enabled = false;
                    }
                }

                IAsyncResult ar = fnRun.BeginInvoke(null, null);

                while (!ar.AsyncWaitHandle.WaitOne(0))
                {
                    Application.DoEvents();
                    Thread.Sleep(2);
                }

                string strRet = fnRun.EndInvoke(ar);

                for (int i = lDisabledForms.Count - 1; i >= 0; --i)
                {
                    lDisabledForms[i].Enabled = true;
                }

                return(strRet);
            }

            return(fnRun());
        }
Example #4
0
        public static string RunConsoleApp(string strAppPath, string strParams,
                                           string strStdInput, AppRunFlags f)
        {
            if (strAppPath == null)
            {
                throw new ArgumentNullException("strAppPath");
            }
            if (strAppPath.Length == 0)
            {
                throw new ArgumentException("strAppPath");
            }

            bool bStdOut = ((f & AppRunFlags.GetStdOutput) != AppRunFlags.None);

            RunProcessDelegate fnRun = delegate()
            {
                Process pToDispose = null;
                try
                {
                    ProcessStartInfo psi = new ProcessStartInfo();

                    psi.FileName = strAppPath;
                    if (!string.IsNullOrEmpty(strParams))
                    {
                        psi.Arguments = strParams;
                    }

                    psi.CreateNoWindow  = true;
                    psi.WindowStyle     = ProcessWindowStyle.Hidden;
                    psi.UseShellExecute = false;

                    psi.RedirectStandardOutput = bStdOut;
                    if (strStdInput != null)
                    {
                        psi.RedirectStandardInput = true;
                    }

                    Process p = StartProcessEx(psi);
                    pToDispose = p;

                    if (strStdInput != null)
                    {
                        EnsureNoBom(p.StandardInput);

                        p.StandardInput.Write(strStdInput);
                        p.StandardInput.Close();
                    }

                    string strOutput = string.Empty;
                    if (bStdOut)
                    {
                        strOutput = p.StandardOutput.ReadToEnd();
                    }

                    if ((f & AppRunFlags.WaitForExit) != AppRunFlags.None)
                    {
                        p.WaitForExit();
                    }
                    else if ((f & AppRunFlags.GCKeepAlive) != AppRunFlags.None)
                    {
                        pToDispose = null;                         // Thread disposes it

                        Thread th = new Thread(delegate()
                        {
                            try { p.WaitForExit(); p.Dispose(); }
                            catch (Exception) { Debug.Assert(false); }
                        });
                        th.Start();
                    }

                    return(strOutput);
                }
#if DEBUG
                catch (ThreadAbortException) { }
                catch (Win32Exception exW)
                {
                    Debug.Assert((strAppPath == ClipboardU.XSel) &&
                                 (exW.NativeErrorCode == 2));                // XSel not found
                }
                catch (Exception) { Debug.Assert(false); }
#else
                catch (Exception) { }
#endif
                finally
                {
                    try { if (pToDispose != null)
                          {
                              pToDispose.Dispose();
                          }
                    }
                    catch (Exception) { Debug.Assert(false); }
                }

                return(null);
            };

            if ((f & AppRunFlags.DoEvents) != AppRunFlags.None)
            {
                List <Form> lDisabledForms = new List <Form>();
                if ((f & AppRunFlags.DisableForms) != AppRunFlags.None)
                {
                    foreach (Form form in Application.OpenForms)
                    {
                        if (!form.Enabled)
                        {
                            continue;
                        }

                        lDisabledForms.Add(form);
                        form.Enabled = false;
                    }
                }

                IAsyncResult ar = fnRun.BeginInvoke(null, null);

                while (!ar.AsyncWaitHandle.WaitOne(0))
                {
                    Application.DoEvents();
                    Thread.Sleep(2);
                }

                string strRet = fnRun.EndInvoke(ar);

                for (int i = lDisabledForms.Count - 1; i >= 0; --i)
                {
                    lDisabledForms[i].Enabled = true;
                }

                return(strRet);
            }

            return(fnRun());
        }
Example #5
0
        private static void FixClipThread()
        {
            try
            {
#if !KeePassUAP
                const string      strXSel  = "xsel";
                const string      strXSelR = "--output --clipboard";
                const string      strXSelW = "--input --clipboard --nodetach";
                const AppRunFlags rfW      = AppRunFlags.WaitForExit;
                const int         msDelay  = 250;

                // XSel is required
                string strTest = NativeLib.RunConsoleApp(strXSel, strXSelR);
                if (strTest == null)
                {
                    return;                                 // XSel not installed
                }
                // Without XDoTool, the workaround would be applied to
                // all applications, which may corrupt the clipboard
                // when it doesn't contain simple text only;
                // https://sourceforge.net/p/keepass/bugs/1603/#a113
                strTest = (NativeLib.RunConsoleApp(AppXDoTool,
                                                   "help") ?? string.Empty).Trim();
                if (strTest.Length == 0)
                {
                    return;
                }

                Thread.Sleep(msDelay);

                string strLast = null;
                while (true)
                {
                    string str = NativeLib.RunConsoleApp(strXSel, strXSelR);
                    if (str == null)
                    {
                        Debug.Assert(false);
                    }
                    else if (str != strLast)
                    {
                        if (NeedClipboardWorkaround())
                        {
                            // Use --nodetach to prevent clipboard corruption;
                            // https://sourceforge.net/p/keepass/bugs/1603/
                            NativeLib.RunConsoleApp(strXSel, strXSelW, str, rfW);
                        }

                        strLast = str;
                    }

                    Thread.Sleep(msDelay);
                }
#endif
            }
            catch (ThreadAbortException)
            {
                try { Thread.ResetAbort(); }
                catch (Exception) { Debug.Assert(false); }
            }
            catch (Exception) { Debug.Assert(false); }
            finally { g_thFixClip = null; }
        }