Get Window Handle of calling app in a DLL function

Everything else that doesn't fall into one of the other PB categories.
Worm
User
User
Posts: 20
Joined: Wed Apr 14, 2004 2:32 am
Location: Grand Rapids, MI
Contact:

Get Window Handle of calling app in a DLL function

Post 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?
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

GetForegroundWindow_()
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
Worm
User
User
Posts: 20
Joined: Wed Apr 14, 2004 2:32 am
Location: Grand Rapids, MI
Contact:

Post 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.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

What if the application doesn't even have a window?
Worm
User
User
Posts: 20
Joined: Wed Apr 14, 2004 2:32 am
Location: Grand Rapids, MI
Contact:

Post by Worm »

Good point, but in this case I know there will always be a window.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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)?
Worm
User
User
Posts: 20
Joined: Wed Apr 14, 2004 2:32 am
Location: Grand Rapids, MI
Contact:

Post by Worm »

Passing the handle is an option, but if I can obtain the handle through code I'd rather do that.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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.
I may look like a mule, but I'm not a complete ass.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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
BERESHEIT
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post 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.
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
Worm
User
User
Posts: 20
Joined: Wed Apr 14, 2004 2:32 am
Location: Grand Rapids, MI
Contact:

Post 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.
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post 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. :wink:
--Kale

Image
Post Reply