windows


How to get path of current active application window?


I want to get the path to the executable file of the current active Window.
I tried:
var
WindowModuleFileName : array[0..100] of Char;
sourceWindow: Hwnd;
begin
sourceWindow := GetActiveWindow;
GetWindowModuleFileName(sourceWindow, PChar(WindowModuleFileName), sizeof(WindowModuleFileName));
ShowMessage(WindowModuleFileName);
end;
But it returned correct answer only when my application window was active.
What am I doing wrong?
You can't use GetWindowModuleFileName to locate files for other processes than your own, as stated on GetModuleFileName MSDN:
Retrieves the fully-qualified path for the file that contains the specified module. The module must have been loaded by the current process.
To locate the file for a module that was loaded by another process,
use the GetModuleFileNameEx function.
Therefore, you have to use GetModuleFileNameEx combined with GetWindowThreadProcessId/GetForegroundWindow. This will return you what you need:
uses
Winapi.Windows, Winapi.PsAPI, System.SysUtils;
function GetCurrentActiveProcessPath: String;
var
pid : DWORD;
hProcess: THandle;
path : array[0..4095] of Char;
begin
GetWindowThreadProcessId(GetForegroundWindow, pid);
hProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, FALSE, pid);
if hProcess <> 0 then
try
if GetModuleFileNameEx(hProcess, 0, #path[0], Length(path)) = 0 then
RaiseLastOSError;
result := path;
finally
CloseHandle(hProcess);
end
else
RaiseLastOSError;
end;
GetActiveWindow gets the window handle to the active window that belongs to the calling thread only.
You need to use GetForegroundWindow() function instead of GetActiveWindow().

Related Links

Win 10 UAP ListView Conditional Separator
Using a Qt Help file in an installed app
PowerShell script to remove file from hidden folders
How to filter Microsoft Service using PowerShell
Multi-window cygwin manager tools?
erlang.mk buildtool is unable to detect windows
Age Rating Issues when Publishing to the Windows Store Using Unity3D
PhoneGap Cordova error Windows 10
How to set the Environment path variable ignoring the current user's path variable
How to change hardware requirements in Windows Universal App?
what is the base relocation table in the PE file format?
How can I run this VBS script from a shared network drive, from command line on multiple computers?
Jenkins on Windows 2008 R2 SSH
Open file with Batch file
Unable to install heroku core plugins
Change directory to startup folder

Categories

HOME
shell
jpa
cobalt
eslint
apache-jena
ecmascript-6
datastore
apache-spark-mllib
jelastic
sitecore
access
google-cloud-functions
dojo
hugo
interpolation
32bit-64bit
minecraft-forge
ycsb
gkturnbasedmatch
flatpak
docx
capistrano3
coin3d
powershell-v5.0
sigma.js
drive
asciidoctor-pdf
href
r-mice
event-store
highstock
maze
custom-post-type
underscore.js
8051
subclass
failover
constexpr
scrollview
memory-address
biztalk-rule-engine
contacts
jwplayer6
mnist
batching
reason
alphabetical
google-earth-engine
laravel-elixir
chronicle-map
ssrs-2014
strftime
ovf
opennms
offline.js
swingworker
reachability
usart
webjars
resourcemanager
rspec2
nss
cda
pushbots
false-sharing
contentcontrol
phpgrid
concurrentmodification
sciruby
include-guards
procedural-programming
adehabitathr
nodelist
jbake
rickshaw
formview
verold
buffering
qscrollarea
neos-server
apache-abdera
patricia-trie
boost-msm
rmiregistry
flex-mobile
saxparseexception
fsevents
xcelsius
uideviceorientation
updatesourcetrigger
tabpanel
murmurhash
echo3
data-mapping
cldc
scrubyt
software-lifecycle
fault-tolerance
firefox-3
lemmatization
online-storage

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile