Get Window Handle of calling app in a DLL function
Get Window Handle of calling app in a DLL function
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?
Any ideas?
- Fluid Byte
- Addict
- Posts: 2336
- Joined: Fri Jul 21, 2006 4:41 am
- Location: Berlin, Germany
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.
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.
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
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
BERESHEIT
-
- Enthusiast
- Posts: 767
- Joined: Sat Jan 24, 2004 6:56 pm
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.
So, the answer for me in this instance is to make the handle a parameter that gets passed in.
Thanks to all.
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.dell_jockey wrote:passing a handle IS code too. Put it in the DLL attach procedure and you're good to go.Worm wrote:Passing the handle is an option, but if I can obtain the handle through code I'd rather do that.
