Ejemplo n.º 1
0
        /// <summary>
        ///     Default constructor, this opens the input destop with GENERIC_ALL
        ///     This is needed to support marshalling!!
        /// </summary>
        public SafeCurrentInputDesktopHandle() : base(true)
        {
            var hDesktop = User32Api.OpenInputDesktop(0, true, DesktopAccessRight.GENERIC_ALL);

            if (hDesktop != IntPtr.Zero)
            {
                // Got desktop, store it as handle for the ReleaseHandle
                SetHandle(hDesktop);
                if (User32Api.SetThreadDesktop(hDesktop))
                {
                    Log.Debug().WriteLine("Switched to desktop {0}", hDesktop);
                }
                else
                {
                    Log.Warn().WriteLine("Couldn't switch to desktop {0}", hDesktop);
                    Log.Error().WriteLine(User32Api.CreateWin32Exception("SetThreadDesktop"));
                }
            }
            else
            {
                Log.Warn().WriteLine("Couldn't get current desktop.");
                Log.Error().WriteLine(User32Api.CreateWin32Exception("OpenInputDesktop"));
            }
        }