0

We run into a problem with temporary handles.

As I read here: What is the lifetime of a CWnd obtained from CWnd::FromHandle? CWnd::FromHandle() will create a temporary handle. Our plan was to store the handle for later use, but this won't work.

We also tried CWnd::FromHandlePermanent() but this somehow returns a null pointer .

And also mpWnd->Attach(mhWnd); threw an Access violation exception.

If possible we would like to store the CWnd rather than the HWnd and call FromHandle() everytime the CWnd is required. Does anybody know a solution for this?

user3292642
  • 711
  • 2
  • 8
  • 32

1 Answers1

0

You get only a permanent CWnd object if you subclass an existing window, or you create a CWnd with one of the CWnd::Create functions.

You should not use Attach in this situation. Attach is used by SubclassWindow and by the internal Create hooks. If you use Attach without subclassing, the window will not be removed from the handle map upon destruction.

So when you get an error with the Attach function then either the mpwnd object isn't valid, or the CWnd object is already subclassed.

Also, it doesn't make sense to me why you want to store a CWnd pointer. Use a handle and if needed use FromHandle. The drawback of subclassing windows always is: that if your program later tries to subclass the window finally this fails, because it is already subclassed.

Also your question doesn't give information WHY you want to do this.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
xMRi
  • 14,982
  • 3
  • 26
  • 59