Esempio n. 1
0
        /// <summary>
        ///     Runs the application with the specified <paramref name="form" /> a single instance application.
        /// </summary>
        /// <param name="form">The form.</param>
        public static void RunAsSingleton(Form form)
        {
            bool createdNew;

            // Use a Mutex to make this a single instance application.
            using (new Mutex(true, Application.ProductName, out createdNew))
            {
                if (createdNew)
                {
                    Application.Run(form);
                }
                else
                {
                    // Display the application that is already running by using Native API methods.
                    Process current = Process.GetCurrentProcess();
                    foreach (Process process in Process.GetProcessesByName(current.ProcessName))
                    {
                        if (process.Id != current.Id)
                        {
                            UnsafeWindowMethods.ShowWindow(process.MainWindowHandle, UnsafeWindowMethods.WindowShowStyle.Restore);
                            break;
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        ///     Handles the <see cref="PropertyChangedCallback" /> for the CanMinimize dependency property.
        /// </summary>
        /// <param name="d">The dependency property.</param>
        /// <param name="e">
        ///     The <see cref="System.Windows.DependencyPropertyChangedEventArgs" /> instance containing the event
        ///     data.
        /// </param>
        private static void OnCanMinimizeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Window window = d as Window;

            if (window != null)
            {
                RoutedEventHandler loadedHandler = null;
                loadedHandler = delegate
                {
                    lock (window)
                    {
                        UnsafeWindowMethods.SetMinimizeBoxVisibility(window, (Visibility)e.NewValue);
                    }

                    window.Loaded -= loadedHandler;
                };

                if (!window.IsLoaded)
                {
                    window.Loaded += loadedHandler;
                }
                else
                {
                    loadedHandler(null, null);
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        ///     Impersonates the specified user name.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <param name="domain">The domain.</param>
        /// <param name="password">The password.</param>
        /// <param name="impersonationLevel">The impersonation level.</param>
        /// <param name="logonType">Type of the logon.</param>
        /// <exception cref="Win32Exception">
        /// </exception>
        private void Impersonate(string userName, string domain, SecureString password, ImpersonationLevel impersonationLevel, LogonType logonType)
        {
            if (UnsafeWindowMethods.RevertToSelf())
            {
                var token         = Marshal.SecureStringToGlobalAllocUnicode(password);
                var logonProvider = (logonType == LogonType.NewCredentials)
                    ? UnsafeWindowMethods.LogonProvider.WinNT50
                    : UnsafeWindowMethods.LogonProvider.Default;

                try
                {
                    SafeTokenHandle safeTokenHandle;
                    if (UnsafeWindowMethods.LogonUser(userName, domain, token, logonType, logonProvider, out safeTokenHandle) != 0)
                    {
                        using (safeTokenHandle)
                        {
                            if (UnsafeWindowMethods.DuplicateToken(safeTokenHandle.DangerousGetHandle(), (int)impersonationLevel, out _SafeDuplicateTokenHandle) != 0)
                            {
                                _ImpersonationContext = WindowsIdentity.Impersonate(_SafeDuplicateTokenHandle.DangerousGetHandle());
                            }
                            else
                            {
                                throw new Win32Exception(Marshal.GetLastWin32Error());
                            }
                        }
                    }
                    else
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    }
                }
                finally
                {
                    // Perform cleanup whether or not the call succeeded.
                    // Zero-out and free the unmanaged string reference.
                    Marshal.ZeroFreeGlobalAllocUnicode(token);
                }
            }
            else
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
        }