Example #1
0
        /// <summary>
        /// Sets protection on a block previously allocated with AllocateLarge.
        /// </summary>
        /// <param name="block">The starting memory address to set protection for.</param>
        /// <param name="size">The size of the block.</param>
        /// <param name="readAccess">Whether to allow read access.</param>
        /// <param name="writeAccess">Whether to allow write access.</param>
        /// <remarks>
        /// You may not specify false for read access without also specifying false for write access.
        /// Note to implementors: This method is not guaranteed to actually set read/write-ability
        /// on a block of memory, and may instead be implemented as a no-op after parameter validation.
        /// </remarks>
        public static void ProtectBlockLarge(IntPtr block, ulong size, bool readAccess, bool writeAccess)
        {
            uint flOldProtect;
            uint flNewProtect;

            if (readAccess && writeAccess)
            {
                flNewProtect = NativeConstants.PAGE_READWRITE;
            }
            else if (readAccess && !writeAccess)
            {
                flNewProtect = NativeConstants.PAGE_READONLY;
            }
            else if (!readAccess && !writeAccess)
            {
                flNewProtect = NativeConstants.PAGE_NOACCESS;
            }
            else
            {
                throw new InvalidOperationException("May not specify a page to be write-only");
            }

#if DEBUGSPEW
            Tracing.Ping("ProtectBlockLarge: block #" + block.ToString() + ", read: " + readAccess + ", write: " + writeAccess);
#endif

            SafeNativeMethods.VirtualProtect(block, new UIntPtr(size), flNewProtect, out flOldProtect);
        }
Example #2
0
        /// <summary>
        /// Launches the default browser and opens the given URL.
        /// </summary>
        /// <param name="url">The URL to show. The maximum length is 512 characters.</param>
        /// <remarks>
        /// This method will not present an error dialog if the URL could not be launched.
        /// Note: This method must only be used by Paint.NET, and not any plugins. It may
        /// change or be removed in future versions.
        /// </remarks>
        public static bool LaunchUrl(IWin32Window owner, string url)
        {
            if (url.Length > 512)
            {
                throw new ArgumentOutOfRangeException("url.Length must be <= 512");
            }

            bool             success   = false;
            string           quotedUrl = "\"" + url + "\"";
            ExecutePrivilege executePrivilege;

            if (!Security.IsAdministrator || (Security.IsAdministrator && !Security.CanLaunchNonAdminProcess))
            {
                executePrivilege = ExecutePrivilege.AsInvokerOrAsManifest;
            }
            else
            {
                executePrivilege = ExecutePrivilege.RequireNonAdminIfPossible;
            }

            // Method 1. Just launch the url, and hope that the shell figures out the association correctly.
            // This method will not work with ExecutePrivilege.RequireNonAdmin though.
            if (!success && executePrivilege != ExecutePrivilege.RequireNonAdminIfPossible)
            {
                try
                {
                    Execute(owner, quotedUrl, null, executePrivilege, ExecuteWaitType.ReturnImmediately);
                    success = true;
                }

                catch (Exception ex)
                {
                    Tracing.Ping("Exception while using method 1 to launch url, " + quotedUrl + ", :" + ex.ToString());
                    success = false;
                }
            }

            // Method 2. Launch the url through explorer
            if (!success)
            {
                const string shellFileLoc = @"%WINDIR%\explorer.exe";
                string       shellExePath = "(n/a)";

                try
                {
                    shellExePath = Environment.ExpandEnvironmentVariables(shellFileLoc);
                    Execute(owner, shellExePath, quotedUrl, executePrivilege, ExecuteWaitType.ReturnImmediately);
                    success = true;
                }

                catch (Exception ex)
                {
                    Tracing.Ping("Exception while using method 2 to launch url through '" + shellExePath + "', " + quotedUrl + ", : " + ex.ToString());
                    success = false;
                }
            }

            return(success);
        }
Example #3
0
 private void OnItemMouseEnter(object sender, EventArgs e)
 {
     if (this.managedFocus && !MenuStripEx.IsAnyMenuActive && UI.IsOurAppActive && !IsFocusLocked)
     {
         Tracing.Ping("stealing focus");
         this.Focus();
     }
 }
Example #4
0
        protected override void OnMouseLeave(EventArgs e)
        {
            if (this.managedFocus && !MenuStripEx.IsAnyMenuActive && UI.IsOurAppActive && !IsFocusLocked)
            {
                Tracing.Ping("relinquishing focus");
                OnRelinquishFocus();
            }

            base.OnMouseLeave(e);
        }
Example #5
0
        /// <summary>
        /// Draws several filled rectangles using the same color. If there is an error while trying to draw,
        /// it is discarded and ignored.
        /// </summary>
        /// <param name="g">The Graphics context to draw to.</param>
        /// <param name="rects">A list of rectangles to draw.</param>
        /// <param name="color">The color to fill the rectangles with.</param>
        /// <remarks>
        /// Note to implementors: This method is used to avoid drawing with GDI+, which avoids flickering
        /// with our transparent toolforms. Implementations may thunk straight to g.FillRectangle().
        /// </remarks>
        public static void FillRectangles(Graphics g, Color color, Rectangle[] rects)
        {
            try
            {
                FillRectanglesImpl(g, color, rects);
            }

            catch (Exception ex)
            {
                Tracing.Ping("Exception while executing PdnGraphics.FillRectangles: " + ex.ToString());
            }
        }
Example #6
0
        /// <summary>
        /// Draws a polygon. The last point is not joined to the beginning point. If there is an error while
        /// trying to draw, it is discarded and ignored.
        /// </summary>
        /// <param name="g">The Graphics context to draw to.</param>
        /// <param name="points">The points to draw. Lines are drawn between every point N to point N+1.</param>
        /// <param name="color">The color to draw with.</param>
        /// <remarks>
        /// Note to implementors: This method is used to avoid drawing with GDI+, which avoids flickering
        /// with our transparent toolforms. Implementations may thunk straight to g.DrawLines().
        /// </remarks>
        public static void DrawPolyLine(Graphics g, Color color, Point[] points)
        {
            try
            {
                DrawPolyLineImpl(g, color, points);
            }

            catch (Exception ex)
            {
                Tracing.Ping("Exception while executing PdnGraphics.DrawPolyLine: " + ex.ToString());
            }
        }
Example #7
0
        public PropertyItem2(int id, int len, short type, byte[] value)
        {
            this.id   = id;
            this.len  = len;
            this.type = type;

            if (value == null)
            {
                this.value = new byte[0];
            }
            else
            {
                this.value = (byte[])value.Clone();
            }

            if (len != this.value.Length)
            {
                Tracing.Ping("len != value.Length: id=" + id + ", type=" + type);
            }
        }
