Author Topic: Get Active Window's Executable Path  (Read 375 times)


  • Moderator
  • Sr. Member
  • *****
  • Posts: 331
  • Can you dig it?
Get Active Window's Executable Path
« on: March 29, 2018, 11:03:59 am »
Sometimes you may want to access or make modifications to files or folders inside the root directory of a given game or application while it is running. The below C# inline function provides you the file path to the active window's executable, which can then be manipulated to find whatever files you need to modify or other tasks you'd like to perform. For demonstration purposes I've placed the function inside a small loop so that you can see the path retrieval in action across multiple window selections.

Cheers!  :)

VoiceAttack Actions
Code: [Select]
Start Loop : Repeat 5 Times
Inline C# Function: Retrieve active window's executable file path, wait until execution finishes
Write '[Blue] {TXT:~~ActiveProcessID} = {TXT:~~ActiveProcessExePath}' to log
Pause 1 second
End Loop

C# Inline Function
Referenced Assemblies: System.dll; System.Core.dll; System.Management.dll
Code: [Select]
using System;
using System.Linq;
using System.Management;
using System.Runtime.InteropServices;

public class VAInline
public void main()
IntPtr hwnd = GetForegroundWindow(); // Initialize integer pointer, retrieve the active window's handle, and store it within the pointer
uint ProcessID; // Initialize unsigned integer
GetWindowThreadProcessId(hwnd, out ProcessID); // Call external function for obtaining the process ID associated with the inputted window handle       
VA.SetText("~~ActiveProcessID", ProcessID.ToString()); // Send active window's process ID back to VoiceAttack as a text variable
string myPath = GetProcessPath((int)ProcessID); // Obtain active window's executable file path and store it in string variable
VA.SetText("~~ActiveProcessExePath", myPath); // Send the active window's executable file path back to VoiceAttack as a text variable
hwnd = IntPtr.Zero; // Set hwnd to IntPtr.Zero (just for good measure)

// Function for retrieving executable file path associated with inputted process ID
public string GetProcessPath(int processId)
string MethodResult = "";
string Query = "SELECT ExecutablePath FROM Win32_Process WHERE ProcessId = " + processId;
using (ManagementObjectSearcher mos = new ManagementObjectSearcher(Query))
using (ManagementObjectCollection moc = mos.Get())
string ExecutablePath = (from mo in moc.Cast<ManagementObject>() select mo["ExecutablePath"]).First().ToString();
MethodResult = ExecutablePath;
catch //(Exception ex)
VA.WriteToLog("Error encountered within inline function during executable file path retrieval.");
return MethodResult;

public static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out uint ProcessId);

private static extern IntPtr GetForegroundWindow();

// References: