コード例 #1
0
        /// <inheritdoc />
        public bool TryElevate(ElevationAction elevationAction, string reason)
        {
            if (reason == null)
                throw new ArgumentNullException("reason");
            if (elevationAction == null)
                throw new ArgumentNullException("elevationAction");

            if (HasElevatedPrivileges)
            {
                var elevationContext = new LocalElevationContext(runtime);
                return elevationAction(elevationContext);
            }

            try
            {
                using (IHost host = CreateHost())
                {
                    var elevationContext = new HostedElevationContext(host);
                    if (!elevationContext.Initialize(runtime.GetRuntimeSetup(), runtime.Logger))
                        return false;

                    return elevationAction(elevationContext);
                }
            }
            catch (Exception ex)
            {
                runtime.Logger.Log(LogSeverity.Error, "Failed to create an elevation context out of process.", ex);
                return false;
            }
        }
コード例 #2
0
        /// <inheritdoc />
        public bool TryElevate(ElevationAction elevationAction, string reason)
        {
            if (reason == null)
            {
                throw new ArgumentNullException("reason");
            }
            if (elevationAction == null)
            {
                throw new ArgumentNullException("elevationAction");
            }

            if (HasElevatedPrivileges)
            {
                var elevationContext = new LocalElevationContext(runtime);
                return(elevationAction(elevationContext));
            }

            try
            {
                using (IHost host = CreateHost())
                {
                    var elevationContext = new HostedElevationContext(host);
                    if (!elevationContext.Initialize(runtime.GetRuntimeSetup(), runtime.Logger))
                    {
                        return(false);
                    }

                    return(elevationAction(elevationContext));
                }
            }
            catch (Exception ex)
            {
                runtime.Logger.Log(LogSeverity.Error, "Failed to create an elevation context out of process.", ex);
                return(false);
            }
        }
コード例 #3
0
        private static bool ElevateIfRequired(IWin32Window window, int[] pids, string[] names, ProcessAccess access, string action)
        {
            ElevationAction result = PromptForElevation(window, pids, names, access, "elevate the action", action);

            switch (result)
            {
            case ElevationAction.DontElevate:
            case ElevationAction.NotRequired:
                return(false);

            case ElevationAction.Cancel:
                return(true);

            case ElevationAction.Elevate:
            {
                string objects = string.Empty;

                foreach (int pid in pids)
                {
                    objects += pid + ",";
                }

                Program.StartProcessHackerAdmin(
                    "-e -type process -action " + action + " -obj \"" + objects + "\" -hwnd " + window.Handle.ToString()
                    ,
                    null,
                    window.Handle
                    );

                return(true);
            }

            default:
                return(false);
            }
        }