Recent

Author Topic: Detect Foreground app  (Read 1783 times)

caiov1n1c1us

  • New Member
  • *
  • Posts: 14
Detect Foreground app
« on: April 30, 2024, 01:14:46 am »
Hi guys!

I'm doing a project with presets linked to apps. Like these gamer mouses, you know? Or StreamDeck.
In the control panel the user will be able to link a preset to a specific ".exe" and the software will change the preset if this app is in the foreground.

The problem is that the only way I know to find which app is in the foreground is using the "caption mode". I need to find out which ".exe" is in the foreground because the user will link the executable file and not the caption.

Is there anyway to do this?

Thanks guys!   

440bx

  • Hero Member
  • *****
  • Posts: 4487
Re: Detect Foreground app
« Reply #1 on: April 30, 2024, 01:17:06 am »
On what O/S ?
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

caiov1n1c1us

  • New Member
  • *
  • Posts: 14
Re: Detect Foreground app
« Reply #2 on: April 30, 2024, 01:56:16 am »

440bx

  • Hero Member
  • *****
  • Posts: 4487
Re: Detect Foreground app
« Reply #3 on: April 30, 2024, 02:17:52 am »
In that case, using GetForegroundWindow() followed by  GetWindowThreadProcessId would get you about halfway there.  Once you have the process id then you can get the image file name which is the .exe you're looking for.

with the process id, you can open the process (OpenProcess), which gives you a process handle and with the process handle, you get the image file name (GetProcessImageFileNameA).  That does it.

As long as you don't mind using the Windows API, it's not that hard.

I probably have code somewhere that does it.  I think I even posted it here in the forums sometime in the past.

HTH.

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

caiov1n1c1us

  • New Member
  • *
  • Posts: 14
Re: Detect Foreground app
« Reply #4 on: April 30, 2024, 02:31:57 am »
In that case, using GetForegroundWindow() followed by  GetWindowThreadProcessId would get you about halfway there.  Once you have the process id then you can get the image file name which is the .exe you're looking for.

with the process id, you can open the process (OpenProcess), which gives you a process handle and with the process handle, you get the image file name (GetProcessImageFileNameA).  That does it.

As long as you don't mind using the Windows API, it's not that hard.

I probably have code somewhere that does it.  I think I even posted it here in the forums sometime in the past.

HTH.

Thanks! I'll search about it!
If you have some code would be very helpful!

440bx

  • Hero Member
  • *****
  • Posts: 4487
Re: Detect Foreground app
« Reply #5 on: April 30, 2024, 02:43:04 am »
Thanks! I'll search about it!
If you have some code would be very helpful!
I was looking for the post where I posted an example that might be useful to you but I couldn't find it (the forum post.)

OTH, I found the code I posted on my hard drive, so I'll post it again here.  It's attached to this post.  Note that the purpose of the code isn't to get the image file name of the foreground window but, it does that among other things.

Specifically, the attached code enumerates all the windows (foreground or not) and displays information related to it including the image file name (among other things.)

What I'm saying is, you can "cannibalize" that.  Instead of enumerating the windows, just use GetForegroundWindow() and you should likely be all set.  Make the output window as wide as you can because there is information that is not visible with the default size on the right hand side

HTH.


(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

caiov1n1c1us

  • New Member
  • *
  • Posts: 14
Re: Detect Foreground app
« Reply #6 on: April 30, 2024, 02:58:02 am »
Thanks! I'll search about it!
If you have some code would be very helpful!
I was looking for the post where I posted an example that might be useful to you but I couldn't find it (the forum post.)

OTH, I found the code I posted on my hard drive, so I'll post it again here.  It's attached to this post.  Note that the purpose of the code isn't to get the image file name of the foreground window but, it does that among other things.

Specifically, the attached code enumerates all the windows (foreground or not) and displays information related to it including the image file name (among other things.)

What I'm saying is, you can "cannibalize" that.  Instead of enumerating the windows, just use GetForegroundWindow() and you should likely be all set.  Make the output window as wide as you can because there is information that is not visible with the default size on the right hand side

HTH.

Nice! Thank you so much!

440bx

  • Hero Member
  • *****
  • Posts: 4487
Re: Detect Foreground app
« Reply #7 on: April 30, 2024, 03:15:25 am »
Nice! Thank you so much!
You're welcome.

In the meantime I modified another example I had laying around to use GetForegroundWindow.  It is attached to this post.  It shows the current foreground window (click around the screen and watch the value change.)

Between this example and the previous one, you can probably get the result you were looking for.

HTH.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

caiov1n1c1us

  • New Member
  • *
  • Posts: 14
Re: Detect Foreground app
« Reply #8 on: May 04, 2024, 03:23:47 am »
Nice! Thank you so much!
You're welcome.

In the meantime I modified another example I had laying around to use GetForegroundWindow.  It is attached to this post.  It shows the current foreground window (click around the screen and watch the value change.)

Between this example and the previous one, you can probably get the result you were looking for.

HTH.


Can you help me one more time?

First let me explain. This is a project I'm working on with my dad. He's the real programmer here, I just try sometimes hahaha

He tried your suggestions, but had some difficulty.

Instead he made an alternative dll with Delphi to communicate with Lazarus.
In Delphi we receive a string as expected, but in Lazarus nothing happens.
I'm attaching the files here, could you check if there's anything wrong?

Thanks!

440bx

  • Hero Member
  • *****
  • Posts: 4487
Re: Detect Foreground app
« Reply #9 on: May 04, 2024, 04:06:01 am »
I see two potential problems in the code you attached.

In the HTML page, the statement "Path := @GetForegroundAppPath;" is incorrect.  The variable "Path" is being assigned the address of the function GetForeground.... instead of the result of calling that function.

In the pdf file, the line "Path := GetForegroundAppPath;" looks correct BUT, that function is returning a pointer to char.  if the pointer the function is returning is to a stack variable then once it leaves the dll, it is no longer any good.

To summarize, in the HTML page the "Path := " statement is wrong.  In the pdf document, the most likely problem is that the dll is returning a stale pointer, unfortunately, without seeing the dll code that is limited to being a guess. 

That's all I can do for you.

Lastly, you don't need a dll, cannibalize the code of the second example I gave you, from the looks of it, that's really all you need.

HTH.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 15555
  • Censorship about opinions does not belong here.
Re: Detect Foreground app
« Reply #10 on: May 04, 2024, 07:09:41 am »
You can probably wrap the PChar and return a Pascal string by using SetString().
If I smell bad code it usually is bad code and that includes my own code.

 

TinyPortal © 2005-2018