Page 1 of 1
Get Window Handle of calling app in a DLL function
Posted: Wed Feb 14, 2007 2:56 pm
by Worm
I'm writing a DLL and would like to be able to obtain the handle of the calling applications window programmatically rather than passing it as a parameter. All the things I've tried seem to fail.
Any ideas?
Posted: Wed Feb 14, 2007 3:20 pm
by Fluid Byte
GetForegroundWindow_()
Posted: Wed Feb 14, 2007 3:32 pm
by Worm
Thanks. I should know by now to be as specific as possible.
The solution you provided would work as long as the window is the active window. The problem comes if the app doesn't have focus and the function is called, it would give me the wrong handle. The DLL function will more than likely be called within a timer event, so there is a real chance the window will *not* be the active window.
Posted: Wed Feb 14, 2007 4:01 pm
by Trond
What if the application doesn't even have a window?
Posted: Wed Feb 14, 2007 4:07 pm
by Worm
Good point, but in this case I know there will always be a window.
Posted: Wed Feb 14, 2007 4:14 pm
by Trond
But Windows doesn't know every process has a window, so it doesn't have a function to get the window from the process. Why don't you just pass the window handle (from which you can get the process, since every window has a process)?
Posted: Wed Feb 14, 2007 4:19 pm
by Worm
Passing the handle is an option, but if I can obtain the handle through code I'd rather do that.
Posted: Wed Feb 14, 2007 4:32 pm
by srod
The only other option which comes to mind is using something like EnumThreadWindows_(), but then you'll proably need to check the window title etc.
Posted: Wed Feb 14, 2007 4:52 pm
by netmaestro
It seems like there should be an easier way, but this will work:
Code: Select all
Procedure ListWindows(hwnd, param)
Shared CallerWindow
GetWindowThreadProcessId_(hwnd, @processid)
If GetCurrentProcessId_() = processid
CallerWindow = hwnd
ProcedureReturn #False
Else
ProcedureReturn #True
EndIf
EndProcedure
ProcedureDLL GetCallerhWnd()
Shared CallerWindow
EnumWindows_(@ListWindows(), 0)
ProcedureReturn CallerWindow
EndProcedure
Posted: Tue Feb 20, 2007 2:15 pm
by dell_jockey
Worm wrote:Passing the handle is an option, but if I can obtain the handle through code I'd rather do that.
passing a handle IS code too. Put it in the DLL attach procedure and you're good to go.
Posted: Tue Feb 20, 2007 2:24 pm
by Worm
I agree, but the concept was to not need to worry about that and simply let the DLL get the handle. Netmaestro's code seems to work perfectly when I use it in PB, but if I try it in a DLL, I do not get the handle I need.
So, the answer for me in this instance is to make the handle a parameter that gets passed in.
Thanks to all.
Posted: Tue Feb 20, 2007 2:26 pm
by Kale
dell_jockey wrote:Worm wrote:Passing the handle is an option, but if I can obtain the handle through code I'd rather do that.
passing a handle IS code too. Put it in the DLL attach procedure and you're good to go.
Yeah. As wierd and long winded as it may seem netmaestro's code is actually the right way to get the handle, but it's far more simpler to just pass the handle to the dll.