Example #8
0
        private static int ExecRequireNonAdmin(IWin32Window parent, string exePath, string args, out IntPtr hProcess)
        {
            int    nError      = NativeConstants.ERROR_SUCCESS;
            string commandLine = "\"" + exePath + "\"" + (args == null ? "" : (" " + args));

            string dir;

            try
            {
                dir = Path.GetDirectoryName(exePath);
            }

            catch (Exception)
            {
                dir = null;
            }

            IntPtr hWndShell          = IntPtr.Zero;
            IntPtr hShellProcess      = IntPtr.Zero;
            IntPtr hShellProcessToken = IntPtr.Zero;
            IntPtr hTokenCopy         = IntPtr.Zero;
            IntPtr bstrExePath        = IntPtr.Zero;
            IntPtr bstrCommandLine    = IntPtr.Zero;
            IntPtr bstrDir            = IntPtr.Zero;

            NativeStructs.PROCESS_INFORMATION procInfo = new NativeStructs.PROCESS_INFORMATION();

            try
            {
                hWndShell = SafeNativeMethods.FindWindowW("Progman", null);
                if (hWndShell == IntPtr.Zero)
                {
                    NativeMethods.ThrowOnWin32Error("FindWindowW() returned NULL");
                }

                uint dwPID;
                uint dwThreadId = SafeNativeMethods.GetWindowThreadProcessId(hWndShell, out dwPID);
                if (0 == dwPID)
                {
                    NativeMethods.ThrowOnWin32Error("GetWindowThreadProcessId returned 0", NativeErrors.ERROR_FILE_NOT_FOUND);
                }

                hShellProcess = NativeMethods.OpenProcess(NativeConstants.PROCESS_QUERY_INFORMATION, false, dwPID);
                if (IntPtr.Zero == hShellProcess)
                {
                    NativeMethods.ThrowOnWin32Error("OpenProcess() returned NULL");
                }

                bool optResult = NativeMethods.OpenProcessToken(
                    hShellProcess,
                    NativeConstants.TOKEN_ASSIGN_PRIMARY | NativeConstants.TOKEN_DUPLICATE | NativeConstants.TOKEN_QUERY,
                    out hShellProcessToken);

                if (!optResult)
                {
                    NativeMethods.ThrowOnWin32Error("OpenProcessToken() returned FALSE");
                }

                bool dteResult = NativeMethods.DuplicateTokenEx(
                    hShellProcessToken,
                    NativeConstants.MAXIMUM_ALLOWED,
                    IntPtr.Zero,
                    NativeConstants.SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation,
                    NativeConstants.TOKEN_TYPE.TokenPrimary,
                    out hTokenCopy);

                if (!dteResult)
                {
                    NativeMethods.ThrowOnWin32Error("DuplicateTokenEx() returned FALSE");
                }

                bstrExePath     = Marshal.StringToBSTR(exePath);
                bstrCommandLine = Marshal.StringToBSTR(commandLine);
                bstrDir         = Marshal.StringToBSTR(dir);

                bool cpwtResult = NativeMethods.CreateProcessWithTokenW(
                    hTokenCopy,
                    0,
                    bstrExePath,
                    bstrCommandLine,
                    0,
                    IntPtr.Zero,
                    bstrDir,
                    IntPtr.Zero,
                    out procInfo);

                if (cpwtResult)
                {
                    hProcess          = procInfo.hProcess;
                    procInfo.hProcess = IntPtr.Zero;
                    nError            = NativeConstants.ERROR_SUCCESS;
                }
                else
                {
                    hProcess = IntPtr.Zero;
                    nError   = Marshal.GetLastWin32Error();
                }
            }

            catch (Win32Exception ex)
            {
                Tracing.Ping(ex.ToString());
                nError   = ex.ErrorCode;
                hProcess = IntPtr.Zero;
            }

            finally
            {
                if (bstrExePath != IntPtr.Zero)
                {
                    Marshal.FreeBSTR(bstrExePath);
                    bstrExePath = IntPtr.Zero;
                }

                if (bstrCommandLine != IntPtr.Zero)
                {
                    Marshal.FreeBSTR(bstrCommandLine);
                    bstrCommandLine = IntPtr.Zero;
                }

                if (bstrDir != IntPtr.Zero)
                {
                    Marshal.FreeBSTR(bstrDir);
                    bstrDir = IntPtr.Zero;
                }

                if (hShellProcess != IntPtr.Zero)
                {
                    SafeNativeMethods.CloseHandle(hShellProcess);
                    hShellProcess = IntPtr.Zero;
                }

                if (hShellProcessToken != IntPtr.Zero)
                {
                    SafeNativeMethods.CloseHandle(hShellProcessToken);
                    hShellProcessToken = IntPtr.Zero;
                }

                if (hTokenCopy != IntPtr.Zero)
                {
                    SafeNativeMethods.CloseHandle(hTokenCopy);
                    hTokenCopy = IntPtr.Zero;
                }

                if (procInfo.hThread != IntPtr.Zero)
                {
                    SafeNativeMethods.CloseHandle(procInfo.hThread);
                    procInfo.hThread = IntPtr.Zero;
                }

                if (procInfo.hProcess != IntPtr.Zero)
                {
                    SafeNativeMethods.CloseHandle(procInfo.hProcess);
                    procInfo.hProcess = IntPtr.Zero;
                }
            }

            return(nError);
        }