My DWM + WPF Hack

1,218 views
Skip to first unread message

Jeremiah Morrill

unread,
Nov 5, 2008, 3:31:46 PM11/5/08
to wpf-di...@googlegroups.com
The Vista DWM API is pretty weak in terms of what you are allowed to do (Maybe Windows 7 will be better??).  For the most part you can only put a live dwm thumbnail on a 2d hwnd, which is pretty boring.  With a bit of hacking, I got a couple undocumented dwm api methods ported to .NET that let you tap a little closer into the dwm.  Now with any hWnd that is using Direct3D (Dx9, Dx10, WPF), I can render the hWnd to any WPF surface (thanks D3DImage).

In this video, the window on the right is a DX10 sample from the DX SDK, on the left is a WPF window, rendering the contents of the DX10 window.

http://www.youtube.com/watch?v=tD9FjFVC5aI

Here are the undocumented dwm API calls.  There's a little more code for the "magic"...but it's documented in the 9Ex so I'll leave that stuff out.

        [PreserveSig]
        [DllImport("dwmapi.dll", EntryPoint="#101")]
        public static extern int UpdateWindowShared(IntPtr hWnd, int one, int two, int three, IntPtr hMonitor, IntPtr unknown);

        [PreserveSig]
        [DllImport("dwmapi.dll", EntryPoint = "#100")]
        public static extern int GetSharedSurface(IntPtr hWnd, Int64 adapterLuid, uint one, uint two, [In, Out]ref uint pD3DFormat, [Out]out IntPtr pSharedHandle, UInt64 unknownArg);

-Jer

Josh Smith

unread,
Nov 5, 2008, 3:36:37 PM11/5/08
to wpf-di...@googlegroups.com
Holy smokes!  RAAAD!

Are those the WPF Disciples marching toward a group of Flex developers?  :)

Josh

Jeremiah Morrill

unread,
Nov 5, 2008, 3:37:36 PM11/5/08
to wpf-di...@googlegroups.com
Yup.  On their way to ruff 'em up!

Mike Brown

unread,
Nov 5, 2008, 3:47:08 PM11/5/08
to wpf-di...@googlegroups.com
You know the problem is once a product group outside of the Windows team starts using those APIs people are going to scream bloody hell again. Are those api's documented at least for non-managed (C++) development?

Jeremiah Morrill

unread,
Nov 5, 2008, 3:53:11 PM11/5/08
to wpf-di...@googlegroups.com
Naw a native disassembler was used (IDA).  This was started a long while back by a developer named "Siwu".  He got 1/2 way and I got the other half.

The C functions aren't even exported, thats why the DllImport entry points are #100, and #101.

-Jer

Pavan Podila

unread,
Nov 5, 2008, 3:57:05 PM11/5/08
to WPF Disciples
Sweet stuff Jer... so an ElementFlow demo is not too far away ;)

On Nov 5, 3:53 pm, "Jeremiah Morrill" <jeremiah.morr...@gmail.com>
wrote:
> Naw a native disassembler was used (IDA).  This was started a long while
> back by a developer named "Siwu".  He got 1/2 way and I got the other half.
>
> The C functions aren't even exported, thats why the DllImport entry points
> are #100, and #101.
>
> -Jer
>
> On Wed, Nov 5, 2008 at 12:47 PM, Mike Brown <mbrow...@gmail.com> wrote:
> > You know the problem is once a product group outside of the Windows team
> > starts using those APIs people are going to scream bloody hell again. Are
> > those api's documented at least for non-managed (C++) development?
>
> > On Wed, Nov 5, 2008 at 3:37 PM, Jeremiah Morrill <
> > jeremiah.morr...@gmail.com> wrote:
>
> >> Yup.  On their way to ruff 'em up!
>

Jeremiah Morrill

unread,
Nov 5, 2008, 3:59:07 PM11/5/08
to wpf-di...@googlegroups.com
The WPF window on the left in that video is actually ElementFlow with one D3DImage in it :).  I was too lazy to throw in my own reflection (I like to use reflection as "evidence" of WPF).

-Jer

Josh Smith

unread,
Nov 5, 2008, 3:59:54 PM11/5/08
to wpf-di...@googlegroups.com
I'm waiting for Jer to release a DX10-DWM-Win32-DirectShow-WPF-Silverlight masterpiece where a bunch of 3D robots are watching 42 simultaneous videos of other 3D robots competing in a massive breakdancing competition.  I just *know* that's what all his projects are leading up to........

Josh

Pavan Podila

unread,
Nov 5, 2008, 4:03:00 PM11/5/08
to WPF Disciples
Its time to replace Flip3D. Jer, do you know how to hack into Vista to
invoke an ElementFlow version of the window-switcher. That would be
cool ;-)

On Nov 5, 3:59 pm, "Jeremiah Morrill" <jeremiah.morr...@gmail.com>
wrote:
> The WPF window on the left in that video is actually ElementFlow with one
> D3DImage in it :).  I was too lazy to throw in my own reflection (I like to
> use reflection as "evidence" of WPF).
>
> -Jer
>

Jeremiah Morrill

unread,
Nov 5, 2008, 4:03:31 PM11/5/08
to wpf-di...@googlegroups.com
LOL.  Yeah, I'll have the DX10-DWM-Win32-DirectShow-WPF-Silverlight monstrosity on your desk by Friday. haha!

I do want to take the DWM surface, encode it to windows media (using the WMF SDK) and stream it to silverlight.  Might be a cool single-app-remote-desktop for silverlight.  The problem is the windows media stream latency sucks (~3 seconds behind real time).

-Jer

Jeremiah Morrill

unread,
Nov 5, 2008, 4:04:48 PM11/5/08
to wpf-di...@googlegroups.com
Thats what I *really* want to do.  The downside with these undocumented dwm calls is it will only work with Direct3D based applications.  That means GDI based applications just give me a blank D3D surface. :(

-Jer

Pavan Podila

unread,
Nov 5, 2008, 4:06:55 PM11/5/08
to WPF Disciples
Josh's Crack.Net can use a few tricks from your library. I have spoken
to Josh at the PDC about those possibilities!

On Nov 5, 4:04 pm, "Jeremiah Morrill" <jeremiah.morr...@gmail.com>
wrote:
> Thats what I *really* want to do.  The downside with these undocumented dwm
> calls is it will only work with Direct3D based applications.  That means GDI
> based applications just give me a blank D3D surface. :(
>
> -Jer
>

Josh Smith

unread,
Nov 5, 2008, 4:07:49 PM11/5/08
to wpf-di...@googlegroups.com
You've piqued my interest...Tell me more...

Pavan Podila

unread,
Nov 5, 2008, 4:11:24 PM11/5/08
to WPF Disciples
Ok... here's a short version:

- Overlay rectangles
- Element Snapshots
- Ruler Measurements and Overlays

;-)

On Nov 5, 4:07 pm, "Josh Smith" <flappleja...@gmail.com> wrote:
> You've piqued my interest...Tell me more...
>

Jeremiah Morrill

unread,
Nov 5, 2008, 4:13:53 PM11/5/08
to wpf-di...@googlegroups.com
I think it'd be cool if Crack.NET had a visual process selector of window thumbnails, instead of a list of processes.  You could use the official dwm apis for that...or vanilla win32 to get a bitmap of each hwnd.

-Jer

Josh Smith

unread,
Nov 5, 2008, 4:15:52 PM11/5/08
to wpf-di...@googlegroups.com
Ruler measurements...great idea!  The other two features have been "slated" already, but i love this Ruler idea too.  Perhaps also add in the ability to "zoom" any element (via scale transforms).  The possibilities are endless...

Josh

Josh Smith

unread,
Nov 5, 2008, 4:16:45 PM11/5/08
to wpf-di...@googlegroups.com
Jer.  You rock.  If you want to add that feature in, I'll gladly make you Developer on the project.  ;)

Josh

Pavan Podila

unread,
Nov 5, 2008, 4:17:48 PM11/5/08
to WPF Disciples
I think its time Jer makes a library out of his findings ;) Crack.Net
will be the first customer !

On Nov 5, 4:15 pm, "Josh Smith" <flappleja...@gmail.com> wrote:
> Ruler measurements...great idea!  The other two features have been "slated"
> already, but i love this Ruler idea too.  Perhaps also add in the ability to
> "zoom" any element (via scale transforms).  The possibilities are endless...
>
> Josh
>

Pavan Podila

unread,
Nov 5, 2008, 4:20:57 PM11/5/08
to WPF Disciples
Don't forget, there is FluidKit for the visual richness ;-)

BTW, today I checked in a bunch of changes for TransitionPresenter
(renamed from TransitionContainer), which will enable more transition
related scenarios ;-). I am already using them on a current project
and I literally removed 15 Storyboards and down to just 2
TransitionPresenters, not to mention the endless Transition
possibilities !

On Nov 5, 4:16 pm, "Josh Smith" <flappleja...@gmail.com> wrote:
> Jer.  You rock.  If you want to add that feature in, I'll gladly make you
> Developer on the project.  ;)
>
> Josh
>
> On Wed, Nov 5, 2008 at 4:13 PM, Jeremiah Morrill <jeremiah.morr...@gmail.com
>
> > wrote:
> > I think it'd be cool if Crack.NET had a visual process selector of window
> > thumbnails, instead of a list of processes.  You could use the official dwm
> > apis for that...or vanilla win32 to get a bitmap of each hwnd.
>
> > -Jer
>
> > On Wed, Nov 5, 2008 at 1:07 PM, Josh Smith <flappleja...@gmail.com> wrote:
>
> >> You've piqued my interest...Tell me more...
>

Jeremiah Morrill

unread,
Nov 5, 2008, 4:21:04 PM11/5/08
to wpf-di...@googlegroups.com
If I got time later on this week I'll give it a shot.  Maybe a control that uses GDI thumbnails for XP and will automatically use DWM if on Vista.

-Jer

Jeremiah Morrill

unread,
Nov 5, 2008, 4:22:44 PM11/5/08
to wpf-di...@googlegroups.com
BTW, did you guys get a Windows 7 SDK on those hard drives from PDC?  If so, could anyone send me the dwmapi.h file.  I'm curious what goodies are in there...Maybe the goodies might be somewhere else.

-Jer

Marlon Grech

unread,
Nov 5, 2008, 5:54:15 PM11/5/08
to wpf-di...@googlegroups.com
hey Jer... you are the bomb!!!! COOL STUFF!
--
Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/

Reply all
Reply to author
Forward
0 new messages