1

I want to enable desktop composition on a Windows 7 machine programmatically (preferrable C#) and already tried to do it with the infamous DwmEnableComposition P/Invoke. However, this does not seem to work if DWM has never been enabled (and the performance test has not run). I can, however, enable DWM via the "Personalization" control panel w/o running the performance test.

My question therefore is: how can I do the same programmatically? And a second question: how can I achieve that a change in the setting is persistent (e. g. if I set DwmEnableComposition(false) on a machine that has DWM enabled, it is re-enabled once the application exits)?

Christoph
  • 1,964
  • 2
  • 27
  • 44
  • 1
    Windows only supports *disabling* composition for your own program. You cannot turn Aero on or off system-wide. Nor can you enable composition when Aero is disabled. This is logical, a program has no business changing a system setting with such wide-ranging side-effects. – Hans Passant Mar 29 '11 at 14:13
  • OK, then let me rephrase the question: How can I programmatically enable the aero theme? – Christoph Mar 29 '11 at 14:19
  • 1
    You're only at stage 3 of the 5 stages of grief. Bargaining doesn't help either. – Hans Passant Mar 29 '11 at 14:26
  • shell32.dll does not agree... – Christoph Mar 29 '11 at 14:40

2 Answers2

1

I found a less nasty solution than my first one: Invoking winsat dwm will turn on Aero automatically once it finishes and if the hardware supports it - and it does not change the wallpaper etc.

Christoph
  • 1,964
  • 2
  • 27
  • 44
  • 1
    I tried this in Win7 with Aero off initially, and it failed to turn it on. I can and have turned it on manually and it works when I do this, so my setup is capable of this. – Jared Updike Apr 06 '11 at 18:46
  • Hmm - it did work on all my machines. Did you issue the shell command or did you use the COM API? – Christoph Apr 12 '11 at 21:05
  • @Jared: That did work for me. There are only two things I could imagine. First, your GPU does not support Aero. Second, someone has actively disabled it before. In the latter case, you probably must change the theme using desk.cpl. – Christoph Apr 14 '11 at 17:56
0

The answer is: You cannot do that.


Microsoft does not expose a programmatic way for programs to enable or disable desktop composition on the user's machine.

The reason they don't provide an API is that programs might use it.
In the past Microsoft trusted developers not to do the wrong thing. That has changed

Because programmers were trusted to do the right thing

DwmEnableComposition is only for your application to temporarily disable DWM if you aren't compatible with composition. It cannot be used to turn on composition, when the user has already turned it off.

Any computer sold in the last 5 years is graphically capable of using composition, and Windows turns it on by default if its supported. The only reason it would be turned off is:

  • the user turned it off
  • another application is running that doesn't support composition

In either case: you should not (and therefor cannot) turn it on.

You cannot do that.

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
  • 4
    If there is no API, programmers will find another way: rundll32.exe Shell32.dll,Control_RunDLL desk.cpl desk,@Themes /Action:OpenTheme /File:"C:\Windows\Resources\Themes\aero.theme" – Christoph Mar 29 '11 at 14:31
  • @Christoph It very well may be that you can modify a user's computer without his permission. Generally we call that malware. Notice that there's no longer a quick launch menu in Windows, or that programs cannot add (or pin) themselves to the start menu, or the new taskbar in Windows 7. It's all because developers can't be trusted to do the right thing. Desktop Composition is off. It's off for a reason. Unless you have a **very** good reason for turning it on: you are doing a bad thing. And considering that your app wasn't able to turn it off - i can't think of any reason to turn it on. – Ian Boyd Mar 29 '11 at 14:40
  • I agree that it is a hugly solution and you should not do it. However, I have a cluster consisting of dozens of nodes and I cannot use the GUI on every one of it. I would have expected that DWM was turned on automatically once team green's driver has been installed - but this is not the case. – Christoph Mar 29 '11 at 14:43
  • Btw, what I do is the same as unattended WAIK installations do. – Christoph Mar 29 '11 at 14:54