Determining if Aero is Enabled in C#

By FoxLearn 1/16/2025 7:38:33 AM   41
While working on a recent project, I needed to adjust the UI based on whether Aero Glass was enabled in Windows.

Since there’s no direct support for this in .NET, I had to leverage DllImport to call the DwmIsCompositionEnabled function from the dwmapi.dll library.

Windows Vista and later versions support the Aero Glass effect, which is a graphical feature providing a translucent window frame. To determine whether Aero is enabled, you need to call the DwmIsCompositionEnabled function from the dwmapi.dll dynamic link library.

HRESULT DwmIsCompositionEnabled(BOOL *pfEnabled);

This function returns a BOOL indicating whether Aero Glass is enabled, and it’s what we need to call in .NET.

First, you need to import the DwmIsCompositionEnabled function into your .NET application using the DllImport attribute.

[DllImport("dwmapi.dll", EntryPoint="DwmIsCompositionEnabled")]
public static extern int DwmIsCompositionEnabled(out bool enabled);

Once the function is imported, you can use it like any other method.

bool aeroEnabled;
DwmIsCompositionEnabled(out aeroEnabled);
Console.WriteLine("Aero Enabled: " + aeroEnabled.ToString());

This will print True if Aero is enabled or False if it’s not.

Since the DwmIsCompositionEnabled function is only available on Windows Vista and later, it's a good idea to check the operating system version before attempting to call it.

if (Environment.OSVersion.Version.Major > 5)
{
    bool aeroEnabled;
    DwmIsCompositionEnabled(out aeroEnabled);
    Console.WriteLine("Aero Enabled: " + aeroEnabled.ToString());
}
else
{
    Console.WriteLine("This function is not supported on versions earlier than Windows Vista.");
}

This ensures that you don’t attempt to call the function on an unsupported operating system.

By using DllImport to call DwmIsCompositionEnabled from the dwmapi.dll library, you can easily check whether Aero Glass is enabled on Windows Vista and later.