Example #1
0
        public static int Main(string[] rgArgs)
        {
            // Create a semaphore to keep more than one instance of the application
            // from running at the same time.  If the semaphore is signalled, then
            // this instance can run.
            Win32.SecurityAttributes sa = new Win32.SecurityAttributes();
            IntPtr semaphore            = Win32.CreateSemaphore(ref sa, 1, 1,
                                                                Process.GetCurrentProcess().MainModule.ModuleName);

            switch (Win32.WaitForSingleObject(semaphore, 0))
            {
            case Win32.WAIT_OBJECT_0:
                // Using the 'using' gizmo will call Dispose on app,
                // which in turn will call Dispose for all FdoCache objects,
                // which will release all of the COM objects it connects to.
                using (LexTextApp application = new LexTextApp(rgArgs))
                {
                    SIL.Utils.ErrorReporter.EmailAddress = "*****@*****.**";
                    application.Run();
                }
                int previousCount;
                Win32.ReleaseSemaphore(semaphore, 1, out previousCount);
                break;

            case Win32.WAIT_TIMEOUT:
                // If the semaphore wait times out then another instance is running.
                // Try to get a handle to its window and activate it.  Then terminate
                // this process.
                try
                {
                    IntPtr hWndMain = ExistingProcess.MainWindowHandle;
                    if (hWndMain != (IntPtr)0)
                    {
                        Win32.SetForegroundWindow(hWndMain);
                    }
                }
                catch
                {
                    // The other instance does not have a window handle.  It is either in
                    // the process of starting up or shutting down.
                }
                break;
            }

            return(0);
        }
Example #2
0
            public static IntPtr OpenAppendFile(string fileName)
            {
                if (fileName == null)
                {
                    throw new ArgumentNullException("fileName");
                }
                Win32.SecurityAttributes securityAttributes = new Win32.SecurityAttributes();
                securityAttributes.Initialize();
                IntPtr handle = Win32.CreateFile(fileName,
                                                 Win32.FileAccess.AppendData,
                                                 Win32.FileShare.Read,
                                                 ref securityAttributes,
                                                 Win32.FileCreateDisposition.OpenAlways,
                                                 Win32.FileAttributes.Normal, IntPtr.Zero);

                if (handle == IntPtr.Zero)
                {
                    int lastError = Marshal.GetLastWin32Error();
                    throw new Win32Exception(lastError,
                                             string.Format(CouldNotOpenFileErrorFormat, fileName, lastError));
                }
                return(handle);
            }
Example #3
0
		public static int Main(string[] rgArgs)
		{
			// Enable visual styles. Ignored on Windows 2000. Needs to be called before
			// we create any controls! Unfortunately, this alone is not good enough. We
			// also need to use a manifest, because some ListView and TreeView controls
			// in native code do not have icons if we just use this method. This is caused
			// by a bug in XP.
			Application.EnableVisualStyles();

			// Create a semaphore to keep more than one instance of the application
			// from running at the same time.  If the semaphore is signalled, then
			// this instance can run.
			Win32.SecurityAttributes sa = new Win32.SecurityAttributes();
			IntPtr semaphore = Win32.CreateSemaphore(ref sa, 1, 1,
				Process.GetCurrentProcess().MainModule.ModuleName);
			switch (Win32.WaitForSingleObject(semaphore, 0))
			{
				case Win32.WAIT_OBJECT_0:
					// Using the 'using' gizmo will call Dispose on app,
					// which in turn will call Dispose for all FdoCache objects,
					// which will release all of the COM objects it connects to.
					using (LexTextApp application = new LexTextApp(rgArgs))
					{
						SIL.Utils.ErrorReporter.EmailAddress = "*****@*****.**";

						string extensionBaseDir = SIL.FieldWorks.Common.Utils.DirectoryFinder.GetFWDataSubDirectory(@"\Language Explorer\Configuration\Words\Extensions");
						string extensionDir = Path.Combine(extensionBaseDir, "Respeller");
						if (Directory.Exists(extensionDir))
						{
							try
							{
								foreach (string file in Directory.GetFiles(extensionDir))
								{
									File.SetAttributes(file, FileAttributes.Normal);
								}
								Directory.Delete(extensionDir, true);
							}
							catch
							{
								MessageBox.Show(String.Format("Please delete the '{0}' folder and all files in it, in order to run Language Explorer.", extensionDir));
								return 1;
							}
						}
						extensionDir = Path.Combine(extensionBaseDir, "DeleteWordforms");
						if (Directory.Exists(extensionDir))
						{
							try
							{
								foreach (string file in Directory.GetFiles(extensionDir))
								{
									File.SetAttributes(file, FileAttributes.Normal);
								}
								Directory.Delete(extensionDir, true);
							}
							catch
							{
								MessageBox.Show(String.Format("Please delete the '{0}' folder and all files in it, in order to run Language Explorer.", extensionDir));
								return 1;
							}
						}

						application.Run();
					}
					int previousCount;
					Win32.ReleaseSemaphore(semaphore, 1, out previousCount);
					break;

				case Win32.WAIT_TIMEOUT:
					// If the semaphore wait times out then another instance is running.
					// Try to get a handle to its window and activate it.  Then terminate
					// this process.
					try
					{
						IntPtr hWndMain = ExistingProcess.MainWindowHandle;
						if (hWndMain != (IntPtr)0)
							Win32.SetForegroundWindow(hWndMain);
					}
					catch
					{
						// The other instance does not have a window handle.  It is either in
						// the process of starting up or shutting down.
					}
					break;
			}

			return 0;
		}
Example #4
0
        public static int Main(string[] rgArgs)
        {
            // Create a semaphore to keep more than one instance of the application
            // from running at the same time.  If the semaphore is signalled, then
            // this instance can run.
            Win32.SecurityAttributes sa = new Win32.SecurityAttributes();
            IntPtr semaphore = Win32.CreateSemaphore(ref sa, 1, 1,
                Process.GetCurrentProcess().MainModule.ModuleName);
            switch (Win32.WaitForSingleObject(semaphore, 0))
            {
                case Win32.WAIT_OBJECT_0:
                    // Using the 'using' gizmo will call Dispose on app,
                    // which in turn will call Dispose for all FdoCache objects,
                    // which will release all of the COM objects it connects to.
                    using (PcPatrBrowserApp application = new PcPatrBrowserApp(rgArgs))
                    {
                        application.Run();
                    }
                    int previousCount;
                    Win32.ReleaseSemaphore(semaphore, 1, out previousCount);
                    break;

                case Win32.WAIT_TIMEOUT:
                    // If the semaphore wait times out then another instance is running.
                    // Try to get a handle to its window and activate it.  Then terminate
                    // this process.
                    try
                    {
                        IntPtr hWndMain = ExistingProcess.MainWindowHandle;
                        if (hWndMain != (IntPtr)0)
                            Win32.SetForegroundWindow(hWndMain);
                    }
                    catch
                    {
                        // The other instance does not have a window handle.  It is either in
                        // the process of starting up or shutting down.
                    }
                    break;
            }

            return 0;
        }
Example #5
0
            void StartProcess()
            {
                IntPtr stdin       = IntPtr.Zero;
                IntPtr stdoutRead  = IntPtr.Zero;
                IntPtr stdoutWrite = IntPtr.Zero;
                IntPtr stderr      = IntPtr.Zero;

                try
                {
                    Win32.StartupInfo startupInfo = new Win32.StartupInfo();
                    startupInfo.Initialize();
                    startupInfo.Flags = Win32.StartProcessFlags.UseStdHandles;
                    #region Set stdin
                    {
                        if (RedirectedStandardInput != null)
                        {
                            stdin = RedirectedStandardInput.ReleaseStandardOutputReadHandle();
                            Helper.EnsureHandleInheritable(stdin);
                        }
                        else
                        {
                            /* Duplicate the handle so that
                             * the cleaning logic is unified.
                             */
                            stdin = Helper.DuplicateHandleForInheritance(
                                Win32.GetStdHandle(Win32.StandardHandleId.StandardInput));
                        }
                        startupInfo.StandardInput = stdin;
                    }
                    #endregion Set stdin
                    #region Set stdout
                    {
                        Win32.SecurityAttributes pipeAttributes = new Win32.SecurityAttributes();
                        pipeAttributes.Initialize();
                        if (Win32.CreatePipe(out stdoutRead, out stdoutWrite,
                                             ref pipeAttributes, 4096) == Win32.Bool.False)
                        {
                            stdoutRead  = IntPtr.Zero;
                            stdoutWrite = IntPtr.Zero;
                            int lastError = Marshal.GetLastWin32Error();
                            throw new Win32Exception(lastError,
                                                     string.Format(CouldNotCreatePipeErrorFormat, lastError));
                        }
                        Helper.EnsureHandleInheritable(stdoutWrite);
                        startupInfo.StandardOutput = stdoutWrite;
                    }
                    #endregion Set stdout
                    #region Set stderr
                    {
                        if (RedirectedStandardError != null)
                        {
                            stderr = standardErrorAppend
                                ? Helper.OpenAppendFile(RedirectedStandardError)
                                : Helper.OpenTruncatedFile(RedirectedStandardError);
                            Helper.EnsureHandleInheritable(stderr);
                        }
                        else
                        {
                            stderr = Helper.DuplicateHandleForInheritance(
                                Win32.GetStdHandle(Win32.StandardHandleId.StandardError));
                        }
                        startupInfo.StandardError = stderr;
                    }
                    #endregion Set stderr
                    Win32.ProcessInformation processInformation;

                    /* Create the process suspended,
                     * and resume it only after we
                     * have started waiting for it.
                     */
                    if (Win32.CreateProcess(null,
                                            BuildCommandLine(),
                                            IntPtr.Zero, IntPtr.Zero,
                                            Win32.Bool.True, Win32.ProcessCreationFlags.CreateSuspended, IntPtr.Zero,
                                            InitialWorkingDirectory,
                                            ref startupInfo, out processInformation)
                        == Win32.Bool.False)
                    {
                        int lastError = Marshal.GetLastWin32Error();
                        throw new Win32Exception(lastError,
                                                 string.Format(CouldNotCreateProcessErrorFormat, lastError));
                    }
                    hasExited     = false;
                    processHandle = processInformation.HandleToProcess;
                    ProcessId     = processInformation.ProcessId;
                    new Thread(WaitForProcessExitWorker).Start(this);
                    Win32.ResumeThread(processInformation.HandleToThread);
                    Win32.CloseHandle(processInformation.HandleToThread);
                    stdoutReadHandle = stdoutRead;

                    /* Prevent this very handle from being
                     * closed. Other handles are useless now
                     * and will be closed in "finally".
                     */
                    stdoutRead = IntPtr.Zero;
                }
                finally
                {
                    if (stdin != IntPtr.Zero)
                    {
                        Win32.CloseHandle(stdin);
                    }
                    if (stdoutRead != IntPtr.Zero)
                    {
                        Win32.CloseHandle(stdoutRead);
                    }
                    if (stdoutWrite != IntPtr.Zero)
                    {
                        Win32.CloseHandle(stdoutWrite);
                    }
                    if (stderr != IntPtr.Zero)
                    {
                        Win32.CloseHandle(stderr);
                    }
                }
            }
Example #6
0
        public static int Main(string[] rgArgs)
        {
            // Enable visual styles. Ignored on Windows 2000. Needs to be called before
            // we create any controls! Unfortunately, this alone is not good enough. We
            // also need to use a manifest, because some ListView and TreeView controls
            // in native code do not have icons if we just use this method. This is caused
            // by a bug in XP.
            Application.EnableVisualStyles();

            // Create a semaphore to keep more than one instance of the application
            // from running at the same time.  If the semaphore is signalled, then
            // this instance can run.
            Win32.SecurityAttributes sa = new Win32.SecurityAttributes();
            IntPtr semaphore            = Win32.CreateSemaphore(ref sa, 1, 1,
                                                                Process.GetCurrentProcess().MainModule.ModuleName);

            switch (Win32.WaitForSingleObject(semaphore, 0))
            {
            case Win32.WAIT_OBJECT_0:
                // Using the 'using' gizmo will call Dispose on app,
                // which in turn will call Dispose for all FdoCache objects,
                // which will release all of the COM objects it connects to.
                using (LexTextApp application = new LexTextApp(rgArgs))
                {
                    SIL.Utils.ErrorReporter.EmailAddress = "*****@*****.**";

                    string extensionBaseDir = SIL.FieldWorks.Common.Utils.DirectoryFinder.GetFWDataSubDirectory(@"\Language Explorer\Configuration\Words\Extensions");
                    string extensionDir     = Path.Combine(extensionBaseDir, "Respeller");
                    if (Directory.Exists(extensionDir))
                    {
                        try
                        {
                            foreach (string file in Directory.GetFiles(extensionDir))
                            {
                                File.SetAttributes(file, FileAttributes.Normal);
                            }
                            Directory.Delete(extensionDir, true);
                        }
                        catch
                        {
                            MessageBox.Show(String.Format("Please delete the '{0}' folder and all files in it, in order to run Language Explorer.", extensionDir));
                            return(1);
                        }
                    }
                    extensionDir = Path.Combine(extensionBaseDir, "DeleteWordforms");
                    if (Directory.Exists(extensionDir))
                    {
                        try
                        {
                            foreach (string file in Directory.GetFiles(extensionDir))
                            {
                                File.SetAttributes(file, FileAttributes.Normal);
                            }
                            Directory.Delete(extensionDir, true);
                        }
                        catch
                        {
                            MessageBox.Show(String.Format("Please delete the '{0}' folder and all files in it, in order to run Language Explorer.", extensionDir));
                            return(1);
                        }
                    }

                    application.Run();
                }
                int previousCount;
                Win32.ReleaseSemaphore(semaphore, 1, out previousCount);
                break;

            case Win32.WAIT_TIMEOUT:
                // If the semaphore wait times out then another instance is running.
                // Try to get a handle to its window and activate it.  Then terminate
                // this process.
                try
                {
                    IntPtr hWndMain = ExistingProcess.MainWindowHandle;
                    if (hWndMain != (IntPtr)0)
                    {
                        Win32.SetForegroundWindow(hWndMain);
                    }
                }
                catch
                {
                    // The other instance does not have a window handle.  It is either in
                    // the process of starting up or shutting down.
                }
                break;
            }

            return(0);
        }