ivan_pozdeev
ivan_pozdeev

Reputation: 35998

EnumWindows and CloseHandle

Do I need to call CloseHandle() on handles returned by EnumWindows()?

Upvotes: 0

Views: 588

Answers (2)

Hans Passant
Hans Passant

Reputation: 941257

You only ever use CloseHandle() on handles returned by functions in kernel32. They are reference counted, closing the last handle destroys the object. File, mutexes, events, that sort of object.

Window handles are different, their lifetime is determined by the user. Or an explicit DestroyWindow() call in a program. You don't want to call DestroyWindow() when you enumerate windows.

GDI handles are different yet, they often have dedicated release functions. CreatePen vs DeleteObject, GetDC vs ReleaseDC, CreateDC vs DeleteDC, etc. In case of doubt, always consult the corresponding creation function to see how it needs to be released again. The SDK documentation never skips that. This is otherwise documented by omission, the EnumWindows documentation doesn't say that you need to release the handle so you don't.

So no.

Upvotes: 6

Daniel A. White
Daniel A. White

Reputation: 190907

I would say no. That is for destroying things that you no longer need. You didn't call CreateWindow.

Upvotes: 0

Related Questions