Convert from HWND to *CWnd ?

win32 used Ask the Experts™

My problem is I have to call a create function
..Create(IDD_??, CWnd* parrentWnd)

I want the parrent window to be the desktop window, I want to get the dektop window caling this function: GetDesktopWindow()

But can't do like this:
Create(IDD.., GetDesktopWindow()) Because HWND is not *CWnd

Can I solve this problem, how can I convert HWND to *CWnd ?

Shoud I create a
CWnd *wnd;
and attach the HWND to it ?

It's just a guess
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Yes, the Attach() member of CWnd should be used:

CWnd mWnd;

 or try  GetDeskTopWindow()->GetSafeHwnd ();
Top Expert 2012

Alternatively, you could

CWnd* pWnd = CWnd::FromHandlePermanent ( GetDesktopWindow());
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Why dont u straightaway use GetDesktopWindow()
It returns a CWnd*

Isn't Windows great?  How can be get bored with it when there are so many ways to do even the simplest thing?
You can't always rely on CWnd::FromHandlePermanent working so you should generally be prepared to create a CWnd and call Attach if it fails, but in this case there shouldn't be a problem because the desktop window will be in the permanent map.

The guy who mentioned GetDesktopWindow returning a CWnd meant the static function of CWnd. This is undoubtedly your best option, thus:
Create(IDD.., CWnd::GetDesktopWindow())

MSDN warns the pointer is temporary, but that doesn't matter because the Create function just ends up extracting the HWND again and using that, after all the trouble you went to...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial