Author Topic: Help getting active window's executable file path  (Read 551 times)

Exergist

  • Full Member
  • ***
  • Posts: 178
  • Can you dig it?
Help getting active window's executable file path
« on: April 03, 2017, 07:53:13 am »
I'm going to repost my "How Do I" in this thread section in the hopes that more plugin-savvy eyes check it out and might lend their programming wizardry to help me figure this out. Cheers!


ORIGINAL POST

Thanks to the VoiceAttack help document and the interwebs I found a way to use a VoiceAttack VB.net inline function to obtain the filepath for the active window's executable file. The code is as such:

Referenced Assemblies: System.dll;System.Core.dll;System.Data.dll;System.Data.DataSetExtensions.dll;System.Deployment.dll;System.Drawing.dll;System.Net.Http.dll;System.Windows.Forms.dll;System.Xml.dll;System.Xml.Linq.dll
Code: [Select]
'Adapted from code by jay20aiii ==> http://www.vbforums.com/showthread.php?668460-Get-path-of-active-window
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Data
Imports System.Drawing
Imports System.Diagnostics
Imports System.Windows.Forms
Imports System.Linq
Imports System.Xml.Linq
Imports System.Threading.Tasks

Public Class VAInline

Private Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hWnd As IntPtr, ByRef lpdwProcessId As UInt32) As UInt32
Private Declare Function GetForegroundWindow Lib "user32.dll" Alias "GetForegroundWindow" () As IntPtr

    Public Sub Main()

            'Get the ForeGround Window
            Dim hWnd As IntPtr = GetForegroundWindow()
            Dim ProcessID As UInt32 = Nothing
            'Get the Window's Process using it's ID (obtained using the GetWindowThreadProcessId API)
            GetWindowThreadProcessId(hWnd, ProcessID)
            Dim Proc As Process = Process.GetProcessById(ProcessID)
               
            'Send the filepath back to VoiceAttack and store in a text variable
            VA.SetText("application path", Proc.MainModule.FileName)

    End Sub

End Class

This seems to work great for getting the executable filepath for 32 bit applications. However it doesn't work for 64 bit applications.

I have nearly zero knowledge about VB.net (and C#), and I was hoping that the community might help me figure out how to get the executable filepath for a 64 bit application through VoiceAttack.

Thanks!

Exergist

  • Full Member
  • ***
  • Posts: 178
  • Can you dig it?
Re: Help getting active window's executable file path
« Reply #1 on: May 22, 2017, 11:49:25 am »
I found a way to solve my problem and posted the updated VB.net code here. Hopefully others will be able to make use of this!
« Last Edit: July 12, 2017, 08:04:53 am by Exergist »