Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a HWND from inside a DLL?

I have a DLL that I want to play sounds using Direct Sound. In order to play sounds, I need the HWND of the executable. I don't have a HWND of the executable that loads the DLL. How do I get that in the DLL without passing it in from the executable?

like image 392
zooropa Avatar asked Jan 22 '11 02:01

zooropa


2 Answers

You could use GetCurrentProcessId to get the current process Id.

You could then call EnumWindows, and check each window with GetWindowThreadProcessId to find a window associated with your process.

However, an easier option might be to just generate your own Window. You can create a 1x1 pixel window that is not visible, and use it with Direct Sound.

This has the advantage of working even if your calling process doesn't have a usable window (or deletes window handles regularly).

like image 68
Reed Copsey Avatar answered Oct 07 '22 05:10

Reed Copsey


Call GetGUIThreadInfo on the main thread. This gets you a bunch of HWNDs. If you need a top-level HWND, pick any valid one (not all values may be filled) and find its top level ancestor with GetAncestor(GA_ROOT).

like image 24
MSalters Avatar answered Oct 07 '22 04:10

MSalters