Disable Aero Peek in a WPF Application

I want to disable Aero Peek in my WPF application (my application should be visible when the user hovers over the "Show Desktop" button). I use this PInvoke signature:

[Flags]
public enum DwmWindowAttribute : uint
{
    DWMWA_NCRENDERING_ENABLED = 1,
    DWMWA_NCRENDERING_POLICY,
    DWMWA_TRANSITIONS_FORCEDISABLED,
    DWMWA_ALLOW_NCPAINT,
    DWMWA_CAPTION_BUTTON_BOUNDS,
    DWMWA_NONCLIENT_RTL_LAYOUT,
    DWMWA_FORCE_ICONIC_REPRESENTATION,
    DWMWA_FLIP3D_POLICY,
    DWMWA_EXTENDED_FRAME_BOUNDS,
    DWMWA_HAS_ICONIC_BITMAP,
    DWMWA_DISALLOW_PEEK,
    DWMWA_EXCLUDED_FROM_PEEK,
    DWMWA_LAST
}

[Flags]
public enum DWMNCRenderingPolicy : uint
{
    UseWindowStyle,
    Disabled,
    Enabled,
    Last
}

[DllImport("dwmapi.dll", PreserveSig=false)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DwmIsCompositionEnabled();

[DllImport("dwmapi.dll", PreserveSig=false)]
public static extern Int32 DwmSetWindowAttribute(IntPtr hwnd,
                                                 DwmWindowAttribute dwmAttribute,
                                                 IntPtr pvAttribute,
                                                 uint cbAttribute);

and this use:

    var helper = new WindowInteropHelper(this);
    helper.EnsureHandle();

    if (API.DwmIsCompositionEnabled())
    {
        var status = Marshal.AllocCoTaskMem(sizeof(uint));
        Marshal.Copy(new[] {(int) API.DWMNCRenderingPolicy.Enabled}, 0, status, 1);
        API.DwmSetWindowAttribute(helper.Handle,
                                  API.DwmWindowAttribute.DWMWA_EXCLUDED_FROM_PEEK,
                                  status,
                                  sizeof (uint));
    }

On my 64-bit system (Windows 7 Professional), it only works if I run a 64-bit application. If I run a 32-bit application in WOW64 mode, I get an exception:

"Calling the PInvoke function" XXX :: DwmSetWindowAttribute "did not balance the stack. Probably because the PInvoke managed signature does not match the unmanaged target signature. Make sure the calling convention and the PInvoke signature parameters match the target unmanaged signature.

What do you think about this? What is the solution?

+5
1

:

[DllImport("dwmapi.dll", PreserveSig = true)]
public static extern int DwmSetWindowAttribute(IntPtr hwnd,
                                               DwmWindowAttribute dwmAttribute,
                                               IntPtr pvAttribute,
                                               uint cbAttribute);

:

if (API.DwmIsCompositionEnabled())
{
    var status = Marshal.AllocHGlobal(sizeof(int));
    Marshal.WriteInt32(status, 1); // true
    API.DwmSetWindowAttribute(helper.Handle,
                              API.DwmWindowAttribute.DWMWA_EXCLUDED_FROM_PEEK,
                              status,
                              sizeof(int));
}
+5

All Articles